AuthFromAuthorizationHeader is a middleware that extracts the token from the authorization header and verifies it
(keyFunc jwt.Keyfunc, claimsFunc ClaimsFunc, signingMethod jwt.SigningMethod, next nhttp.Handler)
| 67 | |
| 68 | // AuthFromAuthorizationHeader is a middleware that extracts the token from the authorization header and verifies it |
| 69 | func AuthFromAuthorizationHeader(keyFunc jwt.Keyfunc, claimsFunc ClaimsFunc, signingMethod jwt.SigningMethod, next nhttp.Handler) nhttp.Handler { |
| 70 | return nhttp.HandlerFunc(func(w http.ResponseWriter, r *nhttp.Request) { |
| 71 | auths := strings.SplitN(r.Header.Get(authorizationKey), " ", 2) |
| 72 | if len(auths) != 2 || !strings.EqualFold(auths[0], bearerWord) { |
| 73 | nhttp.Error(w, "JWT token is missing", nhttp.StatusUnauthorized) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | jwtToken := auths[1] |
| 78 | |
| 79 | verifyJWTAndServeNext(w, r, jwtToken, keyFunc, claimsFunc, signingMethod, next) |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | // verifyAndMarshalJWT verifies the token and returns the map claims |
| 84 | func verifyAndMarshalJWT(token string, keyFunc jwt.Keyfunc, claimsFunc ClaimsFunc, signingMethod jwt.SigningMethod) (*jwt.Claims, error) { |