-
Notifications
You must be signed in to change notification settings - Fork 7
/
jwt.go
110 lines (88 loc) · 3.31 KB
/
jwt.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
103
104
105
106
107
108
109
110
package jwt
import (
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
_err "github.com/romapres2010/goapp/pkg/common/error"
_log "github.com/romapres2010/goapp/pkg/common/logger"
)
// Claims a struct that will be encoded to a JWT.
// We add jwt.StandardClaims as an embedded type, to provide fields like expiry time
type Claims struct {
Username string `json:"username"`
jwt.StandardClaims
}
// CheckJWT check JWT
func CheckJWT(reqID uint64, tknStr string, jwtKey []byte) (*Claims, error) {
// Initialize a new instance of `Claims`
claims := &Claims{}
// Parse the JWT string and store the result in `claims`.
// Note that we are passing the key in this method as well. This method will return an error
// if the token is invalid (if it has expired according to the expiry time we set on sign in),
// or if the signature does not match
tkn, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (interface{}, error) {
return jwtKey, nil
})
if err != nil {
if err == jwt.ErrSignatureInvalid {
return nil, _err.NewTyped(_err.ERR_HTTP_AUTH_JWT_INVALID_ERROR, reqID).PrintfError()
}
return nil, _err.WithCauseTyped(_err.ERR_HTTP_AUTH_JWT_EXPIRED_ERROR, reqID, err).PrintfError()
}
if !tkn.Valid {
return nil, _err.NewTyped(_err.ERR_HTTP_AUTH_JWT_EXPIRED_ERROR, reqID).PrintfError()
}
return claims, nil
}
// CheckJWTFromCookie load JWT check from Cookie and check it
func CheckJWTFromCookie(reqID uint64, cookie *http.Cookie, jwtKey []byte) error {
_, err := CheckJWT(reqID, cookie.Value, jwtKey)
return err
}
// CreateJWT create new JWT
func CreateJWT(claims *Claims, expirationTime *time.Time, jwtKey []byte) (string, error) {
// JWTExpiresAt больше 0 установим expiry time
if expirationTime != nil {
claims.StandardClaims.ExpiresAt = expirationTime.Unix()
}
_log.Debug("JWT: claims", claims)
// Declare the token with the algorithm used for signing, and the claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Create the JWT string
tokenString, err := token.SignedString(jwtKey)
if err != nil {
return "", err
}
_log.Debug("JWT: tokenString", tokenString)
return tokenString, nil
}
// CreateJWTCookie create new JWT as Cookie
func CreateJWTCookie(claims *Claims, jwtExpiresAt int, jwtKey []byte) (*http.Cookie, error) {
var expirationTime *time.Time
// jwtExpiresAt > 0 установим expiry time
if jwtExpiresAt > 0 {
t := time.Now().Add(time.Duration(jwtExpiresAt * int(time.Second)))
expirationTime = &t
} else {
// TODO - добавить дефолтную константу
t := time.Now()
expirationTime = &t
}
// создадим новый токен
tokenString, err := CreateJWT(claims, expirationTime, jwtKey)
if err != nil {
return nil, err
}
// подготовим Cookie
cookie := http.Cookie{
Name: "token",
Value: tokenString,
}
if jwtExpiresAt > 0 {
// set an expiry time is the same as the token itself
cookie.Expires = *expirationTime
} else {
cookie.MaxAge = 0 // без ограничения времени жизни
}
return &cookie, nil
}