Skip to content

Commit 580ee14

Browse files
AmanGIT07claude
andauthored
feat(auth): add auth_via claim to minted access tokens (#1808)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 1713cdd commit 580ee14

5 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎core/authenticate/service.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ func isInvalidGrantErr(err error) bool {
738738
// BuildToken creates an access token for the given subjectID
739739
func (s Service) BuildToken(ctx context.Context, principal Principal, metadata map[string]string) ([]byte, error) {
740740
metadata[token.SubTypeClaimsKey] = principal.Type
741+
metadata[token.AuthViaClaimKey] = principal.AuthVia.String()
741742
if principal.Type == schema.UserPrincipal && s.config.Token.Claims.AddUserEmailClaim {
742743
metadata[token.SubEmailClaimsKey] = principal.User.Email
743744
}

‎core/authenticate/service_test.go‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,3 +863,80 @@ func TestService_GetPrincipal_OrgStateGate(t *testing.T) {
863863
})
864864
}
865865
}
866+
867+
func TestService_BuildToken(t *testing.T) {
868+
userID := uuid.NewString()
869+
870+
tests := []struct {
871+
name string
872+
principal authenticate.Principal
873+
config authenticate.Config
874+
wantClaims map[string]string
875+
}{
876+
{
877+
name: "user via session gets sub_type, auth_via and email claims",
878+
principal: authenticate.Principal{
879+
ID: userID,
880+
Type: schema.UserPrincipal,
881+
AuthVia: authenticate.SessionClientAssertion,
882+
User: &user.User{ID: userID, Email: "jane@acme.org"},
883+
},
884+
config: authenticate.Config{Token: authenticate.TokenConfig{
885+
Claims: authenticate.TokenClaimConfig{AddUserEmailClaim: true},
886+
}},
887+
wantClaims: map[string]string{
888+
token.SubTypeClaimsKey: schema.UserPrincipal,
889+
token.AuthViaClaimKey: authenticate.SessionClientAssertion.String(),
890+
token.SubEmailClaimsKey: "jane@acme.org",
891+
},
892+
},
893+
{
894+
name: "service user via client credentials gets auth_via claim",
895+
principal: authenticate.Principal{
896+
ID: userID,
897+
Type: schema.ServiceUserPrincipal,
898+
AuthVia: authenticate.ClientCredentialsClientAssertion,
899+
},
900+
wantClaims: map[string]string{
901+
token.SubTypeClaimsKey: schema.ServiceUserPrincipal,
902+
token.AuthViaClaimKey: authenticate.ClientCredentialsClientAssertion.String(),
903+
},
904+
},
905+
{
906+
name: "pat principal gets auth_via and user_id claims",
907+
principal: authenticate.Principal{
908+
ID: "pat-token-id",
909+
Type: schema.PATPrincipal,
910+
AuthVia: authenticate.PATClientAssertion,
911+
User: &user.User{ID: userID},
912+
},
913+
wantClaims: map[string]string{
914+
token.SubTypeClaimsKey: schema.PATPrincipal,
915+
token.AuthViaClaimKey: authenticate.PATClientAssertion.String(),
916+
token.UserIDClaimKey: userID,
917+
},
918+
},
919+
{
920+
name: "principal without auth via gets empty auth_via claim",
921+
principal: authenticate.Principal{
922+
ID: userID,
923+
Type: schema.UserPrincipal,
924+
},
925+
wantClaims: map[string]string{
926+
token.SubTypeClaimsKey: schema.UserPrincipal,
927+
token.AuthViaClaimKey: "",
928+
},
929+
},
930+
}
931+
for _, tt := range tests {
932+
t.Run(tt.name, func(t *testing.T) {
933+
mockToken := mocks.NewTokenService(t)
934+
mockToken.EXPECT().Build(tt.principal.ID, tt.wantClaims).Return([]byte("signed-token"), nil)
935+
s := authenticate.NewService(nil, tt.config, nil, nil, mockToken, nil, nil, nil, nil, nil)
936+
937+
got, err := s.BuildToken(context.Background(), tt.principal, map[string]string{})
938+
assert.NoError(t, err)
939+
assert.Equal(t, []byte("signed-token"), got)
940+
})
941+
}
942+
}

‎core/authenticate/token/service.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
SubEmailClaimsKey = "email"
2626
SessionIDClaimKey = "sid"
2727
UserIDClaimKey = "user_id"
28+
AuthViaClaimKey = "auth_via"
2829
)
2930

3031
type Service struct {

‎test/e2e/regression/authentication_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ func (s *AuthenticationRegressionTestSuite) TestUserSession() {
354354
subType, ok := parsed.Get(token.SubTypeClaimsKey)
355355
s.Assert().True(ok)
356356
s.Assert().Equal(schema.PATPrincipal, subType)
357+
authVia, ok := parsed.Get(token.AuthViaClaimKey)
358+
s.Assert().True(ok)
359+
s.Assert().Equal(authenticate.PATClientAssertion.String(), authVia)
357360

358361
// and the minted jwt authenticates
359362
ctxWithMinted := testbench.ContextWithHeaders(context.Background(), map[string]string{

‎test/e2e/regression/serviceusers_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,9 @@ func (s *ServiceUsersRegressionTestSuite) TestServiceUserWithSecret() {
666666
orgIDs, ok := insecureToken.Get("org_ids")
667667
s.Assert().True(ok)
668668
s.Assert().Equal(existingOrgID, orgIDs)
669+
authVia, ok := insecureToken.Get("auth_via")
670+
s.Assert().True(ok)
671+
s.Assert().Equal("client_credentials", authVia)
669672
})
670673
}
671674

0 commit comments

Comments
 (0)