-
Notifications
You must be signed in to change notification settings - Fork 54
feat(cardinal): authenticate game tokens scoped to organization/project #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryanditjia
wants to merge
9
commits into
main
Choose a base branch
from
ryandi/game-player-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−94
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
34d51cf
feat(cardinal): authenticate game player tokens
ryanditjia a072f14
chore(cardinal): satisfy auth lint checks
ryanditjia 1cca115
feat(cardinal): scope player tokens to organization and project
ryanditjia e2cb5e4
chore(cardinal): remove stray persona comment
ryanditjia 88211ff
fix(cardinal): stop checking the game token issuer
ryanditjia 887ecd2
fix(cli): send the player ID header for MCP dev auth
ryanditjia 9af74c7
fix(cardinal): keep trimming trailing slashes from the auth URL
ryanditjia 73a3a58
refactor(cardinal): match main's JWKS URL and fix the test instead
ryanditjia 3254b70
docs(cardinal): update Unity auth and shard examples to the current S…
ryanditjia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package cardinal | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/ed25519" | ||
| "encoding/base64" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/goccy/go-json" | ||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAuthenticatorArgusAcceptsGamePlayerToken(t *testing.T) { | ||
| publicKey, privateKey, err := ed25519.GenerateKey(nil) | ||
| require.NoError(t, err) | ||
|
|
||
| server := newAuthTestServer(t, publicKey) | ||
| authenticator, err := newAuthenticatorArgus(server.URL, "argus", "rampage") | ||
| require.NoError(t, err) | ||
|
|
||
| token := signGameToken(t, privateKey, jwt.RegisteredClaims{ | ||
| Subject: "player-123", | ||
| Issuer: server.URL + "/auth", | ||
| Audience: jwt.ClaimStrings{"argus/rampage"}, | ||
| ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute)), | ||
| }) | ||
| player, err := authenticator.authenticate(context.Background(), requestWithBearer(t, token)) | ||
| require.NoError(t, err) | ||
| require.Equal(t, &Player{ID: "player-123"}, player) | ||
| } | ||
|
|
||
| func TestAuthenticatorArgusRejectsInvalidGameClaims(t *testing.T) { | ||
|
ryanditjia marked this conversation as resolved.
|
||
| publicKey, privateKey, err := ed25519.GenerateKey(nil) | ||
| require.NoError(t, err) | ||
| _, otherPrivateKey, err := ed25519.GenerateKey(nil) | ||
| require.NoError(t, err) | ||
|
|
||
| server := newAuthTestServer(t, publicKey) | ||
| authenticator, err := newAuthenticatorArgus(server.URL, "argus", "rampage") | ||
| require.NoError(t, err) | ||
|
|
||
| validClaims := jwt.RegisteredClaims{ | ||
| Subject: "player-123", | ||
| Issuer: server.URL + "/auth", | ||
| Audience: jwt.ClaimStrings{"argus/rampage"}, | ||
| ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute)), | ||
| } | ||
| for _, test := range []struct { | ||
| name string | ||
| key ed25519.PrivateKey | ||
| claims jwt.RegisteredClaims | ||
| }{ | ||
| {name: "untrusted signature", key: otherPrivateKey, claims: validClaims}, | ||
| { | ||
| name: "wrong audience", key: privateKey, | ||
| claims: jwt.RegisteredClaims{ | ||
| Subject: validClaims.Subject, Issuer: validClaims.Issuer, | ||
| Audience: jwt.ClaimStrings{"argus/other"}, ExpiresAt: validClaims.ExpiresAt, | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing audience", key: privateKey, | ||
| claims: jwt.RegisteredClaims{ | ||
| Subject: validClaims.Subject, Issuer: validClaims.Issuer, ExpiresAt: validClaims.ExpiresAt, | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing expiry", | ||
| key: privateKey, | ||
| claims: jwt.RegisteredClaims{ | ||
| Subject: validClaims.Subject, Issuer: validClaims.Issuer, Audience: validClaims.Audience, | ||
| }, | ||
| }, | ||
| { | ||
| name: "expired", | ||
| key: privateKey, | ||
| claims: jwt.RegisteredClaims{ | ||
| Subject: validClaims.Subject, | ||
| Issuer: validClaims.Issuer, | ||
| Audience: validClaims.Audience, | ||
| ExpiresAt: jwt.NewNumericDate( | ||
| time.Now().Add(-time.Minute), | ||
| ), | ||
| }, | ||
| }, | ||
| { | ||
| name: "missing subject", | ||
| key: privateKey, | ||
| claims: jwt.RegisteredClaims{ | ||
| Issuer: validClaims.Issuer, Audience: validClaims.Audience, ExpiresAt: validClaims.ExpiresAt, | ||
| }, | ||
| }, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| token := signGameToken(t, test.key, test.claims) | ||
| _, authErr := authenticator.authenticate(context.Background(), requestWithBearer(t, token)) | ||
|
ryanditjia marked this conversation as resolved.
|
||
| require.Error(t, authErr) | ||
|
ryanditjia marked this conversation as resolved.
|
||
| }) | ||
| } | ||
|
|
||
| t.Run("non-EdDSA algorithm", func(t *testing.T) { | ||
| token := jwt.NewWithClaims(jwt.SigningMethodHS256, validClaims) | ||
| token.Header["kid"] = "test-key" | ||
| signed, signErr := token.SignedString([]byte("test-secret")) | ||
| require.NoError(t, signErr) | ||
| _, authErr := authenticator.authenticate(context.Background(), requestWithBearer(t, signed)) | ||
| require.Error(t, authErr) | ||
| }) | ||
| } | ||
|
|
||
| func TestAuthenticatorDevUsesPlayerID(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| req.Header.Set("X-Player-Id", " player-123 ") | ||
|
|
||
| player, err := (authenticatorDev{}).authenticate(context.Background(), req) | ||
| require.NoError(t, err) | ||
| require.Equal(t, &Player{ID: "player-123"}, player) | ||
|
|
||
| _, err = (authenticatorDev{}).authenticate(context.Background(), httptest.NewRequest(http.MethodGet, "/", nil)) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func newAuthTestServer(t *testing.T, publicKey ed25519.PublicKey) *httptest.Server { | ||
| t.Helper() | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
| if req.URL.Path != "/auth/jwks" { | ||
| http.NotFound(w, req) | ||
| return | ||
| } | ||
| _ = json.NewEncoder(w).Encode(map[string]any{"keys": []map[string]string{{ | ||
| "kty": "OKP", | ||
| "crv": "Ed25519", | ||
| "alg": "EdDSA", | ||
| "use": "sig", | ||
| "kid": "test-key", | ||
| "x": base64.RawURLEncoding.EncodeToString(publicKey), | ||
| }}}) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
| return server | ||
| } | ||
|
|
||
| func signGameToken(t *testing.T, privateKey ed25519.PrivateKey, claims jwt.RegisteredClaims) string { | ||
| t.Helper() | ||
| token := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claims) | ||
| token.Header["kid"] = "test-key" | ||
| signed, err := token.SignedString(privateKey) | ||
| require.NoError(t, err) | ||
| return signed | ||
| } | ||
|
|
||
| func requestWithBearer(t *testing.T, token string) *http.Request { | ||
| t.Helper() | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| return req | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The startup sample never handles the
SignedOutcase, even though the paragraph above saysAuthStatustells the game what it can do. A fresh install (no saved login) follows this snippet and gets no render and no direction to sign in; a failedRefreshAuthAsyncis likewise silently swallowed. Render theSignedOutstate and make refresh failure visible so readers following the sample end up authenticated or with a clear next step.Prompt for AI agents