-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt-manager.go
58 lines (49 loc) · 1.71 KB
/
jwt-manager.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
package service
import (
"fmt"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/isjyi/grpc-demo/global"
"github.com/isjyi/grpc-demo/model"
"google.golang.org/grpc/status"
)
type JWTManager struct {
secretKey string
tokenDuration time.Duration
}
type UserClaims struct {
jwt.StandardClaims
Username string `json:"username"`
}
func NewJWTManager(secretKey string, tokenDuration time.Duration) *JWTManager {
return &JWTManager{secretKey, tokenDuration}
}
func (manager *JWTManager) Generate(user *model.User) (string, error) {
claims := UserClaims{
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(manager.tokenDuration).Unix(),
},
Username: user.Username,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(manager.secretKey))
}
func ParseToken(tokenString string) (interface{}, error) {
token, err := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(global.JwtSecret), nil
})
if claims, ok := token.Claims.(*UserClaims); ok && token.Valid {
return claims, nil
// fmt.Printf("%v %v", claims.Username, claims.StandardClaims.ExpiresAt)
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, status.Error(http.StatusUnauthorized, "That's not even a token")
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
return nil, status.Error(http.StatusUnauthorized, "Timing is everything")
} else {
return nil, status.Error(http.StatusUnauthorized, fmt.Sprint("Couldn't handle this token:", err))
}
}
return nil, status.Error(http.StatusInternalServerError, "unknown mistake")
}