Created
July 1, 2021 06:41
-
-
Save ishantsagar/4a9098d5a809a157bf7bd8fd25eaa2a1 to your computer and use it in GitHub Desktop.
JWKs Validation in Golang
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"time" | |
"github.com/MicahParks/keyfunc" | |
"github.com/golang-jwt/jwt" | |
) | |
func main() { | |
// Get a JWT to parse. | |
jwtB64 := "OIDC_ACCESS_TOKEN" | |
//################ | |
// Get the JWKs URL from an environment variable. | |
// jwksURL := "OIDC_JWKS_URL" | |
// Create the JWKs from the resource at the given URL. | |
// jwks, err := keyfunc.Get(jwksURL) | |
// if err != nil { | |
// log.Fatalf("Failed to get the JWKs from the given URL.\nError:%s\n", err.Error()) | |
// } | |
//################ | |
//******** | |
// Get the JWKS as JSON. | |
var jwksJSON json.RawMessage = []byte("JWKS_URI_KEYS_JSON") | |
// //Create the JWKS from the resource at the given URL. | |
jwks, err := keyfunc.New(jwksJSON) | |
if err != nil { | |
log.Fatalf("Failed to create JWKS from resource at the given URL.\nError: %s", err.Error()) | |
} | |
//******** | |
// Parse the JWT. | |
token, err := jwt.Parse(jwtB64, jwks.KeyFunc) | |
if err != nil { | |
log.Fatalf("Failed to parse the JWT.\nError: %s", err.Error()) | |
} | |
// Check if the token is valid. | |
if !token.Valid { | |
log.Fatalf("The token is not valid.") | |
} | |
log.Println("The token is valid.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment