Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ add_library(lootlocker_core STATIC
src/core/request_id.cpp
src/core/rate_limiter.cpp
src/core/json.cpp
src/core/platform.cpp
src/core/query_parameters.cpp
src/core/session.cpp
src/core/state_writer.cpp
src/core/executor.cpp
src/core/client.cpp
Expand Down Expand Up @@ -190,6 +192,11 @@ foreach(module IN LISTS LOOTLOCKER_FEATURE_MODULES)
target_link_libraries(lootlocker_${module}
PUBLIC LootLocker::core
PRIVATE lootlocker_compile_options)

# A module may include `core`'s internal headers, because it is built inside the library
# (D18). An adopter cannot: `src/` is not on their include path, so the definitions are
# unreachable even though the declarations are visible.
target_include_directories(lootlocker_${module} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
endforeach()

# ---------------------------------------------------------------------------
Expand Down
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,61 @@ definition.
over a shared, immutable document, which is what makes it cheap to pass around and safe to
hold across a callback. Writing needs its own type.

### Persistence
### Authentication

Guest auth is the MVP's primary path: no credentials, no platform, just an identifier the
SDK generates once and reuses.

```cpp
lootlocker::auth::StartGuestSession(ll, [](lootlocker::Result<lootlocker::Session> r) {
if (!r.Ok()) {
my_log(r.Error().message);
return;
}
std::printf("player %s\n", r.Value().player_ulid.c_str());
});

while (ll.Update()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
```

**The identifier reuse is the whole point.** A guest who loses their identifier silently
becomes a new account with no inventory, so the SDK persists it and sends it back on the next
launch. You do not have to do anything: the resolved identifier is written *before* the
request is sent, so a crash between sending and receiving does not orphan the account the
server just created.

The resolution order is: `GuestSessionInputs::guest_identifier` if you set it, then the
persisted one, then a newly generated one. Setting it explicitly is for a game that manages
its own identity; leaving it unset is what almost every caller wants.

A successful session is stored on the client, so subsequent requests carry
`x-session-token` and `ll.GetSession()` returns it. `ll.HasSession()` is the cheap check.
**The token is never sent on an authentication request** — an authentication request is not
an authenticated request by definition.

```cpp
lootlocker::GuestSessionInputs inputs;

lootlocker::SessionOptionals optionals;
optionals.timezone = "Europe/Stockholm"; // empty = resolve: provider, then OS, then Etc/UTC
optionals.player_name = "Erik";
optionals.check_dlc = true;

inputs.optionals = optionals;

lootlocker::auth::StartGuestSession(ll, inputs, on_session);
```

`SessionOptionals` is the backend's `optionals` object verbatim — `timezone`, `player_name`,
`auto_create_profile`, `check_dlc`, `referral_code`, `referral_program_key`. A field equal to
the backend's default is omitted rather than sent.

With a `NullStateWriter` every launch generates a new identifier, which is the documented
cost of choosing not to persist.

## Persistence

The SDK remembers a session between launches through a `IStateWriter`. The default writes one
file next to the game; a console or an engine injects its own.
Expand Down
100 changes: 94 additions & 6 deletions include/lootlocker/auth.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,104 @@
#pragma once

// Placeholder for the auth module's public surface.
// Guest authentication (D16, T14).
//
// T8 scaffolds the target graph only. The real surface lands with T14 (guest), T15
// (white label), T16 (Steam) and T18 (end session + refresh), per
// specs-library/specs/discipline/sdk/cpp-sdk/api-sketch.md.
// A guest session is the MVP's primary auth path: no credentials, no platform, just an
// identifier the SDK generates once and reuses. **The reuse is the whole point** - a guest
// who loses their identifier silently becomes a new account with no inventory, so the SDK
// persists it (D8) and sends it back on the next launch.
//
// The identifier is generated here rather than by the caller, because a caller that has to
// invent one will invent a different one on the next launch.

#include <lootlocker/client.h>
#include <lootlocker/platform.h>
#include <lootlocker/request_id.h>
#include <lootlocker/result.h>
#include <lootlocker/session.h>

#include <functional>
#include <optional>
#include <string>

namespace lootlocker {

// The backend's `optionals` object, verbatim.
//
// **Named `optionals`, not `options`.** It is the backend's term for "a set of optional
// values", and `options` invokes a different meaning - configuration rather than
// per-request optional data. The other SDKs use the backend term for the same reason.
//
// Every field is optional, and the defaults are the backend's: an absent `timezone` means
// UTC, an absent `auto_create_profile` means true.
struct SessionOptionals {
// IANA format. Empty means "resolve it": the injected provider, then the OS, then
// `Etc/UTC` (D26).
std::string timezone;

// The player's display name. Empty means "leave it as it is".
std::string player_name;

// Whether to create a profile for a player who does not have one. Defaults to true;
// false makes the session fail with a 404 for an unknown player.
bool auto_create_profile = true;

// Whether the client reports owning DLC, so the server reconciles ownership at session
// start. Defaults to false.
bool check_dlc = false;

// The referrer's public UID, for attributing a referral on a first session. Ignored for
// returning players.
std::string referral_code;

// The referral program the code redeems. Omit when the game has a single active program.
std::string referral_program_key;
};

// Everything a guest session start takes.
//
// **An input struct rather than parameters plus overloads** (owner, 2026-11-26). Two
// optional parameters is four overloads and four is sixteen; a struct is one shape that
// gains fields without breaking callers. The zero-argument `StartGuestSession(ll, cb)` stays
// as the common call.
//
// Both fields are `Optional` because neither is required, and "unset" is a different
// statement from "set to empty" - the same distinction `Config::send_timezone` exists to
// preserve.
struct GuestSessionInputs {
// The guest identifier to use. Unset means "use the persisted one, or generate a new
// one", which is what almost every caller wants.
//
// **Not part of `optionals`.** The backend's `optionals` object does not contain it, and
// muddling the two would make the SDK's shape disagree with the wire's.
std::optional<std::string> guest_identifier;

std::optional<SessionOptionals> optionals;
};

using SessionCallback = std::function<void(Result<Session>)>;

namespace auth {

// Intentionally empty until T14. The header exists so the target graph and the
// include-path boundary (D18) can be verified before any feature is written.
// POST /game/v2/session/guest
//
// Starts a guest session and stores it on the client, so subsequent requests carry
// `x-session-token` and `Client::GetSession()` returns it.
//
// **The identifier is resolved in this order:** `inputs.guest_identifier` if set, then the
// one persisted by a previous session, then a newly generated one. Whatever is chosen is
// persisted before the request is sent, so a crash between sending and receiving does not
// lose it and orphan the account.
//
// **The session token is never sent on this request.** An authentication request is not an
// authenticated request, so attaching a token to it is wrong by definition - and a stale
// token from a previous session would be sent to an endpoint that has no use for it.
//
// Returns the id the executor assigned. The callback fires on the thread that calls
// `Client::Update()` (D5).
RequestId StartGuestSession(Client& ll, SessionCallback on_done);

RequestId StartGuestSession(Client& ll, const GuestSessionInputs& inputs,
SessionCallback on_done);

} // namespace auth
} // namespace lootlocker
23 changes: 23 additions & 0 deletions include/lootlocker/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <lootlocker/config.h>
#include <lootlocker/result.h>
#include <lootlocker/session.h>

#include <memory>
#include <string>
Expand Down Expand Up @@ -96,6 +97,28 @@ class Client {
// `FileStateWriter`.
std::weak_ptr<IStateWriter> GetStateWriter() const;

// The session, if one has been started.
//
// Returns a reference to the client's own copy rather than a value, so a per-frame read
// costs nothing. `HasSession()` is the cheap check; `Session()` is only valid when it is
// true.
bool HasSession() const;

// Precondition: `HasSession()`. Dereferencing without checking is a precondition
// violation, the same contract as `Result::Value()`.
const Session& GetSession() const;

// Sets the session directly, without a network call.
//
// For a game that restores a session from its own storage, and for a test. The SDK does
// not call this itself: `auth::StartGuestSession` sets the session when the response
// arrives.
void SetSession(const Session& session);

// Clears the session. Does not call the API - `auth::EndSession` is the call that ends
// a session server-side.
void ClearSession();

private:
// The correct form: the function is declared in `core` above, so the friend
// declaration names it qualified rather than declaring a new one in this scope.
Expand Down
50 changes: 50 additions & 0 deletions include/lootlocker/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

// The auth platform (D16, T14 review).
//
// A wire value, not a free string. The API accepts a fixed set of platform names and rejects
// anything else, so a typo in a string literal is a runtime 400 that a type would have caught
// at compile time. The names match the other SDKs' `LL_AuthPlatforms` exactly, because the
// backend compares them literally.

#include <lootlocker/json_value.h>
#include <lootlocker/result.h>

#include <string>

namespace lootlocker {

enum class Platform {
None = 0,
Guest,
WhiteLabel,
Steam,
PlayStationNetwork,
XboxOne,
NintendoSwitch,
AmazonLuna,
AppleSignIn,
AppleGameCenter,
Android,
GoogleSignIn,
GooglePlayGames,
EpicGames,
Meta,
Remote,
Discord,
};

// The wire name, e.g. `"guest"`. Never null.
const char* ToString(Platform platform);

// Parses a wire name. Returns false and leaves `out` untouched when the name is unknown,
// rather than defaulting to `None` - an unknown platform is a contract change, not a
// missing value.
bool TryParsePlatform(const std::string& name, Platform& out);

// The ADL deserialiser, so a platform round-trips through the JSON layer like any other
// field (D27). There is no `to_json`: a `JsonBuilder` writes into an object, so a bare enum
// has no key to write under. A caller serialises with `b.Set("platform", ToString(p))`.
Result<Empty> from_json(const JsonValue& j, Platform& out);

} // namespace lootlocker
55 changes: 55 additions & 0 deletions include/lootlocker/session.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

// The session (D8, D12). A wire mirror: fields are `snake_case` and match the JSON exactly.
//
// This is the one type that is both a wire mirror and a persisted record, which is why its
// field set is exactly what D8 says to store and nothing more.

#include <lootlocker/json_value.h>
#include <lootlocker/result.h>

#include <cstdint>
#include <string>
#include <type_traits>

namespace lootlocker {

struct Session {
std::string session_token;
std::string player_identifier;
std::string player_ulid;
std::string public_uid;
std::string player_name;
std::uint64_t player_id = 0; // uint64 on the wire; see D4
std::string player_created_at;
bool seen_before = false;
bool check_grant_notifications = false;
bool check_deactivation_notifications = false;
};

// `player_id` must stay an unsigned 64-bit integer.
//
// **This is R9's documented mitigation, not a guard against someone editing the struct.**
// The D4 spike showed both candidate parsers are exact by default, so the hazard is not the
// parser but a `double` field in our own structs - and the failure is silent, because a
// value above 2^53 loses precision without any error. The assert is what makes adding such
// a field a compile error rather than a support ticket.
static_assert(sizeof(Session::player_id) == 8 && !std::is_floating_point<decltype(Session::player_id)>::value,
"player_id must be an unsigned 64-bit integer (D4, R9)");

// Decodes a session response (D27).
//
// **Which fields are required, and why that is not a silent default.** D23 says a missing
// field is a decode error rather than a zero, and that rule exists to stop a field the SDK
// depends on being silently defaulted. It does not mean every field is mandatory:
//
// - `session_token`, `player_identifier`, `player_ulid`, `public_uid` and `player_id` are
// required. Without them the session is unusable or the player cannot be identified,
// and a default would be a lie.
// - `player_name` is optional, because an absent name genuinely means "this player has
// not set one". Defaulting it to empty is the correct reading, not a silent default.
// - `player_created_at` and the three booleans are optional for the same reason: they are
// display and notification hints, and the API omits them on some auth paths.
Result<Empty> from_json(const JsonValue& j, Session& out);

} // namespace lootlocker
Comment thread
kirre-bylund marked this conversation as resolved.
Resolved
Loading
Loading