mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
fix panic on auth
This commit is contained in:
parent
e56ebf1f72
commit
53973bc0f7
@ -2,6 +2,7 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
|
||||||
@ -19,6 +20,12 @@ const (
|
|||||||
jwtIssuer = "iss"
|
jwtIssuer = "iss"
|
||||||
jwtNotBefore = "nbf"
|
jwtNotBefore = "nbf"
|
||||||
jwtSubject = "sub"
|
jwtSubject = "sub"
|
||||||
|
noDetailReason = "no detail reason"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errInvalidToken = errors.New("invalid auth token")
|
||||||
|
errNoClaims = errors.New("no auth params")
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -47,13 +54,13 @@ func Authorize(secret string, opts ...AuthorizeOption) func(http.Handler) http.H
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !token.Valid {
|
if !token.Valid {
|
||||||
unauthorized(w, r, err, authOpts.Callback)
|
unauthorized(w, r, errInvalidToken, authOpts.Callback)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, ok := token.Claims.(jwt.MapClaims)
|
claims, ok := token.Claims.(jwt.MapClaims)
|
||||||
if !ok {
|
if !ok {
|
||||||
unauthorized(w, r, err, authOpts.Callback)
|
unauthorized(w, r, errNoClaims, authOpts.Callback)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,10 +100,15 @@ func detailAuthLog(r *http.Request, reason string) {
|
|||||||
func unauthorized(w http.ResponseWriter, r *http.Request, err error, callback UnauthorizedCallback) {
|
func unauthorized(w http.ResponseWriter, r *http.Request, err error, callback UnauthorizedCallback) {
|
||||||
writer := newGuardedResponseWriter(w)
|
writer := newGuardedResponseWriter(w)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
detailAuthLog(r, err.Error())
|
detailAuthLog(r, err.Error())
|
||||||
|
} else {
|
||||||
|
detailAuthLog(r, noDetailReason)
|
||||||
|
}
|
||||||
if callback != nil {
|
if callback != nil {
|
||||||
callback(writer, r, err)
|
callback(writer, r, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.WriteHeader(http.StatusUnauthorized)
|
writer.WriteHeader(http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +75,14 @@ func TestAuthHandlerWithPrevSecret(t *testing.T) {
|
|||||||
assert.Equal(t, "content", resp.Body.String())
|
assert.Equal(t, "content", resp.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAuthHandler_NilError(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
unauthorized(resp, req, nil, nil)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func buildToken(secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
|
func buildToken(secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
claims := make(jwt.MapClaims)
|
claims := make(jwt.MapClaims)
|
||||||
|
Loading…
Reference in New Issue
Block a user