Created
April 15, 2020 12:35
-
-
Save agzuniverse/995ebb4724330ac3a13878787bb4737b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
else { | |
jwtToken := authHeader[1] | |
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) { | |
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | |
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) | |
} | |
return []byte(SECRETKEY), nil | |
}) | |
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { | |
ctx := context.WithValue(r.Context(), "props", claims) | |
// Access context values in handlers like this | |
// props, _ := r.Context().Value("props").(jwt.MapClaims) | |
next.ServeHTTP(w, r.WithContext(ctx)) | |
} else { | |
fmt.Println(err) | |
w.WriteHeader(http.StatusUnauthorized) | |
w.Write([]byte("Unauthorized")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment