Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for validating HS512 JWT tokens #282

Merged
merged 4 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var Config = struct {
If you wish to use JWT Auth via headers you can simply set the header `Authorization Bearer [access_token]`

Supported signing methods:
* HS256, in this case `FLAGR_JWT_AUTH_SECRET` contains the passphrase
* HS256/HS512, in this case `FLAGR_JWT_AUTH_SECRET` contains the passphrase
* RS256, in this case `FLAGR_JWT_AUTH_SECRET` contains the key in PEM Format

Note:
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func setupJWTAuthMiddleware() *auth {
case "HS256":
signingMethod = jwt.SigningMethodHS256
validationKey = []byte(Config.JWTAuthSecret)
case "HS512":
signingMethod = jwt.SigningMethodHS512
validationKey = []byte(Config.JWTAuthSecret)
case "RS256":
signingMethod = jwt.SigningMethodRS256
validationKey, errParsingKey = jwt.ParseRSAPublicKeyFromPEM([]byte(Config.JWTAuthSecret))
Expand Down
25 changes: 25 additions & 0 deletions pkg/config/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (

// Signed with secret: "mysecret"
validHS256JWTTokenWithSecret = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.drt_po6bHhDOF_FJEHTrK-KD8OGjseJZpHwHIgsnoTM"

// Signed with secret: "mysecret"
validHS512JWTTokenWithSecret = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.G4VTPaWRHtByF6SaHSQFTeu-896jFb2dF2KnYjJTa9MY_a6Tbb9BsO7Uu0Ju_QOGGDI_b-k6U0T6qwj9lA5_Aw"
)

func (o *okHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -228,6 +231,28 @@ o2kQ+X5xK9cipRgEKwIDAQAB
hh.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
})

t.Run("it will pass if jwt enabled with correct header token encrypted using HS512", func(t *testing.T) {
Config.JWTAuthEnabled = true
Config.JWTAuthSecret = "mysecret"
Config.JWTAuthSigningMethod = "HS512"
defer func() {
Config.JWTAuthEnabled = false
Config.JWTAuthSecret = ""
Config.JWTAuthSigningMethod = ""
}()
hh := SetupGlobalMiddleware(h)

res := httptest.NewRecorder()
res.Body = new(bytes.Buffer)
req, _ := http.NewRequest("GET", "http://localhost:18000/api/v1/flags", nil)
req.AddCookie(&http.Cookie{
Name: "access_token",
Value: validHS512JWTTokenWithSecret,
})
hh.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
})
}

func TestAuthMiddlewareWithUnauthorized(t *testing.T) {
Expand Down