Authentication
How your game server proves who it is to Alogame. A runnable sample you can download is sign.js; every snippet below is the same logic.
Onboarding
Alogame's data team does these steps with you before your first request:
- Registers your game and its markets (ISO country codes).
- Issues a key: a
key_idand asecret. The secret is shown once; store it in your secret manager. If it is lost, Alogame issues a new key. - Declares the schema (Schema_Version 1) of every log type your game sends, with you, and locks it.
- Gives you the base URL, written
BASE_URLbelow: the root address of the Game Log service for your environment (dev or production). A sandbox key on the dev environment comes first; production keys are issued after acceptance.
Signing every request
Send three headers:
| Header | Value |
|---|---|
X-Key-Id | your key_id |
X-Timestamp | current Unix time in seconds |
X-Signature | lower-case hex of HMAC_SHA256(secret, string_to_sign) |
string_to_sign is five lines joined by a line feed (\n, no trailing line feed):
| # | Line | Value | Example |
|---|---|---|---|
| 1 | X-Timestamp | the same value as the header, Unix seconds | 1700000000 |
| 2 | METHOD | upper case: always POST (batch and file) | POST |
| 3 | path | the request path as you send it, without scheme, host or query | /ingest/v1/games/7/logs/login |
| 4 | canonical_query | the query rebuilt as described below. It is never empty: every request carries client_id and schema_version | client_id=default&schema_version=1 |
| 5 | body_sha256 | lower-case hex SHA-256 of the exact bytes you send | 015abd7f…f862 |
Building canonical_query
- Split the query into
name=valuepairs and decode each name and value. - Sort the pairs by name, then by value.
- Encode every name and value again with
encodeURIComponent(or the equivalent in your language; the code tabs below do it). - Join the pairs with
&.
The order in which you write the query parameters does not matter: ?schema_version=1&client_id=default gives client_id=default&schema_version=1.
Worked example
Secret secret, POST to /ingest/v1/games/7/logs/login?client_id=default&schema_version=1, body {"a":1}, timestamp 1700000000. The string_to_sign is these five lines (the fourth is the canonical query):
1700000000
POST
/ingest/v1/games/7/logs/login
client_id=default&schema_version=1
015abd7f5cc57a2dd94b7590f04ad8084273905ee33ec5cebeae62276a97f862
HMAC_SHA256('secret', string_to_sign) is the X-Signature:
9b8bdeae4a7743d2370c0348405bff38d47faef46cd8fbc3997c4541eeca313d
If your code prints a different value, print your string_to_sign and compare it with the five lines above.
- Re-serialising the JSON after signing. Sign the exact bytes you send.
- Signing the uncompressed bytes of a full-log file. Sign the compressed bytes you upload; the fifth value is their SHA-256, the same value you send in
X-Content-Sha256. - Sending through a proxy that rewrites the path or the query. Both are signed.
- Changing the case or the encoding of the path after signing.
A request whose timestamp differs from Alogame's clock by more than ±300 seconds is rejected, so keep your server clock synchronised (NTP). Because the path and query are signed, a captured request cannot be replayed against another log_type, schema_version or client_id.
- JavaScript
- Python
- Java
- Go
- C#
- PHP
- curl (bash)
const crypto = require('node:crypto');
function canonicalQuery(query) {
const pairs = [...new URLSearchParams(query).entries()];
pairs.sort(([ak, av], [bk, bv]) => (ak < bk ? -1 : ak > bk ? 1 : av < bv ? -1 : av > bv ? 1 : 0));
return pairs.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
}
// `url` is the path plus query as sent, e.g. '/ingest/v1/games/42/logs/login'
function sign(secret, timestamp, method, url, body) {
const at = url.indexOf('?');
const path = at < 0 ? url : url.slice(0, at);
const query = at < 0 ? '' : url.slice(at + 1);
const bodySha256 = crypto.createHash('sha256').update(body).digest('hex');
const stringToSign = [String(timestamp), method.toUpperCase(), path, canonicalQuery(query), bodySha256].join('\n');
return crypto.createHmac('sha256', secret).update(stringToSign).digest('hex');
}
import hashlib
import hmac
from urllib.parse import parse_qsl, quote
# Same set encodeURIComponent leaves unescaped.
SAFE = "-_.!~*'()"
def canonical_query(query):
pairs = sorted(parse_qsl(query, keep_blank_values=True))
return "&".join(f"{quote(k, safe=SAFE)}={quote(v, safe=SAFE)}" for k, v in pairs)
# `url` is the path with its query exactly as sent, e.g. '/ingest/v1/games/42/logs/login'
# `body` is bytes
def sign(secret, timestamp, method, url, body):
path, _, query = url.partition("?")
body_sha256 = hashlib.sha256(body).hexdigest()
string_to_sign = "\n".join([str(timestamp), method.upper(), path, canonical_query(query), body_sha256])
return hmac.new(secret.encode(), string_to_sign.encode(), hashlib.sha256).hexdigest()
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HexFormat;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Sign {
// Same set encodeURIComponent leaves unescaped.
private static final String SAFE = "-_.!~*'()";
private static String encode(String s) {
StringBuilder sb = new StringBuilder();
for (byte b : s.getBytes(StandardCharsets.UTF_8)) {
char c = (char) (b & 0xff);
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || SAFE.indexOf(c) >= 0) {
sb.append(c);
} else {
sb.append(String.format("%%%02X", b & 0xff));
}
}
return sb.toString();
}
static String canonicalQuery(String query) {
if (query.isEmpty()) return "";
List<String[]> pairs = new ArrayList<>();
for (String part : query.split("&")) {
int eq = part.indexOf('=');
String k = eq < 0 ? part : part.substring(0, eq);
String v = eq < 0 ? "" : part.substring(eq + 1);
pairs.add(new String[] {
URLDecoder.decode(k, StandardCharsets.UTF_8), URLDecoder.decode(v, StandardCharsets.UTF_8) });
}
pairs.sort(Comparator.<String[], String>comparing(p -> p[0]).thenComparing(p -> p[1]));
StringBuilder sb = new StringBuilder();
for (String[] p : pairs) {
if (sb.length() > 0) sb.append('&');
sb.append(encode(p[0])).append('=').append(encode(p[1]));
}
return sb.toString();
}
// `url` is the path with its query exactly as sent, e.g. "/ingest/v1/games/42/logs/login"
public static String sign(String secret, long timestamp, String method, String url, byte[] body) throws Exception {
int at = url.indexOf('?');
String path = at < 0 ? url : url.substring(0, at);
String query = at < 0 ? "" : url.substring(at + 1);
String bodySha256 = HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(body));
String stringToSign = String.join("\n",
String.valueOf(timestamp), method.toUpperCase(), path, canonicalQuery(query), bodySha256);
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
return HexFormat.of().formatHex(mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)));
}
}
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/url"
"sort"
"strings"
)
// Same set encodeURIComponent leaves unescaped.
const safe = "-_.!~*'()"
func encode(s string) string {
var sb strings.Builder
for _, b := range []byte(s) {
if (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9') || strings.IndexByte(safe, b) >= 0 {
sb.WriteByte(b)
} else {
fmt.Fprintf(&sb, "%%%02X", b)
}
}
return sb.String()
}
func canonicalQuery(query string) string {
values, _ := url.ParseQuery(query)
var pairs [][2]string
for k, vs := range values {
for _, v := range vs {
pairs = append(pairs, [2]string{k, v})
}
}
sort.Slice(pairs, func(i, j int) bool {
if pairs[i][0] != pairs[j][0] {
return pairs[i][0] < pairs[j][0]
}
return pairs[i][1] < pairs[j][1]
})
out := make([]string, len(pairs))
for i, p := range pairs {
out[i] = encode(p[0]) + "=" + encode(p[1])
}
return strings.Join(out, "&")
}
// rawURL is the path with its query exactly as sent, e.g. "/ingest/v1/games/42/logs/login"
func sign(secret string, timestamp int64, method, rawURL string, body []byte) string {
path, query, _ := strings.Cut(rawURL, "?")
bodySum := sha256.Sum256(body)
stringToSign := strings.Join([]string{
fmt.Sprint(timestamp), strings.ToUpper(method), path, canonicalQuery(query), hex.EncodeToString(bodySum[:]),
}, "\n")
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(stringToSign))
return hex.EncodeToString(mac.Sum(nil))
}
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public static class GameLogSigner
{
// Same set encodeURIComponent leaves unescaped.
private const string Safe = "-_.!~*'()";
private static string Encode(string s)
{
var sb = new StringBuilder();
foreach (var b in Encoding.UTF8.GetBytes(s))
{
var c = (char)b;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || Safe.IndexOf(c) >= 0)
sb.Append(c);
else
sb.Append('%').Append(b.ToString("X2"));
}
return sb.ToString();
}
private static string Decode(string s) => Uri.UnescapeDataString(s.Replace('+', ' '));
public static string CanonicalQuery(string query)
{
if (query.Length == 0) return "";
var pairs = query.Split('&').Select(part =>
{
var eq = part.IndexOf('=');
return eq < 0 ? (K: Decode(part), V: "") : (K: Decode(part[..eq]), V: Decode(part[(eq + 1)..]));
}).OrderBy(p => p.K, StringComparer.Ordinal).ThenBy(p => p.V, StringComparer.Ordinal);
return string.Join("&", pairs.Select(p => Encode(p.K) + "=" + Encode(p.V)));
}
// `url` is the path with its query exactly as sent, e.g. "/ingest/v1/games/42/logs/login"
public static string Sign(string secret, long timestamp, string method, string url, byte[] body)
{
var at = url.IndexOf('?');
var path = at < 0 ? url : url[..at];
var query = at < 0 ? "" : url[(at + 1)..];
var bodySha256 = Convert.ToHexString(SHA256.HashData(body)).ToLowerInvariant();
var stringToSign = string.Join("\n", timestamp.ToString(), method.ToUpperInvariant(), path, CanonicalQuery(query), bodySha256);
var mac = HMACSHA256.HashData(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(stringToSign));
return Convert.ToHexString(mac).ToLowerInvariant();
}
}
<?php
// Same set encodeURIComponent leaves unescaped.
function enc(string $s): string {
return strtr(rawurlencode($s), ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')']);
}
function canonicalQuery(string $query): string {
if ($query === '') return '';
$pairs = [];
foreach (explode('&', $query) as $part) {
[$k, $v] = array_pad(explode('=', $part, 2), 2, '');
$pairs[] = [urldecode($k), urldecode($v)];
}
usort($pairs, fn($a, $b) => strcmp($a[0], $b[0]) ?: strcmp($a[1], $b[1]));
return implode('&', array_map(fn($p) => enc($p[0]) . '=' . enc($p[1]), $pairs));
}
// $url is the path with its query exactly as sent, e.g. '/ingest/v1/games/42/logs/login'
function sign(string $secret, $timestamp, string $method, string $url, string $body): string {
$at = strpos($url, '?');
$path = $at === false ? $url : substr($url, 0, $at);
$query = $at === false ? '' : substr($url, $at + 1);
$stringToSign = implode("\n", [
(string) $timestamp, strtoupper($method), $path, canonicalQuery($query), hash('sha256', $body),
]);
return hash_hmac('sha256', $stringToSign, $secret);
}
# For a quick check only: `openssl -hmac` puts the secret on the command line, where other users of the machine can read it
# (process list) and shell history may keep it. In a real client sign in code, as in the other tabs.
BODY='[["acc-1","role-1","Hero","VN","2026-11-02 10:15:00","google","s1","10.0.0.1","Android","14","dev-1","Pixel 8"]]'
TS=$(date +%s)
URL_PATH="/ingest/v1/games/$GAME_ID/logs/login"
QUERY="client_id=default&schema_version=1"
BODY_SHA=$(printf '%s' "$BODY" | openssl dgst -sha256 -hex | sed 's/^.* //')
# the fourth value is the canonical query (already sorted here)
SIG=$(printf '%s\n%s\n%s\n%s\n%s' "$TS" "POST" "$URL_PATH" "$QUERY" "$BODY_SHA" | openssl dgst -sha256 -hmac "$GAME_LOG_SECRET" -hex | sed 's/^.* //')
curl -sS -X POST "$BASE_URL$URL_PATH?$QUERY" \
-H "Content-Type: application/json" \
-H "X-Key-Id: $GAME_LOG_KEY_ID" -H "X-Timestamp: $TS" -H "X-Signature: $SIG" \
--data-binary "$BODY"
To check your implementation, sign('secret', '1700000000', 'POST', '/ingest/v1/games/7/logs/login?client_id=default&schema_version=1', '{"a":1}') must return 9b8bdeae4a7743d2370c0348405bff38d47faef46cd8fbc3997c4541eeca313d.