forked from PEngG7/jwt-go-ecdsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
46 lines (39 loc) · 1.14 KB
/
test.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
41
42
43
44
45
46
package main
import (
"fmt"
"github.com/golang-jwt/jwt/v5"
"io/ioutil"
"log"
)
func main() {
tokenString, err := GenerateToken("policy.json", "trackingService-maximal", "purpose1", "key.pem", 2)
if err != nil {
log.Println(err)
}
// Read the ECDSA public key from file
keyData, err := ioutil.ReadFile("public.pem") // Replace with the path to your ECDSA public key file
if err != nil {
log.Fatalf("Error reading public key: %v", err)
}
// Parse the ECDSA public key
publicKey, err := jwt.ParseECPublicKeyFromPEM(keyData)
if err != nil {
log.Fatalf("Error parsing public key: %v", err)
}
// Verify the token signature using the ECDSA-256 public key
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return publicKey, nil
})
if err != nil {
log.Fatalf("Error verifying token: %v", err)
}
if token.Valid {
fmt.Println("Token signature is valid")
} else {
fmt.Println("Token signature is invalid")
}
fmt.Println("Here is your token: ", tokenString)
}