-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
55 lines (49 loc) · 1.1 KB
/
token.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
package appConfig
import (
"encoding/hex"
"fmt"
"github.com/dgrijalva/jwt-go/v4"
"net"
"time"
)
const (
expiry = 172800 //48hour
)
//painterClaim custom claim
type painterClaim struct {
IP string `json:"address"`
jwt.StandardClaims
}
//GenToken json web token
func GenToken(ip net.IP, key []byte) string {
now := time.Now()
next := now.Add(expiry * time.Second)
claims := &painterClaim{
IP: ip.String(),
StandardClaims: jwt.StandardClaims{
ExpiresAt: jwt.At(next),
NotBefore: jwt.At(now),
},
}
fmt.Println("###key",hex.EncodeToString(key))
fmt.Println("###ip",ip.String())
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
result, err := token.SignedString(key)
if err !=nil{
panic(err)
}
return result
}
//CheckToken 校验token是否有效, 包括IP
func CheckToken(token string, key []byte, fromIP *net.IP) bool {
tokenObject, err := jwt.ParseWithClaims(token, &painterClaim{},
func(*jwt.Token) (interface{}, error) {
return key, nil
},
)
if err != nil {
return false
}
claim := tokenObject.Claims.(*painterClaim)
return fromIP.Equal(net.ParseIP(claim.IP))
}