-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (32 loc) · 972 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"log"
"github.com/dgrijalva/jwt-go"
"github.com/MicahParks/compatibility-keyfunc"
)
func main() {
// Declare the custom signing method's key and key ID.
key := []byte("example secret")
const exampleKID = "exampleKeyID"
// Create and sign the token using the HMAC key.
unsignedToken := jwt.New(jwt.SigningMethodHS512)
unsignedToken.Header["kid"] = exampleKID
jwtB64, err := unsignedToken.SignedString(key)
if err != nil {
log.Fatalf("Failed to self sign an HMAC token.\nError: %s.", err.Error())
}
// Create the JWKS from the HMAC key.
jwks := keyfunc.NewGiven(map[string]keyfunc.GivenKey{
exampleKID: keyfunc.NewGivenHMAC(key),
})
// Parse the token.
token, err := jwt.Parse(jwtB64, jwks.KeyfuncLegacy)
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.")
}