Skip to content

Commit 1662da6

Browse files
authored
Merge pull request #273 from supertokens/jwt-rework/issuer-in-payload
feat: Add issuer to access token payload [JWT Rework #2]
2 parents bd2591c + 30d8524 commit 1662da6

18 files changed

+55
-50
lines changed

examples/go.sum

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
4949
github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
5050
github.com/ClickHouse/clickhouse-go/v2 v2.2.0/go.mod h1:8f2XZUi7XoeU+uPIytSi1cvx8fmJxi7vIgqpvYTF1+o=
5151
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
52-
github.com/MicahParks/keyfunc v1.0.0 h1:O9VAkG6q/LqX4eS+HuIsW9KfC/Luh2NBQr9v4NiwHU0=
53-
github.com/MicahParks/keyfunc v1.0.0/go.mod h1:R8RZa27qn+5cHTfYLJ9/+7aSb5JIdz7cl0XFo0o4muo=
52+
github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o=
53+
github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw=
5454
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
5555
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
5656
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
@@ -198,7 +198,6 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
198198
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
199199
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
200200
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
201-
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
202201
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
203202
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
204203
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -214,6 +213,7 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
214213
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
215214
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
216215
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
216+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
217217
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
218218
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
219219
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -423,6 +423,7 @@ github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uY
423423
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
424424
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
425425
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
426+
github.com/nyaruka/phonenumbers v1.0.73 h1:bP2WN8/NUP8tQebR+WCIejFaibwYMHOaB7MQVayclUo=
426427
github.com/nyaruka/phonenumbers v1.0.73/go.mod h1:3aiS+PS3DuYwkbK3xdcmRwMiPNECZ0oENH8qUT1lY7Q=
427428
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
428429
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -542,6 +543,7 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69
542543
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
543544
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
544545
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
546+
github.com/twilio/twilio-go v0.26.0 h1:wFW4oTe3/LKt6bvByP7eio8JsjtaLHjMQKOUEzQry7U=
545547
github.com/twilio/twilio-go v0.26.0/go.mod h1:lz62Hopu4vicpQ056H5TJ0JE4AP0rS3sQ35/ejmgOwE=
546548
github.com/twitchtv/twirp v8.1.0+incompatible h1:KGXanpa9LXdVE/V5P/tA27rkKFmXRGCtSNT7zdeeVOY=
547549
github.com/twitchtv/twirp v8.1.0+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=

recipe/emailverification/api/emailverify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func EmailVerify(apiImplementation evmodels.APIInterface, options evmodels.APIOp
3939
userContext := supertokens.MakeDefaultUserContextFromAPI(options.Req)
4040
sessionRequired := false
4141
sessionContainer, err := session.GetSessionWithContext(
42-
*options.Req, options.Res,
42+
options.Req, options.Res,
4343
&sessmodels.VerifySessionOptions{
4444
SessionRequired: &sessionRequired,
4545
OverrideGlobalClaimValidators: func(globalClaimValidators []claims.SessionClaimValidator, sessionContainer sessmodels.SessionContainer, userContext supertokens.UserContext) ([]claims.SessionClaimValidator, error) {
@@ -97,7 +97,7 @@ func EmailVerify(apiImplementation evmodels.APIInterface, options evmodels.APIOp
9797

9898
userContext := supertokens.MakeDefaultUserContextFromAPI(options.Req)
9999
sessionContainer, err := session.GetSessionWithContext(
100-
*options.Req,
100+
options.Req,
101101
options.Res,
102102
&sessmodels.VerifySessionOptions{
103103
OverrideGlobalClaimValidators: func(globalClaimValidators []claims.SessionClaimValidator, sessionContainer sessmodels.SessionContainer, userContext supertokens.UserContext) ([]claims.SessionClaimValidator, error) {

recipe/emailverification/api/generateEmailVerifyToken.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func GenerateEmailVerifyToken(apiImplementation evmodels.APIInterface, options e
3333
userContext := supertokens.MakeDefaultUserContextFromAPI(options.Req)
3434

3535
sessionContainer, err := session.GetSessionWithContext(
36-
*options.Req, options.Res,
36+
options.Req, options.Res,
3737
&sessmodels.VerifySessionOptions{
3838
OverrideGlobalClaimValidators: func(globalClaimValidators []claims.SessionClaimValidator, sessionContainer sessmodels.SessionContainer, userContext supertokens.UserContext) ([]claims.SessionClaimValidator, error) {
3939
validators := []claims.SessionClaimValidator{}

recipe/jwt/createJWTFeature_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ func TestSendingZeroValidityThrowsAnError(t *testing.T) {
6060
}
6161

6262
vaildityPointer := uint64(0)
63-
_, err = CreateJWT(map[string]interface{}{}, nil, &vaildityPointer)
63+
_, err = CreateJWT(map[string]interface{}{}, &vaildityPointer, nil)
6464
assert.Contains(t, err.Error(), "validity must be greater than or equal to 0")
6565
}

recipe/jwt/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func GetJWKSWithContext(userContext supertokens.UserContext) (jwtmodels.GetJWKSR
4040
return (*instance.RecipeImpl.GetJWKS)(userContext)
4141
}
4242

43-
func CreateJWT(payload map[string]interface{}, useStaticSigningKey *bool, validitySecondsPointer *uint64) (jwtmodels.CreateJWTResponse, error) {
43+
func CreateJWT(payload map[string]interface{}, validitySecondsPointer *uint64, useStaticSigningKey *bool) (jwtmodels.CreateJWTResponse, error) {
4444
return CreateJWTWithContext(payload, validitySecondsPointer, useStaticSigningKey, &map[string]interface{}{})
4545
}
4646

recipe/jwt/override_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestOverridingFunctions(t *testing.T) {
106106
}
107107
payload := result["payload"]
108108
validity := uint64(1000)
109-
resp, err := CreateJWT(payload.(map[string]interface{}), nil, &validity)
109+
resp, err := CreateJWT(payload.(map[string]interface{}), &validity, nil)
110110
if err != nil {
111111
t.Error(err.Error())
112112
}

recipe/session/apiImplementation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func MakeAPIImplementation() sessmodels.APIInterface {
4444
session, err := RefreshSessionInRequest(options.Req, options.Res, options.Config, options.RecipeImplementation, userContext)
4545
return session, err
4646
} else {
47-
sessionContainer, err := GetSessionFromRequest(*options.Req, options.Res, options.Config, &sessmodels.VerifySessionOptions{
47+
sessionContainer, err := GetSessionFromRequest(options.Req, options.Res, options.Config, &sessmodels.VerifySessionOptions{
4848
AntiCsrfCheck: verifySessionOptions.AntiCsrfCheck,
4949
SessionRequired: verifySessionOptions.SessionRequired,
5050
CheckDatabase: verifySessionOptions.CheckDatabase,

recipe/session/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func CreateNewSessionWithContextWithoutRequestResponse(userID string, accessToke
5353
finalAccessTokenPayload = map[string]interface{}{}
5454
}
5555

56+
appInfo := instance.RecipeModule.GetAppInfo()
57+
issuer := appInfo.APIDomain.GetAsStringDangerous() + appInfo.APIBasePath.GetAsStringDangerous()
58+
59+
finalAccessTokenPayload["iss"] = issuer
60+
5661
for _, claim := range claimsAddedByOtherRecipes {
5762
finalAccessTokenPayload, err = claim.Build(userID, finalAccessTokenPayload, userContext)
5863
if err != nil {
@@ -69,7 +74,7 @@ func CreateNewSessionWithContextWithoutRequestResponse(userID string, accessToke
6974
return (*instance.RecipeImpl.CreateNewSession)(userID, accessTokenPayload, sessionDataInDatabase, &_disableAntiCSRF, userContext)
7075
}
7176

72-
func GetSessionWithContext(req http.Request, res http.ResponseWriter, options *sessmodels.VerifySessionOptions, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) {
77+
func GetSessionWithContext(req *http.Request, res http.ResponseWriter, options *sessmodels.VerifySessionOptions, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) {
7378
instance, err := getRecipeInstanceOrThrowError()
7479
if err != nil {
7580
return nil, err
@@ -100,7 +105,7 @@ func GetSessionWithContextWithoutRequestResponse(accessToken string, antiCSRFTok
100105
if err != nil {
101106
return nil, err
102107
}
103-
claimValidators, err := GetRequiredClaimValidators(*result, overrideGlobalClaimValidators, userContext)
108+
claimValidators, err := GetRequiredClaimValidators(result, overrideGlobalClaimValidators, userContext)
104109

105110
if err != nil {
106111
return nil, err
@@ -114,7 +119,7 @@ func GetSessionWithContextWithoutRequestResponse(accessToken string, antiCSRFTok
114119

115120
}
116121

117-
return *result, nil
122+
return result, nil
118123
}
119124

120125
func GetSessionInformationWithContext(sessionHandle string, userContext supertokens.UserContext) (*sessmodels.SessionInformation, error) {
@@ -372,7 +377,7 @@ func CreateNewSessionWithoutRequestResponse(userId string, accessTokenPayload ma
372377
return CreateNewSessionWithContextWithoutRequestResponse(userId, accessTokenPayload, sessionDataInDatabase, disableAntiCSRF, nil)
373378
}
374379

375-
func GetSession(req http.Request, res http.ResponseWriter, options *sessmodels.VerifySessionOptions) (sessmodels.SessionContainer, error) {
380+
func GetSession(req *http.Request, res http.ResponseWriter, options *sessmodels.VerifySessionOptions) (sessmodels.SessionContainer, error) {
376381
return GetSessionWithContext(req, res, options, &map[string]interface{}{})
377382
}
378383

@@ -412,7 +417,7 @@ func UpdateSessionDataInDatabase(sessionHandle string, newSessionData map[string
412417
return UpdateSessionDataInDatabaseWithContext(sessionHandle, newSessionData, &map[string]interface{}{})
413418
}
414419

415-
func CreateJWT(payload map[string]interface{}, useStaticSigningKey *bool, validitySecondsPointer *uint64) (jwtmodels.CreateJWTResponse, error) {
420+
func CreateJWT(payload map[string]interface{}, validitySecondsPointer *uint64, useStaticSigningKey *bool) (jwtmodels.CreateJWTResponse, error) {
416421
return CreateJWTWithContext(payload, validitySecondsPointer, useStaticSigningKey, &map[string]interface{}{})
417422
}
418423

recipe/session/middleware_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestSessionVerifyMiddleware(t *testing.T) {
152152
SessionRequired: &customSessionRequiredValue,
153153
AntiCsrfCheck: &customValForAntiCsrfCheck,
154154
}, func(rw http.ResponseWriter, r *http.Request) {
155-
GetSession(*r, rw, &sessmodels.VerifySessionOptions{
155+
GetSession(r, rw, &sessmodels.VerifySessionOptions{
156156
SessionRequired: &customSessionRequiredValue,
157157
AntiCsrfCheck: &customValForAntiCsrfCheck,
158158
})
@@ -442,7 +442,7 @@ func TestSessionVerifyMiddlewareWithAutoRefresh(t *testing.T) {
442442
SessionRequired: &customSessionRequiredValue,
443443
AntiCsrfCheck: &customValForAntiCsrfCheck,
444444
}, func(rw http.ResponseWriter, r *http.Request) {
445-
GetSession(*r, rw, &sessmodels.VerifySessionOptions{
445+
GetSession(r, rw, &sessmodels.VerifySessionOptions{
446446
SessionRequired: &customSessionRequiredValue,
447447
AntiCsrfCheck: &customValForAntiCsrfCheck,
448448
})
@@ -722,7 +722,7 @@ func TestSessionVerifyMiddlewareWithDriverConfig(t *testing.T) {
722722
SessionRequired: &customSessionRequiredValue,
723723
AntiCsrfCheck: &customValForAntiCsrfCheck,
724724
}, func(rw http.ResponseWriter, r *http.Request) {
725-
GetSession(*r, rw, &sessmodels.VerifySessionOptions{
725+
GetSession(r, rw, &sessmodels.VerifySessionOptions{
726726
SessionRequired: &customSessionRequiredValue,
727727
AntiCsrfCheck: &customValForAntiCsrfCheck,
728728
})
@@ -1015,7 +1015,7 @@ func TestSessionVerifyMiddlewareWithDriverConfigWithAutoRefresh(t *testing.T) {
10151015
SessionRequired: &customSessionRequiredValue,
10161016
AntiCsrfCheck: &customValForAntiCsrfCheck,
10171017
}, func(rw http.ResponseWriter, r *http.Request) {
1018-
GetSession(*r, rw, &sessmodels.VerifySessionOptions{
1018+
GetSession(r, rw, &sessmodels.VerifySessionOptions{
10191019
SessionRequired: &customSessionRequiredValue,
10201020
AntiCsrfCheck: &customValForAntiCsrfCheck,
10211021
})

recipe/session/recipeImplementation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func MakeRecipeImplementation(querier supertokens.Querier, config sessmodels.Typ
6969

7070
// In all cases if sIdRefreshToken token exists (so it's a legacy session) we return TRY_REFRESH_TOKEN. The refresh endpoint will clear this cookie and try to upgrade the session.
7171
// Check https://supertokens.com/docs/contribute/decisions/session/0007 for further details and a table of expected behaviours
72-
getSession := func(accessTokenString string, antiCsrfToken *string, options *sessmodels.VerifySessionOptions, userContext supertokens.UserContext) (*sessmodels.SessionContainer, error) {
72+
getSession := func(accessTokenString string, antiCsrfToken *string, options *sessmodels.VerifySessionOptions, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) {
7373
if options != nil && *options.AntiCsrfCheck != false && config.AntiCsrf != AntiCSRF_VIA_CUSTOM_HEADER {
7474
return nil, defaultErrors.New("Since the anti-csrf mode is VIA_CUSTOM_HEADER getSession can't check the CSRF token. Please either use VIA_TOKEN or set antiCsrfCheck to false")
7575
}
@@ -149,7 +149,7 @@ func MakeRecipeImplementation(querier supertokens.Querier, config sessmodels.Typ
149149
sessionContainerInput := makeSessionContainerInput(accessTokenStringForSession, session.Handle, session.UserID, payload, result, frontToken, antiCsrfToken, nil, nil, !accessTokenNil)
150150
sessionContainer := newSessionContainer(config, &sessionContainerInput)
151151

152-
return &sessionContainer, nil
152+
return sessionContainer, nil
153153
}
154154

155155
getSessionInformation := func(sessionHandle string, userContext supertokens.UserContext) (*sessmodels.SessionInformation, error) {

0 commit comments

Comments
 (0)