@@ -192,3 +192,72 @@ func TestAssertClaimsWithPayloadWithJWTAndCallRightUpdateAccessTokenPayload(t *t
192192 assert .Equal (t , "stub" , jwtPayload ["st-stub" ].(map [string ]interface {})["v" ])
193193 assert .Equal (t , "rope" , jwtPayload ["sub" ])
194194}
195+
196+ func TestMergeIntoAccessTokenPayloadForJWT (t * testing.T ) {
197+ configValue := supertokens.TypeInput {
198+ Supertokens : & supertokens.ConnectionInfo {
199+ ConnectionURI : "http://localhost:8080" ,
200+ },
201+ AppInfo : supertokens.AppInfo {
202+ AppName : "SuperTokens" ,
203+ WebsiteDomain : "supertokens.io" ,
204+ APIDomain : "api.supertokens.io" ,
205+ },
206+ RecipeList : []supertokens.Recipe {
207+ Init (& sessmodels.TypeInput {
208+ Jwt : & sessmodels.JWTInputConfig {
209+ Enable : true ,
210+ },
211+ }),
212+ },
213+ }
214+
215+ BeforeEach ()
216+ unittesting .StartUpST ("localhost" , "8080" )
217+ defer AfterEach ()
218+ err := supertokens .Init (configValue )
219+ if err != nil {
220+ t .Error (err .Error ())
221+ }
222+
223+ mux := http .NewServeMux ()
224+
225+ mux .HandleFunc ("/create" , func (rw http.ResponseWriter , r * http.Request ) {
226+ CreateNewSession (rw , "rope" , nil , map [string ]interface {}{})
227+ })
228+
229+ mux .HandleFunc ("/verifySession" , VerifySession (nil , func (rw http.ResponseWriter , r * http.Request ) {
230+ sessionContainer := GetSessionFromRequestContext (r .Context ())
231+ assert .NotNil (t , sessionContainer )
232+
233+ sessionContainer .MergeIntoAccessTokenPayload (map [string ]interface {}{
234+ "testClaim" : "newValue" ,
235+ })
236+ jwtPayloadStr := sessionContainer .GetAccessTokenPayload ()["jwt" ].(string )
237+ jwtPayload := jwt.MapClaims {}
238+
239+ _ , _ , err = (& jwt.Parser {}).ParseUnverified (jwtPayloadStr , jwtPayload )
240+ assert .NoError (t , err )
241+
242+ assert .Equal (t , "newValue" , jwtPayload ["testClaim" ])
243+ }))
244+
245+ testServer := httptest .NewServer (supertokens .Middleware (mux ))
246+ defer func () {
247+ testServer .Close ()
248+ }()
249+
250+ req , err := http .NewRequest (http .MethodGet , testServer .URL + "/create" , nil )
251+ assert .NoError (t , err )
252+ res , err := http .DefaultClient .Do (req )
253+ assert .NoError (t , err )
254+ cookieData := unittesting .ExtractInfoFromResponse (res )
255+
256+ reqV , err := http .NewRequest (http .MethodGet , testServer .URL + "/verifySession" , nil )
257+ assert .NoError (t , err )
258+ reqV .Header .Add ("Cookie" , "sAccessToken=" + cookieData ["sAccessToken" ]+ ";" + "sIdRefreshToken=" + cookieData ["sIdRefreshToken" ])
259+ reqV .Header .Add ("anti-csrf" , cookieData ["antiCsrf" ])
260+ resv , err := http .DefaultClient .Do (reqV )
261+ assert .NoError (t , err )
262+ assert .Equal (t , resv .StatusCode , 200 )
263+ }
0 commit comments