Skip to content

Commit 5da0425

Browse files
committed
e2e: implement claims validation
1 parent 54474ff commit 5da0425

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

e2e/container/nginx.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ http {
2727
local cjson = require("cjson.safe")
2828
local token, err = libjwt.validate({
2929
jwks_files = {"/usr/share/tokens/jwks1.json", "/usr/share/tokens/jwks2.json"},
30+
validate_claims = {
31+
email = {exact = "[email protected]"},
32+
},
3033
})
3134
ngx.header.content_type = "application/json"
3235
if err and err ~= "" then

e2e/e2e_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,24 @@ func TestNginxContainer(t *testing.T) {
212212
assert.Equal(http.StatusOK, statusCode)
213213
})
214214

215+
t.Run("Should deny when passes a invalid claim", func(t *testing.T) {
216+
t.Parallel()
217+
assert := assertTestify.New(t)
218+
219+
jwtRequest, err := jwks_test.CreateJWT(
220+
privateKey2,
221+
jwks_test.JWTParams{KID: "kid-654321", Email: "[email protected]"})
222+
assert.NoError(err)
223+
body, statusCode, err := request_test.Do(request_test.Params{
224+
URL: URL,
225+
HeaderKey: "Authorization",
226+
HeaderValue: fmt.Sprintf("Bearer %s", jwtRequest),
227+
})
228+
assert.NoError(err)
229+
assert.Equal(http.StatusForbidden, statusCode)
230+
assert.Equal(string(body), `{"message":"Claim 'email' must be exactly '[email protected]'"}
231+
`)
232+
233+
})
234+
215235
}

e2e/jwks/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func CreateJWT(privateKey *rsa.PrivateKey, params JWTParams) (string, error) {
4747
claims := jwt.MapClaims{
4848
"sub": "1234567890",
4949
"name": "Tsuru",
50+
"email": "[email protected]",
5051
"admin": true,
5152
"iat": time.Now().Unix(),
5253
"exp": time.Now().Add(time.Hour * 24).Unix(),

0 commit comments

Comments
 (0)