MCPcopy
hub / github.com/dgraph-io/dgraph / TestStringCustomClaim

Function TestStringCustomClaim

graphql/resolve/auth_test.go:162–196  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

160}
161
162func TestStringCustomClaim(t *testing.T) {
163 sch, err := os.ReadFile("../e2e/auth/schema.graphql")
164 require.NoError(t, err, "Unable to read schema file")
165
166 authSchema, err := testutil.AppendAuthInfo(sch, jwt.SigningMethodHS256.Name, "", false)
167 require.NoError(t, err)
168
169 schema := test.LoadSchemaFromString(t, string(authSchema))
170 require.NotNil(t, schema.Meta().AuthMeta())
171
172 // Token with custom claim:
173 // "https://xyz.io/jwt/claims": {
174 // "USERNAME": "Random User",
175 // "email": "random@dgraph.io"
176 // }
177 //
178 // It also contains standard claim : "email": "test@dgraph.io", but the
179 // value of "email" gets overwritten by the value present inside custom claim.
180 token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjM1MTYyMzkwMjIsImVtYWlsIjoidGVzdEBkZ3JhcGguaW8iLCJodHRwczovL3h5ei5pby9qd3QvY2xhaW1zIjp7IlVTRVJOQU1FIjoiUmFuZG9tIFVzZXIiLCJlbWFpbCI6InJhbmRvbUBkZ3JhcGguaW8ifX0.MgF1CfJI4RHDTQjLz6PtowlTB0Tlq5NmID5T4YmQ5mI"
181 md := metadata.New(map[string]string{"authorizationJwt": token})
182 ctx := metadata.NewIncomingContext(context.Background(), md)
183
184 customClaims, err := schema.Meta().AuthMeta().ExtractCustomClaims(ctx)
185 require.NoError(t, err)
186 authVar := customClaims.AuthVariables
187 result := map[string]interface{}{
188 "sub": "1234567890",
189 "name": "John Doe",
190 "USERNAME": "Random User",
191 "email": "random@dgraph.io",
192 }
193 delete(authVar, "exp")
194 delete(authVar, "iat")
195 require.Equal(t, authVar, result)
196}
197
198func TestAudienceClaim(t *testing.T) {
199 sch, err := os.ReadFile("../e2e/auth/schema.graphql")

Callers

nothing calls this directly

Calls 5

AppendAuthInfoFunction · 0.92
LoadSchemaFromStringFunction · 0.92
AuthMetaMethod · 0.80
ExtractCustomClaimsMethod · 0.80
MetaMethod · 0.65

Tested by

no test coverage detected