Skip to content

Instantly share code, notes, and snippets.

@calam1
Created December 14, 2022 21:33
Show Gist options
  • Save calam1/39ab77fe81024230d5898df38683d4f1 to your computer and use it in GitHub Desktop.
Save calam1/39ab77fe81024230d5898df38683d4f1 to your computer and use it in GitHub Desktop.
A quick and dirty JWT/JWKS endpoint validator
package main
import (
"log"
"time"
// remember go get these libs
"github.com/dgrijalva/jwt-go"
"github.com/MicahParks/keyfunc"
)
func main() {
// Get the JWKS URL.
jwksURL := "https://sblpf.test.com/ext/oauth/jwks"
// Create the keyfunc options. Refresh the JWKS every hour and log errors.
refreshInterval := time.Hour
options := keyfunc.Options{
RefreshInterval: &refreshInterval,
RefreshErrorHandler: func(err error) {
log.Printf("There was an error with the jwt.KeyFunc\nError: %s", err.Error())
},
}
// Create the JWKS from the resource at the given URL.
jwks, err := keyfunc.Get(jwksURL, options)
if err != nil {
log.Fatalf("Failed to create JWKS from resource at the given URL.\nError: %s", err.Error())
}
// Get a JWT to parse.
jwtB64 := "eyJhbGciOiJSUzI1NiIsImtpZCI6ImsxIiwicGkuYXRtIjoiNTdpdiJ9.eyJzY29wZSI6W10sImNsaWVudF9pZF9uYW1lIjoic3BvcmtfYmZmX3NhbmRib3giLCJpc3MiOiJwcm9kdWN0Y29yZSIsImZvbyI6ImJhciIsImV4cCI6MTYyNDQ2NzEyMn0.TFa1x1PGZGRbo9ZAH4iGCQIX1e5QRAj-h503Kvm4WHYfEt2HG7hfmIoLzTieKOocO-Oo9QDQPOxfhtDdcHo6Wruk9XOZeAYxH70_V4u4kkG5SWKy-Mz_G3o1HMp_YPFgeZV8nRx8UCQmZD-CSWg4_Bzo5Xrbc82K4m8lKHwxkvYDYkusudfz1Pwz-iT6rAmfJ5-6qJMuP5QyeDaA3ShAuFXKG9vEHcMgkLZXL_oUYNU6qZ9G1wVeO2c51ObP350dir-V1cuAngUR4jWJGPvJKjB9NQZ4xEEycvTOmwHj_mFgeY2w4HXVuzg62o2xPlP8a-Olxg-1S0YXjinoZJzW3w"
// 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