-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
45 lines (38 loc) · 873 Bytes
/
example.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
package example
import (
"fmt"
"log"
"time"
j "github.com/dgrijalva/jwt-go"
"github.com/mohammadyaseen2/TokenUtils/jwt"
)
func ExampleMain() {
payload := `{
"username":"adam",
"roles":["ADMIN"],
"extra":{
"age":15,
"salary":1500.0
}
}`
// 1. Generate keys (private, public)
keyPair, err := jwt.MakeKeyPair(2048)
if err != nil {
log.Fatalln(err)
}
// 2. Initialize JWT.
JWT := jwt.New(keyPair.PrivateKey, keyPair.PublicKey, *j.SigningMethodRS256)
// 3. Generate a JWT Token with payload and expiry time.
token, err := JWT.GetToken(payload, time.Second)
if err != nil {
log.Fatalln(err)
}
fmt.Println("TOKEN:", token)
// 4. Validate an existing JWT token.
validator := jwt.NewValidator(keyPair.PublicKey)
content, err := validator.ValidateToken(token)
if err != nil {
log.Fatalln(err)
}
fmt.Println("CONTENT:", content)
}