Files
aevolution/include/evo/client/auth.hpp
T
dingoandClaude Opus 5.5 a97f0c3fea
Foundation / checks (ecs) (push) Successful in 2m21s
Foundation / checks (deploy) (push) Successful in 4m9s
Foundation / checks (tsan) (push) Successful in 9m48s
Foundation / checks (client) (push) Successful in 19m17s
Foundation / checks (network) (push) Successful in 19m44s
Foundation / checks (checks) (push) Successful in 42m37s
Foundation / checks (postgres) (push) Successful in 45m40s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 3m13s
Foundation / image (push) Successful in 3m27s
ADR-0131: per-profile client sign-in for several clients on one machine
Only one client per machine could sign in: the saved login lived under a single
Credential Manager name, and Keycloak ignores prompt=select_account, so the
browser reused its single sign-on session. --profile NAME keeps one saved login
per profile (the default name is unchanged), and authorization asks for
prompt=login so the browser shows the login form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 17:35:53 -04:00

116 lines
4.3 KiB
C++

#pragma once
#include "evo/client/launcher.hpp"
#include <chrono>
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <span>
#include <stop_token>
#include <string>
#include <string_view>
namespace evo::client {
inline constexpr std::string_view auth_redirect = "http://127.0.0.1:47600/callback";
inline constexpr std::string_view auth_client_id = "aevolution-game";
enum class AuthError : std::uint8_t {
unavailable,
cancelled,
listener,
timeout,
browser,
transport,
invalid_response,
denied,
token_expired,
email_unverified,
throttled,
service_unavailable,
credential_store,
logout_incomplete
};
[[nodiscard]] std::string_view auth_error_text(AuthError error);
struct AuthGrant {
GrantRecord bytes{};
std::string account, endpoint, server_name;
std::uint64_t region{}, expires_at{};
};
struct AuthTokens {
std::string access, refresh;
std::uint64_t lifetime{};
};
struct AuthorizationCode {
std::string code, verifier;
};
struct AuthHttpReply {
unsigned status{};
std::string body;
};
enum class AuthEndpoint : std::uint8_t { token, grant, logout, revoke };
// Opaque tokens stay inside the worker/platform boundary. No UI, logs or plaintext files.
class AuthPlatform {
public:
virtual ~AuthPlatform() = default;
[[nodiscard]] virtual std::expected<AuthorizationCode, AuthError>
authorize(std::stop_token stop) = 0;
[[nodiscard]] virtual std::expected<AuthHttpReply, AuthError> post(AuthEndpoint endpoint,
std::string_view body,
std::string_view bearer,
std::stop_token stop) = 0;
[[nodiscard]] virtual std::expected<std::string, AuthError> load_refresh() = 0;
[[nodiscard]] virtual bool save_refresh(std::string_view token) = 0;
[[nodiscard]] virtual bool erase_refresh() = 0;
};
// A sign-in profile keeps its own saved login, so several clients on one machine can
// use different accounts. Empty is the default profile; otherwise 1..32 of [A-Za-z0-9_-].
[[nodiscard]] bool auth_profile_valid(std::string_view profile);
// The Credential Manager target for a profile.
[[nodiscard]] std::string auth_credential_name(std::string_view profile);
// Windows; null elsewhere or for an invalid profile.
[[nodiscard]] std::unique_ptr<AuthPlatform> native_auth_platform(std::string_view profile = {});
[[nodiscard]] std::string auth_form_escape(std::string_view value);
[[nodiscard]] std::string auth_base64url(std::span<const std::uint8_t> bytes);
[[nodiscard]] std::string auth_authorization_url(std::string_view state,
std::string_view challenge);
// Only the exact loopback path, one matching state, and one code (or denial) are accepted.
[[nodiscard]] std::expected<std::string, AuthError> auth_callback(std::string_view target,
std::string_view state);
[[nodiscard]] std::expected<AuthTokens, AuthError> auth_tokens(const AuthHttpReply& reply);
[[nodiscard]] std::expected<AuthGrant, AuthError> auth_grant(const AuthHttpReply& reply,
std::uint64_t now_seconds);
enum class AuthPhase : std::uint8_t {
signed_out,
signing_in,
ready,
renewing,
signing_out,
failed
};
struct AuthStatus {
AuthPhase phase{AuthPhase::signed_out};
bool supported{}, busy{}, signed_in{};
std::optional<AuthError> error;
};
// One joined worker, serialized refresh-token rotation; main-thread caller only.
// Destruction cancels browser waiting and joins bounded network work.
class AuthClient {
public:
explicit AuthClient(std::unique_ptr<AuthPlatform> platform);
~AuthClient();
AuthClient(const AuthClient&) = delete;
AuthClient& operator=(const AuthClient&) = delete;
[[nodiscard]] bool sign_in();
[[nodiscard]] bool request_grant();
[[nodiscard]] bool sign_out();
void cancel();
[[nodiscard]] AuthStatus status() const;
[[nodiscard]] std::optional<AuthGrant> take_grant();
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace evo::client