mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-11 14:53:15 -05:00
Merge pull request #6 from sogko/sogko/master
Fixed panic error when `token.Method != ja.signer`
This commit is contained in:
commit
cf1ac5a102
2 changed files with 43 additions and 1 deletions
|
|
@ -106,7 +106,7 @@ func (ja *JwtAuth) Verify(paramAliases ...string) func(chi.Handler) chi.Handler
|
||||||
|
|
||||||
// Verify the token
|
// Verify the token
|
||||||
token, err := ja.Decode(tokenStr)
|
token, err := ja.Decode(tokenStr)
|
||||||
if err != nil || !token.Valid || token.Method != ja.signer {
|
if err != nil {
|
||||||
switch err.Error() {
|
switch err.Error() {
|
||||||
case "token is expired":
|
case "token is expired":
|
||||||
err = ErrExpired
|
err = ErrExpired
|
||||||
|
|
@ -117,6 +117,13 @@ func (ja *JwtAuth) Verify(paramAliases ...string) func(chi.Handler) chi.Handler
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if token == nil || !token.Valid || token.Method != ja.signer {
|
||||||
|
err = ErrUnauthorized
|
||||||
|
ctx = ja.SetContext(ctx, token, err)
|
||||||
|
next.ServeHTTPC(ctx, w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Check expiry via "exp" claim
|
// Check expiry via "exp" claim
|
||||||
if ja.IsExpired(token) {
|
if ja.IsExpired(token) {
|
||||||
err = ErrExpired
|
err = ErrExpired
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,16 @@ func TestSimple(t *testing.T) {
|
||||||
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 && resp != "Unauthorized\n" {
|
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 && resp != "Unauthorized\n" {
|
||||||
t.Fatalf(resp)
|
t.Fatalf(resp)
|
||||||
}
|
}
|
||||||
|
// wrong token secret and wrong alg
|
||||||
|
h.Set("Authorization", "BEARER "+newJwt512Token([]byte("wrong"), map[string]interface{}{}))
|
||||||
|
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 && resp != "Unauthorized\n" {
|
||||||
|
t.Fatalf(resp)
|
||||||
|
}
|
||||||
|
// correct token secret but wrong alg
|
||||||
|
h.Set("Authorization", "BEARER "+newJwt512Token(TokenSecret, map[string]interface{}{}))
|
||||||
|
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 && resp != "Unauthorized\n" {
|
||||||
|
t.Fatalf(resp)
|
||||||
|
}
|
||||||
|
|
||||||
// sending authorized requests
|
// sending authorized requests
|
||||||
if status, resp := testRequest(t, ts, "GET", "/", newAuthHeader(), nil); status != 200 && resp != "welcome" {
|
if status, resp := testRequest(t, ts, "GET", "/", newAuthHeader(), nil); status != 200 && resp != "welcome" {
|
||||||
|
|
@ -127,6 +137,16 @@ func TestMore(t *testing.T) {
|
||||||
if status, resp := testRequest(t, ts, "GET", "/admin", h, nil); status != 401 && resp != "Unauthorized\n" {
|
if status, resp := testRequest(t, ts, "GET", "/admin", h, nil); status != 401 && resp != "Unauthorized\n" {
|
||||||
t.Fatalf(resp)
|
t.Fatalf(resp)
|
||||||
}
|
}
|
||||||
|
// wrong token secret and wrong alg
|
||||||
|
h.Set("Authorization", "BEARER "+newJwt512Token([]byte("wrong"), map[string]interface{}{}))
|
||||||
|
if status, resp := testRequest(t, ts, "GET", "/admin", h, nil); status != 401 && resp != "Unauthorized\n" {
|
||||||
|
t.Fatalf(resp)
|
||||||
|
}
|
||||||
|
// correct token secret but wrong alg
|
||||||
|
h.Set("Authorization", "BEARER "+newJwt512Token(TokenSecret, map[string]interface{}{}))
|
||||||
|
if status, resp := testRequest(t, ts, "GET", "/admin", h, nil); status != 401 && resp != "Unauthorized\n" {
|
||||||
|
t.Fatalf(resp)
|
||||||
|
}
|
||||||
|
|
||||||
h = newAuthHeader((jwtauth.Claims{}).Set("exp", jwtauth.EpochNow()-1000))
|
h = newAuthHeader((jwtauth.Claims{}).Set("exp", jwtauth.EpochNow()-1000))
|
||||||
if status, resp := testRequest(t, ts, "GET", "/admin", h, nil); status != 401 && resp != "expired\n" {
|
if status, resp := testRequest(t, ts, "GET", "/admin", h, nil); status != 401 && resp != "expired\n" {
|
||||||
|
|
@ -191,6 +211,21 @@ func newJwtToken(secret []byte, claims ...jwtauth.Claims) string {
|
||||||
return tokenStr
|
return tokenStr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newJwt512Token(secret []byte, claims ...jwtauth.Claims) string {
|
||||||
|
// use-case: when token is signed with a different alg than expected
|
||||||
|
token := jwt.New(jwt.GetSigningMethod("HS512"))
|
||||||
|
if len(claims) > 0 {
|
||||||
|
for k, v := range claims[0] {
|
||||||
|
token.Claims[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokenStr, err := token.SignedString(secret)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return tokenStr
|
||||||
|
}
|
||||||
|
|
||||||
func newAuthHeader(claims ...jwtauth.Claims) http.Header {
|
func newAuthHeader(claims ...jwtauth.Claims) http.Header {
|
||||||
h := http.Header{}
|
h := http.Header{}
|
||||||
h.Set("Authorization", "BEARER "+newJwtToken(TokenSecret, claims...))
|
h.Set("Authorization", "BEARER "+newJwtToken(TokenSecret, claims...))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue