/
jwt_gen.go
102 lines (80 loc) · 2.27 KB
/
jwt_gen.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// gRPC Runtime of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2020.09.
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/dgrijalva/jwt-go"
)
var (
jwtKey = flag.String("jwt-key", "", "The JWT Signing Key")
userName = flag.String("user", "HongGilDong", "The User Name")
orgName = flag.String("org", "ETRI", "The Organization Name")
clientIP = flag.String("client-ip", "127.0.0.1", "The Client IP Address")
expire = flag.Int("expire", 3650, "The Expire Days")
)
func main() {
flag.Parse()
if *jwtKey == "" {
log.Fatalf("jwt key required..")
}
tokenStr, err := generateJWT()
if err != nil {
log.Fatal(err)
}
fmt.Printf("token : %s\n", tokenStr)
fmt.Printf("----------------------------\n")
extractJWT(tokenStr)
}
func generateJWT() (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["userName"] = *userName
claims["orgName"] = *orgName
claims["clientIP"] = *clientIP
claims["expire"] = time.Now().AddDate(0, 0, *expire).Unix()
tokenString, err := token.SignedString([]byte(*jwtKey))
if err != nil {
return "", fmt.Errorf("JWT Generation Error : %s", err.Error())
}
return tokenString, nil
}
func extractJWT(tokenStr string) {
claims := jwt.MapClaims{}
token, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(*jwtKey), nil
})
if err != nil {
log.Fatal(err)
}
if token.Valid {
for key, val := range claims {
if key == "expire" {
var timestamp interface{} = val
t := time.Unix(int64(timestamp.(float64)), 0)
fmt.Printf("Key: %v, %d-%02d-%02dT%02d:%02d:%02d, remainder seconds: %d\n", key,
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(),
getTokenRemainingValidity(val),
)
} else {
fmt.Printf("Key: %v, value: %v\n", key, val)
}
}
}
}
func getTokenRemainingValidity(timestamp interface{}) int {
if validity, ok := timestamp.(float64); ok {
tm := time.Unix(int64(validity), 0)
remainder := tm.Sub(time.Now())
return int(remainder.Seconds())
}
return -1
}