跳到主要内容

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:

  1. Registers your game and its markets (ISO country codes).
  2. Issues a key: a key_id and a secret. The secret is shown once; store it in your secret manager. If it is lost, Alogame issues a new key.
  3. Declares the schema (Schema_Version 1) of every log type your game sends, with you, and locks it.
  4. Gives you the base URL, written BASE_URL below: 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:

HeaderValue
X-Key-Idyour key_id
X-Timestampcurrent Unix time in seconds
X-Signaturelower-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):

#LineValueExample
1X-Timestampthe same value as the header, Unix seconds1700000000
2METHODupper case: always POST (batch and file)POST
3paththe request path as you send it, without scheme, host or query/ingest/v1/games/7/logs/login
4canonical_querythe query rebuilt as described below. It is never empty: every request carries client_id and schema_versionclient_id=default&schema_version=1
5body_sha256lower-case hex SHA-256 of the exact bytes you send015abd7f…f862

Building canonical_query​

  1. Split the query into name=value pairs and decode each name and value.
  2. Sort the pairs by name, then by value.
  3. Encode every name and value again with encodeURIComponent (or the equivalent in your language; the code tabs below do it).
  4. 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.

Common mistakes
  • 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.
Clock

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.

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');
}

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.