-
Notifications
You must be signed in to change notification settings - Fork 186
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
Add JWT auth via headers and RS256 signing option #146
Merged
zhouzhuojie
merged 2 commits into
openflagr:master
from
vayan:add-header-jwt-and-rs256-token
Jul 23, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -15,6 +15,23 @@ import ( | |
|
||
type okHandler struct{} | ||
|
||
const ( | ||
// Signed with secret: "" | ||
validHS256JWTToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmbGFncl91c2VyIjoiMTIzNDU2Nzg5MCJ9.CLXgNEtwPCqCOtUU-KmqDyO8S2wC_G6PZ0tml8DCuNw" | ||
|
||
// Public Key: | ||
//-----BEGIN PUBLIC KEY----- | ||
//MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd | ||
//UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs | ||
//HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D | ||
//o2kQ+X5xK9cipRgEKwIDAQAB | ||
//-----END PUBLIC KEY----- | ||
validRS256JWTToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.TCYt5XsITJX1CxPCT8yAV-TVkIEq_PbChOMqsLfRoPsnsgw5WEuts01mq-pQy7UJiN5mgRxD-WUcX16dUEMGlv50aqzpqh4Qktb3rk-BuQy72IFLOqV0G_zS245-kronKb78cPN25DGlcTwLtjPAYuNzVBAh4vGHSrQyHUdBBPM" | ||
|
||
// Signed with secret: "mysecret" | ||
validHS256JWTTokenWithSecret = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.drt_po6bHhDOF_FJEHTrK-KD8OGjseJZpHwHIgsnoTM" | ||
) | ||
|
||
func (o *okHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
w.Write([]byte("OK")) | ||
} | ||
|
@@ -83,7 +100,7 @@ func TestAuthMiddleware(t *testing.T) { | |
req, _ := http.NewRequest("GET", "http://localhost:18000/api/v1/flags", nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: "access_token", | ||
Value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmbGFncl91c2VyIjoiMTIzNDU2Nzg5MCJ9.CLXgNEtwPCqCOtUU-KmqDyO8S2wC_G6PZ0tml8DCuNw", // {"flagr_user": "1234567890"} | ||
Value: validHS256JWTToken, | ||
}) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
|
@@ -100,6 +117,120 @@ func TestAuthMiddleware(t *testing.T) { | |
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
}) | ||
|
||
t.Run("it will pass if jwt enabled with correct header token", func(t *testing.T) { | ||
Config.JWTAuthEnabled = true | ||
defer func() { Config.JWTAuthEnabled = false }() | ||
hh := SetupGlobalMiddleware(h) | ||
|
||
res := httptest.NewRecorder() | ||
res.Body = new(bytes.Buffer) | ||
req, _ := http.NewRequest("GET", "http://localhost:18000/api/v1/flags", nil) | ||
req.Header.Add("Authorization", "Bearer "+validHS256JWTToken) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
}) | ||
|
||
t.Run("it will redirect if jwt enabled with invalid cookie token and valid header token", func(t *testing.T) { | ||
Config.JWTAuthEnabled = true | ||
defer func() { Config.JWTAuthEnabled = false }() | ||
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: "invalid_jwt", | ||
}) | ||
req.Header.Add("Authorization", "Bearer "+validHS256JWTToken) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusTemporaryRedirect, res.Code) | ||
}) | ||
|
||
t.Run("it will redirect if jwt enabled and a cookie token encrypted with the wrong method", func(t *testing.T) { | ||
Config.JWTAuthEnabled = true | ||
Config.JWTAuthSigningMethod = "RS256" | ||
defer func() { | ||
Config.JWTAuthEnabled = false | ||
Config.JWTAuthSigningMethod = "HS256" | ||
}() | ||
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: "invalid_jwt", | ||
}) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusTemporaryRedirect, res.Code) | ||
}) | ||
|
||
t.Run("it will pass if jwt enabled with correct header token encrypted using RS256", func(t *testing.T) { | ||
Config.JWTAuthEnabled = true | ||
Config.JWTAuthSigningMethod = "RS256" | ||
Config.JWTAuthSecret = `-----BEGIN PUBLIC KEY----- | ||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd | ||
UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs | ||
HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D | ||
o2kQ+X5xK9cipRgEKwIDAQAB | ||
-----END PUBLIC KEY-----` | ||
defer func() { | ||
Config.JWTAuthEnabled = false | ||
Config.JWTAuthSigningMethod = "HS256" | ||
Config.JWTAuthSecret = "" | ||
}() | ||
hh := SetupGlobalMiddleware(h) | ||
|
||
res := httptest.NewRecorder() | ||
res.Body = new(bytes.Buffer) | ||
req, _ := http.NewRequest("GET", "http://localhost:18000/api/v1/flags", nil) | ||
req.Header.Add("Authorization", "Bearer "+validRS256JWTToken) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
}) | ||
|
||
t.Run("it will pass if jwt enabled with valid cookie token with passphrase", func(t *testing.T) { | ||
Config.JWTAuthEnabled = true | ||
Config.JWTAuthSecret = "mysecret" | ||
defer func() { | ||
Config.JWTAuthEnabled = false | ||
Config.JWTAuthSecret = "" | ||
}() | ||
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: validHS256JWTTokenWithSecret, | ||
}) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
}) | ||
|
||
t.Run("it will pass if jwt enabled with correct HS256 token cookie and signing method is wrong", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: change to "it will pass with a correct HS256 token cookie when signing method is wrong and it defaults to empty string secret" |
||
Config.JWTAuthEnabled = true | ||
Config.JWTAuthSigningMethod = "invalid" | ||
defer func() { | ||
Config.JWTAuthEnabled = false | ||
Config.JWTAuthSigningMethod = "HS256" | ||
}() | ||
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: validHS256JWTToken, | ||
}) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
}) | ||
} | ||
|
||
func TestStatsMiddleware(t *testing.T) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a comment for possible values of signing methods.
The whole file will be served as the doc at https://checkr.github.io/flagr/#/flagr_env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can you help change the comments above of the pattern of how to use JWT auth?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I missed this comment 👍