Backend-agnostic authentication, account lifecycle, and typed REST API framework for Unity 6. It separates HTTP transport, auth backend adapters, encrypted token storage, and Unity Multiplayer connection approval so teams can connect to a self-hosted server or BaaS (Supabase, Firebase, PlayFab, Steam, or custom APIs) without coupling gameplay or UI code to one vendor.
- Backend-agnostic adapter architecture (
IAuthBackendAdapter): Swap self-hosted servers or BaaS providers without changing UI or session code. Ships withRestAuthBackendAdapterand a customizableRestEndpointMap, an in-processMockAuthTransportfor offline Editor and test workflows, and starter adapters for Supabase and Firebase underSamples~/BackendAdapters/. - Typed REST client (
ApiHttpClient): AsyncUnityWebRequestpipeline withAuthResult<T>and structuredApiErrorenvelopes, orderedIApiInterceptormiddleware (RequestIdInterceptor,BearerAuthInterceptor), bounded exponential backoff with jitter (ExponentialBackoffRetryPolicyhandling network errors,408,429withRetry-After, and5xx), and an in-memory HTTP trace ring buffer with password redaction. - Single-flight
401token refresh: When multiple concurrent authenticated requests fail with401 Unauthorized,ApiHttpClientcoalesces them onto a single refresh call viaOnUnauthorizedRefreshAsyncand replays every waiting request using the rotated access token. - Pure C# JWT utilities and encrypted token storage: Includes RFC 7515 Base64Url helpers, claims decoding, HS256 signing, and constant-time HS256 verification (
JwtUtility), plusInMemoryTokenStoreandEncryptedFileTokenStore(AES-256-CBC with Encrypt-then-MAC HMAC-SHA256 tamper rejection). - Unity Multiplayer connection approval (
MultiplayerAuthBridge): Transport-agnostic UTF-8MultiplayerAuthTicketserialization compatible with Netcode for GameObjects (ConnectionData), Mirror, and FishNet, paired with dedicated-server validators (LocalHs256JwtValidator,RemoteIntrospectionValidator), optional client version enforcement, and an activeclientIdtoaccountIdregistry with duplicate-login control. - Scene and Canvas component (
AccountController): Drop-inMonoBehaviourexposing Inspector-bindableUnityEventhooks (onSignedIn,onSignedOut,onSessionRefreshed,onProfileUpdated,onAuthError,onBusyStateChanged), zero-argument button helpers (SignInAsGuest,SignOut), and runtime custom adapter injection. - UI Toolkit console and inspector: Four-tab
AuthWorkbenchWindow(Tools > HaruChanZ64 > Networking > UnityAuthCore > Auth and API Console) for testing auth flows, inspecting decoded JWT claims, triggering 3-way concurrent401refresh tests, verifying multiplayer tickets, and reviewing live HTTP traces, plus a customAuthFrameworkConfigInspector. - Optional Dockerized reference server and OpenAPI 3.0 contract: Ships Docs~/openapi.yaml alongside a self-contained Node.js 22 LTS reference backend in Server~/ (
scryptsalted password hashing, rotating refresh tokens with reuse-detection family revocation, per-IP rate limiting, and/v1/auth/introspect).
- Unity
6000.0or newer - Zero required external Unity package dependencies
- Optional for the self-hosted reference backend in
Server~/: Docker or Node.js22+ - Optional for multiplayer networking integration: Netcode for GameObjects, Mirror, or FishNet (not required to compile
UnityAuthCore.Runtime)
- Open Window > Package Manager in Unity 6 (
6000.0+). - Click + (top-left) and choose Install package from git URL....
- Enter:
https://github.com/haruchanz64/UnityAuthCore.git
Add the package path to Packages/manifest.json:
{
"dependencies": {
"com.haruchanz64.unity-auth-core": "file:../Plugins/UnityAuthCore"
}
}- Open Tools > HaruChanZ64 > Networking > UnityAuthCore > Auth and API Console. In the default MockInProcess sandbox mode you can sign in as a guest, register or log in with email/password, upgrade a guest account, inspect decoded HS256 JWT claims, run the 3-request single-flight
401refresh test, and verify multiplayer handshake tickets with no external server. - Create a configuration asset via Assets > Create > UnityAuthCore > Auth Framework Config. Choose
backendMode(MockInProcess,RestEndpoint, orCustomAdapter), settokenStorage(EncryptedFileorInMemory), and configure retry and refresh skew values. - (Optional) Start the Dockerized reference backend in
Server~/when testing against a live HTTP server:
cd Plugins/UnityAuthCore/Server~
cp .env.example .env
docker compose up --build -dSet backendMode to RestEndpoint, point baseUrl at http://localhost:8080, and align expectedIssuer, expectedAudience, and serverHs256Secret on your server-side config asset with Server~/.env.
4. Add AccountController (from UnityAuthCore.Components) to a bootstrap GameObject, assign your AuthFrameworkConfig asset, and bind onSignedIn, onSignedOut, onAuthError, and onBusyStateChanged in the Inspector.
5. Click Open in Auth & API Console in the AuthFrameworkConfig Inspector to test directly against that asset's settings.
Every request sent through ApiHttpClient.SendRawAsync or SendJsonAsync<T> runs through a deterministic loop:
- Before-send interceptors: Runs registered
IApiInterceptor.OnBeforeSendAsynchooks in registration order.ApiHttpClientautomatically registersRequestIdInterceptor(X-Request-Id), andAuthClientaddsBearerAuthInterceptorto attachAuthorization: Bearer <token>on requests whereApiRequest.RequiresAuthis true. - Transport dispatch and after-receive hooks: Sends the request via
IHttpTransport(UnityWebRequestTransportin live mode orMockAuthTransportin mock mode), runsOnAfterReceiveAsyncin reverse registration order, and records a boundedApiTraceEntry(redacting any JSON body containing"password"). - Single-flight
401recovery: If a protected request returns HTTP401on its first attempt andOnUnauthorizedRefreshAsyncis assigned,ApiHttpClientlocks around a shared_inFlightRefreshTaskso all concurrent401callers await the same refresh request. When refresh succeeds, each request loops and re-runsBearerAuthInterceptorto pick up the new access token. - Transient retry policy: For non-auth transient failures (
IsNetworkError,408,429,5xx),ExponentialBackoffRetryPolicyhonors numericRetry-Afterheaders or computes bounded exponential backoff with random jitter. Refresh and logout requests setApiRequest.SkipRetry = trueso rotating refresh tokens are never replayed automatically.
AuthClient coordinates IAuthBackendAdapter, ApiHttpClient, and ITokenStore. Calling InitializeAsync() loads any persisted AuthSession from ITokenStore and refreshes it automatically if it is within refreshSkewSeconds of expiry. Protected calls (GetProfileAsync, UpdateProfileAsync, UpgradeGuestWithEmailAsync) also run a proactive expiry check before hitting the network.
When TokenStorageMode.EncryptedFile is selected, EncryptedFileTokenStore writes [32-byte HMAC-SHA256][16-byte random IV][AES-256-CBC ciphertext] to Application.persistentDataPath. On load, the HMAC tag is verified in constant time before decryption; modified or truncated files return null.
MultiplayerAuthBridge decouples account authentication from your chosen networking library:
- Client side:
MultiplayerAuthBridge.BuildClientConnectionPayload(session, clientVersion)(oraccountController.BuildMultiplayerConnectionPayload()) encodes aMultiplayerAuthTicket(accessToken,clientVersion,nonce,issuedAtUnixSeconds) as UTF-8 JSON bytes ready for NGOConnectionData, Mirror, or FishNet handshakes. - Server side:
ApproveConnectionAsync(clientId, payload, validator)validates the payload structure, checksRequiredClientVersionif configured, verifies the token using eitherLocalHs256JwtValidator(offline constant-time HS256 check) orRemoteIntrospectionValidator(POST /v1/auth/introspector BaaS lookup), enforcesRejectDuplicateAccountLogin(or reportsDisplacedPreviousClientId), and stores theMultiplayerVerifiedIdentityuntilHandleClientDisconnected(clientId)is called.
Inject any BaaS or custom backend adapter before signing in, while keeping UI scenes bound to AccountController:
using UnityAuthCore.Components;
using UnityAuthCore.Core;
using UnityAuthCore.Http;
using UnityAuthCore.Samples.Backends;
using UnityEngine;
public sealed class AuthBootstrap : MonoBehaviour
{
[SerializeField] private AccountController accountController;
[SerializeField] private string supabaseUrl = "https://your-project.supabase.co";
[SerializeField] private string supabaseAnonKey = "your-publishable-key";
private void Awake()
{
var http = new ApiHttpClient(new UnityWebRequestTransport());
IAuthBackendAdapter adapter = new SampleSupabaseAuthAdapter(http, supabaseUrl, supabaseAnonKey);
accountController.ConfigureCustomAdapter(adapter);
}
public async void LoginPlayer(string email, string password)
{
AuthResult<AuthSession> result = await accountController.LoginAsync(email, password);
if (!result.Succeeded)
{
Debug.LogWarning($"Login failed [{result.Error.errorCode}]: {result.Error.message}");
return;
}
Debug.Log($"Signed in as {result.Data.user.displayName} ({result.Data.user.accountId})");
}
}Attach custom headers, telemetry, or request signing through IApiInterceptor, and reuse AuthClient.HttpClient for authenticated game API calls that automatically benefit from single-flight 401 refresh:
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityAuthCore.Core;
using UnityAuthCore.Engine;
using UnityEngine;
public sealed class ClientVersionInterceptor : IApiInterceptor
{
private readonly string _version;
public ClientVersionInterceptor(string version) => _version = version;
public Task OnBeforeSendAsync(ApiRequest request, CancellationToken ct = default)
{
request.Headers["X-Client-Version"] = _version;
return Task.CompletedTask;
}
public Task OnAfterReceiveAsync(ApiRequest request, HttpRawResponse response, CancellationToken ct = default)
{
return Task.CompletedTask;
}
}
[Serializable]
public sealed class InventoryDto
{
public int gold;
}
public static class GameApiExample
{
public static async Task<AuthResult<InventoryDto>> FetchInventoryAsync(AuthClient client, string apiBaseUrl)
{
client.HttpClient.AddInterceptor(new ClientVersionInterceptor(Application.version));
var req = new ApiRequest
{
Method = "GET",
Url = $"{apiBaseUrl.TrimEnd('/')}/v1/inventory",
RequiresAuth = true
};
return await client.HttpClient.SendJsonAsync<InventoryDto>(req);
}
}Serialize the client ticket before connecting, and verify it on the Dedicated Server or Host during connection approval:
using System.Threading.Tasks;
using UnityAuthCore.Components;
using UnityAuthCore.Config;
using UnityAuthCore.Core;
using UnityAuthCore.Multiplayer;
using UnityEngine;
public sealed class MatchServerAuthHandler
{
private readonly MultiplayerAuthBridge _bridge = new MultiplayerAuthBridge
{
RequiredClientVersion = "1.0.0",
RejectDuplicateAccountLogin = false
};
private readonly IServerTokenValidator _validator;
public MatchServerAuthHandler(AuthFrameworkConfig serverConfig, IAuthBackendAdapter introspectionAdapter = null)
{
_validator = serverConfig.CreateServerTokenValidator(introspectionAdapter);
}
public static byte[] BuildClientPayload(AccountController controller)
{
// Assign to NGO NetworkManager.Singleton.NetworkConfig.ConnectionData before StartClient()
return controller.BuildMultiplayerConnectionPayload();
}
public async Task<MultiplayerApprovalResult> ApproveAsync(ulong clientId, byte[] payload)
{
MultiplayerApprovalResult approval = await _bridge.ApproveConnectionAsync(clientId, payload, _validator);
if (!approval.Approved)
{
Debug.LogWarning($"Rejected client {clientId} [{approval.RejectionCode}]: {approval.RejectionReason}");
return approval;
}
if (approval.DisplacedPreviousClientId.HasValue)
{
Debug.Log($"Kick previous client {approval.DisplacedPreviousClientId.Value} for duplicate login.");
}
Debug.Log($"Approved client {clientId} as {approval.Identity.accountId}");
return approval;
}
public void OnDisconnected(ulong clientId) => _bridge.HandleClientDisconnected(clientId);
}Decode JWT claims on the client for UI display without exposing a signing secret:
using UnityAuthCore.Core;
using UnityAuthCore.Engine;
using UnityEngine;
public static class JwtInspectionExample
{
public static void LogCurrentClaims(AuthClient client)
{
if (client.TryDecodeCurrentClaims(out JwtClaims claims, out string error))
{
Debug.Log($"sub={claims.sub}, iss={claims.iss}, exp={claims.exp}, expired={claims.IsExpired()}");
}
else
{
Debug.LogWarning($"Cannot decode access token: {error}");
}
}
}| Type | Namespace | Purpose | Built-in implementation |
|---|---|---|---|
IAuthBackendAdapter |
UnityAuthCore.Core |
Translates auth, profile, account linking, and introspection operations to any backend | RestAuthBackendAdapter (plus Supabase and Firebase starters in Samples~/) |
IHttpTransport |
UnityAuthCore.Core |
Dispatches raw ApiRequest calls and returns HttpRawResponse |
UnityWebRequestTransport, MockAuthTransport |
IApiInterceptor |
UnityAuthCore.Core |
Modifies outgoing requests or inspects incoming responses | RequestIdInterceptor, BearerAuthInterceptor |
IRetryPolicy |
UnityAuthCore.Core |
Controls retry attempts and backoff delays for transient failures | ExponentialBackoffRetryPolicy |
ITokenStore |
UnityAuthCore.Core |
Persists and clears AuthSession across app launches |
InMemoryTokenStore, EncryptedFileTokenStore |
IServerTokenValidator |
UnityAuthCore.Core |
Verifies client access tokens on a Dedicated Server or Host | LocalHs256JwtValidator, RemoteIntrospectionValidator |
AuthClient |
UnityAuthCore.Engine |
High-level session manager coordinating adapter, HTTP client, refresh, and storage | AuthClient (including AuthClient.CreateMock) |
ApiHttpClient |
UnityAuthCore.Http |
Typed JSON HTTP pipeline with interceptors, retries, traces, and single-flight 401 refresh |
ApiHttpClient |
AccountController |
UnityAuthCore.Components |
Drop-in MonoBehaviour with Inspector UnityEvent hooks and custom adapter injection |
AccountController |
AuthFrameworkConfig |
UnityAuthCore.Config |
ScriptableObject factory configuring backend mode, endpoints, storage, retries, and server validation | AuthFrameworkConfig |
MultiplayerAuthBridge |
UnityAuthCore.Multiplayer |
Builds connection tickets, validates handshakes, and tracks active clientId to accountId mappings |
MultiplayerAuthBridge |
JwtUtility |
UnityAuthCore.Security |
Pure C# Base64Url encoding, JWT claims decoding, HS256 signing, and constant-time verification | JwtUtility |
Import samples from the Package Manager Samples tab:
- Backend Adapters (Supabase, Firebase, Custom) (
Samples~/BackendAdapters):SampleSupabaseAuthAdapter: StarterIAuthBackendAdapterwired to Supabase Auth (GoTrue REST API).SampleFirebaseAuthAdapter: StarterIAuthBackendAdapterwired to Firebase Identity Toolkit and Secure Token REST APIs.
- Multiplayer Authenticators (NGO, Mirror, FishNet) (
Samples~/MultiplayerAdapters):SampleMultiplayerAuthWiring: MonoBehaviour demonstratingAccountController,AuthFrameworkConfig.CreateServerTokenValidator, andMultiplayerAuthBridgeconnection approval and disconnect cleanup.
The package also includes Docs~/openapi.yaml (OpenAPI 3.0.3 contract) and an optional Dockerized Node.js 22 LTS reference server in Server~/.
The package includes EditMode NUnit tests in Tests/Editor/UnityAuthCoreTests.cs runnable from Window > General > Test Runner (EditMode tab):
GuestSignIn_UpgradeToEmail_AndUpdateProfile_WorksEndToEnd: verifies guest sign-in, guest-to-email upgrade preservingaccountId, and profile updates.Register_DuplicateEmailReturns409_AndBadPasswordReturns401: verifies email registration (201), duplicate email conflict (409 EMAIL_ALREADY_EXISTS), and invalid login credentials (401 INVALID_CREDENTIALS).ConcurrentUnauthorizedCalls_TriggerSingleFlightRefreshExactlyOnce: corrupts the access token across 3 concurrent protected calls and asserts all 3 succeed while/v1/auth/refreshis hit once.RefreshTokenReuseDetection_RevokesEntireSessionFamily: verifies refresh rotation and confirms replaying an already-consumed refresh token revokes the entire token family.JwtUtility_SignsVerifiesAndRejectsTamperedOrExpiredTokens: verifies HS256 token creation, claims decoding, valid verification, signature tamper rejection (SIGNATURE_INVALID), and expiry rejection (TOKEN_EXPIRED).EncryptedFileTokenStore_PersistsSessionAndRejectsTamperedFile: verifies AES-256-CBC round-trip persistence and confirms flipping a byte fails HMAC verification and returnsnull.MultiplayerAuthBridge_ValidatesTicketsViaLocalHs256AndRemoteIntrospect: verifies ticket encoding,RequiredClientVersion,LocalHs256JwtValidator, duplicate login rejection (ACCOUNT_ALREADY_CONNECTED), disconnect cleanup, andRemoteIntrospectionValidator.RetryPolicy_RetriesTransient429AndRecovers: verifiesExponentialBackoffRetryPolicyretries a simulated429and succeeds on the second attempt.AccountController_SupportsCustomAdapterInjectionAndFiresUnityEvents: verifiesConfigureCustomAdapter,UnityEventcallbacks (onSignedIn,onSignedOut), and multiplayer payload creation.
ISSUER_MISMATCHwhen switching from Mock mode to the Docker reference server:AuthFrameworkConfig.expectedIssuerdefaults to"unity-auth-core-mock", whileServer~/.env.exampledefaultsJWT_ISSUERto"unity-auth-core-server". MatchexpectedIssuer,expectedAudience, andserverHs256Secreton your server-sideAuthFrameworkConfigto the values configured inServer~/.env.- Never ship
serverHs256Secretin game client builds: PopulateserverHs256Secretonly in dedicated server builds or local dev tests. For client-hosted or BaaS setups where the host cannot hold a signing secret, useServerValidationMode.RemoteIntrospectionwith a server-only key or provider token lookup. JsonUtilitydeserialization constraints:ApiHttpClient.SendJsonAsync<T>usesUnityEngine.JsonUtility, so response DTOs must be[Serializable]classes or structs with public fields (not auto-properties or bare top-level JSON arrays). For arbitrary JSON responses, callSendJsonAsync<string>orSendRawAsyncand parse with your preferred JSON library.EncryptedFileTokenStorereturnsnullafter tampering or device/bundle ID changes: Any modified or truncated session file fails Encrypt-then-MAC verification and is treated as no saved session. When no custompassphraseis passed, the key is derived fromApplication.identifierandSystemInfo.deviceUniqueIdentifier, so changing the bundle ID invalidates previously saved files.AccountControllerfalls back to Mock mode if unassigned: If neitherconfignorConfigureCustomAdapter(...)is supplied beforeStart(),AccountControllercreates an in-processAuthClient.CreateMock(out _).- Starter BaaS sample stubs:
SampleFirebaseAuthAdapterleavesGetProfileAsync,UpdateProfileAsync, andLinkEmailPasswordAsyncreturning HTTP501 NOT_IMPLEMENTED, andSampleSupabaseAuthAdapter.UpdateProfileAsyncdelegates toGetProfileAsync. Extend those methods to match your project's profile schema before production use. - Reference server persistence:
Server~/src/server.mjsstores accounts and refresh tokens in a single JSON file (data/auth-store.json) and tracks rate limits in process memory. Replace the storage layer with a database (such as PostgreSQL or Redis) before running multiple server instances in production.