-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(cherry picked from commit f4c857b)
- Loading branch information
1 parent
8691809
commit 7a93c47
Showing
5 changed files
with
143 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -264,7 +264,7 @@ func TestInvalidAuthInfo(t *testing.T) { | |
authSchema, err := testutil.AppendJWKAndVerificationKey(sch) | ||
require.NoError(t, err) | ||
_, err = schema.NewHandler(string(authSchema), false) | ||
require.Error(t, err, fmt.Errorf("Expecting either JWKUrl or (VerificationKey, Algo), both were given")) | ||
require.Error(t, err, fmt.Errorf("Expecting either JWKUrl/JWKUrls or (VerificationKey, Algo), both were given")) | ||
} | ||
|
||
func TestMissingAudienceWithJWKUrl(t *testing.T) { | ||
|
@@ -276,7 +276,6 @@ func TestMissingAudienceWithJWKUrl(t *testing.T) { | |
require.Error(t, err, fmt.Errorf("required field missing in Dgraph.Authorization: `Audience`")) | ||
} | ||
|
||
//Todo(Minhaj): Add a testcase for token without Expiry | ||
func TestVerificationWithJWKUrl(t *testing.T) { | ||
sch, err := ioutil.ReadFile("../e2e/auth/schema.graphql") | ||
require.NoError(t, err, "Unable to read schema file") | ||
|
@@ -293,21 +292,73 @@ func TestVerificationWithJWKUrl(t *testing.T) { | |
require.Equal(t, metainfo.Header, "X-Test-Auth") | ||
require.Equal(t, metainfo.Namespace, "https://xyz.io/jwt/claims") | ||
require.Equal(t, metainfo.VerificationKey, "") | ||
require.Equal(t, metainfo.JWKUrl, "https://www.googleapis.com/service_accounts/v1/jwk/[email protected]") | ||
require.Equal(t, metainfo.JWKUrl, "") | ||
require.Equal(t, metainfo.JWKUrls, []string{"https://dev-hr2kugfp.us.auth0.com/.well-known/jwks.json"}) | ||
|
||
testCase := struct { | ||
name string | ||
token string | ||
}{ | ||
name: `Expired Token`, | ||
token: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE2NzUwM2UwYWVjNTJkZGZiODk2NTIxYjkxN2ZiOGUyMGMxZjMzMDAiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vZmlyLXByb2plY3QxLTI1OWU3IiwiYXVkIjoiZmlyLXByb2plY3QxLTI1OWU3IiwiYXV0aF90aW1lIjoxNjAxNDQ0NjM0LCJ1c2VyX2lkIjoiMTdHb3h2dU5CWlc5YTlKU3Z3WXhROFc0bjE2MyIsInN1YiI6IjE3R294dnVOQlpXOWE5SlN2d1l4UThXNG4xNjMiLCJpYXQiOjE2MDE0NDQ2MzQsImV4cCI6MTYwMTQ0ODIzNCwiZW1haWwiOiJtaW5oYWpAZGdyYXBoLmlvIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJmaXJlYmFzZSI6eyJpZGVudGl0aWVzIjp7ImVtYWlsIjpbIm1pbmhhakBkZ3JhcGguaW8iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.q5YmOzOUkZHNjlz53hgLNSVg-brIU9tLJ4jLC0_Xurl5wEbyZ6D_KQ9-UFqbl2HR6R1V5kpaf6eDFR3c83i1PpCbJ4LTjHAf_njQvL75ByERld23lZtKZyEeE6ujdFXL8ne4fI2qenD1Xeqx9AnXbLf7U_CvZpbX3l1wj7p0Lpn7qixi0AztuLSJMLkMfFpaiwyFZQivi4cqtnI25VIsK6a4KIpl1Sk0AHT-lv9PRadd_JDjWAIzD0SfhpZOskaeA9PljVMp-Y3Xscwg_Qc6u1MIBPg1jKO-ngjhWkgEWBoz5F836P7phT60LVBHhYuk-jRN6HSSNWQ3ineuN-jBk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MSeKvvpvtVlPaeM1NI4S0R3g0qUGvX6L6qsLZqIilSQUiUaOEo8bLNBFHOxhBbocF-R-x40nSYjdjrEz60A99mz5XAA", | ||
} | ||
|
||
md := metadata.New(map[string]string{"authorizationJwt": testCase.token}) | ||
ctx := metadata.NewIncomingContext(context.Background(), md) | ||
|
||
_, err = metainfo.ExtractCustomClaims(ctx) | ||
require.True(t, strings.Contains(err.Error(), "unable to parse jwt token:token is unverifiable: Keyfunc returned an error")) | ||
require.Nil(t, err) | ||
} | ||
|
||
func TestVerificationWithMultipleJWKUrls(t *testing.T) { | ||
sch, err := ioutil.ReadFile("../e2e/auth/schema.graphql") | ||
require.NoError(t, err, "Unable to read schema file") | ||
|
||
authSchema, err := testutil.AppendAuthInfoWithMultipleJWKUrls(sch) | ||
require.NoError(t, err) | ||
|
||
schema := test.LoadSchemaFromString(t, string(authSchema)) | ||
require.NotNil(t, schema.Meta().AuthMeta()) | ||
|
||
// Verify that authorization information is set correctly. | ||
metainfo := schema.Meta().AuthMeta() | ||
require.Equal(t, metainfo.Algo, "") | ||
require.Equal(t, metainfo.Header, "X-Test-Auth") | ||
require.Equal(t, metainfo.Namespace, "https://xyz.io/jwt/claims") | ||
require.Equal(t, metainfo.VerificationKey, "") | ||
require.Equal(t, metainfo.JWKUrl, "") | ||
require.Equal(t, metainfo.JWKUrls, []string{"https://www.googleapis.com/service_accounts/v1/jwk/[email protected]", "https://dev-hr2kugfp.us.auth0.com/.well-known/jwks.json"}) | ||
|
||
testCases := []struct { | ||
name string | ||
token string | ||
invalid bool | ||
}{ | ||
{ | ||
name: `Expired Token`, | ||
token: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE2NzUwM2UwYWVjNTJkZGZiODk2NTIxYjkxN2ZiOGUyMGMxZjMzMDAiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vZmlyLXByb2plY3QxLTI1OWU3IiwiYXVkIjoiZmlyLXByb2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WUNnc2RlR3lFMHM1d01Gdmd2X1htZDA4Q3B3PXM5Ni1jIiwibG9jYWxlIjoiZW4iLCJ1cGRhdGVkX2F0IjoiMjAyMS0wMy0wOVQxMDowOTozNi4yMDNaIiwiZW1haWwiOiJtc3JpaXRkQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJpc3MiOiJodHRwczovL2Rldi1ocjJrdWdmcC51cy5hdXRoMC5jb20vIiwic3ViIjoiZ29vZ2xlLW9hdXRoMnwxMDM2NTgyNjIxNzU2NDczNzEwNjQiLCJhdWQiOiJIaGFYa1FWUkJuNWUwSzNEbU1wMnpiakk4aTF3Y3YyZSIsImlhdCI6MTYxNTI4NDU3NywiZXhwIjo1MjE1Mjg0NTc3LCJub25jZSI6IlVtUk9NbTV0WWtoR2NGVjVOWGRhVGtKV1UyWm5ZM0pKUzNSR1ZsWk1jRzFLZUVkMGQzWkdkVTFuYXc9PSJ9.rlVl0tGOCypIts0C52g1qyiNaFV3UnDafJETXTGbt-toWvtCyZsa-JySgwG0DD1rMYm-gdwyJcjJlgwVPQD3ZlkJqbFFNvY4cX5injiOljpVFOHKXdi7tehY9We_vv1KYYpvhGMsE4u7o8tz2wEctdLTXT7omEq7gSdHuDgpM-h-K2RLApU8oyu8YOIqQlrqGgJ7Q8jy-jxMlU7BoZVz38FokjmkSapAAVORsbdEqPgQjeDnjaDQ5bRhxZUMSeKvvpvtVlPaeM1NI4S0R3g0qUGvX6L6qsLZqIilSQUiUaOEo8bLNBFHOxhBbocF-R-x40nSYjdjrEz60A99mz5XAA", | ||
invalid: false, | ||
}, | ||
} | ||
|
||
for _, tcase := range testCases { | ||
t.Run(tcase.name, func(t *testing.T) { | ||
md := metadata.New(map[string]string{"authorizationJwt": tcase.token}) | ||
ctx := metadata.NewIncomingContext(context.Background(), md) | ||
|
||
_, err := metainfo.ExtractCustomClaims(ctx) | ||
if tcase.invalid { | ||
require.True(t, strings.Contains(err.Error(), "unable to parse jwt token:token is unverifiable: Keyfunc returned an error")) | ||
} else { | ||
require.Nil(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TODO(arijit): Generate the JWT token instead of using pre generated token. | ||
|
This file contains 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 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 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 |
---|---|---|
|
@@ -249,7 +249,12 @@ func AppendAuthInfo(schema []byte, algo, publicKeyFile string, closedByDefault b | |
} | ||
|
||
func AppendAuthInfoWithJWKUrl(schema []byte) ([]byte, error) { | ||
authInfo := `# Dgraph.Authorization {"VerificationKey":"","Header":"X-Test-Auth","jwkurl":"https://www.googleapis.com/service_accounts/v1/jwk/[email protected]", "Namespace":"https://xyz.io/jwt/claims","Algo":"","Audience":["fir-project1-259e7"]}` | ||
authInfo := `# Dgraph.Authorization {"VerificationKey":"","Header":"X-Test-Auth","jwkurl":"https://dev-hr2kugfp.us.auth0.com/.well-known/jwks.json", "Namespace":"https://xyz.io/jwt/claims","Algo":"","Audience":[ "HhaXkQVRBn5e0K3DmMp2zbjI8i1wcv2e"]}` | ||
return append(schema, []byte(authInfo)...), nil | ||
} | ||
|
||
func AppendAuthInfoWithMultipleJWKUrls(schema []byte) ([]byte, error) { | ||
authInfo := `# Dgraph.Authorization {"VerificationKey":"","Header":"X-Test-Auth","jwkurls":["https://www.googleapis.com/service_accounts/v1/jwk/[email protected]","https://dev-hr2kugfp.us.auth0.com/.well-known/jwks.json"], "Namespace":"https://xyz.io/jwt/claims","Algo":"","Audience":["fir-project1-259e7", "HhaXkQVRBn5e0K3DmMp2zbjI8i1wcv2e"]}` | ||
return append(schema, []byte(authInfo)...), nil | ||
} | ||
|
||
|