-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt_service.go
160 lines (135 loc) · 3.54 KB
/
jwt_service.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package token
import (
"net/http"
"crypto/rsa"
"sync"
"time"
"io/ioutil"
"encoding/pem"
"crypto/x509"
"context"
jwt "github.com/dgrijalva/jwt-go"
"github.com/ipastushenko/simple-chat/server/settings"
)
type JWTClaims struct {
jwt.StandardClaims
Info interface{}
}
type JWTService struct {
secret *jwtSecret
}
type jwtSecret struct {
secretKey *rsa.PrivateKey
verifyKey *rsa.PublicKey
}
const (
userIdContextName string = "info"
)
var (
service *JWTService
once sync.Once
)
func NewJWTService() ITokenService {
return getInstance()
}
func (service *JWTService) GenerateToken(info interface{}) (string, error) {
config := settings.GetInstance()
token := jwt.New(jwt.SigningMethodRS256)
timeNow := time.Now()
expirationTime := time.Duration(config.GetInt("token_expiration"))
exp := timeNow.Add(expirationTime * time.Minute).Unix()
iat := timeNow.Unix()
token.Claims = &JWTClaims{
Info: info,
StandardClaims: jwt.StandardClaims{IssuedAt: iat, ExpiresAt: exp},
}
return token.SignedString(service.secret.secretKey)
}
func (service *JWTService) ParseToken(tokenString string) (interface{}, bool) {
token, err := jwt.ParseWithClaims(
tokenString,
&JWTClaims{},
func (*jwt.Token) (interface{}, error) {
return service.secret.verifyKey, nil
},
)
if err == nil && token.Valid {
return token, true
}
return nil, false
}
func (service *JWTService) UpdateRequestContext(
token interface{},
request *http.Request,
) *http.Request {
jwtToken, ok := token.(*jwt.Token)
if !ok {
return request
}
claims, ok := jwtToken.Claims.(*JWTClaims)
if !ok {
return request
}
currentContext := request.Context()
newContext := context.WithValue(
currentContext,
userIdContextName,
claims.Info,
)
return request.WithContext(newContext)
}
func (service *JWTService) GetRequestContextInfo(
request *http.Request,
) interface{} {
return request.Context().Value(userIdContextName)
}
//TODO: need to implement
func (service *JWTService) RefreshToken(string) (string, error) {
return "", nil
}
//TODO: need to implement
func (service *JWTService) RevokeToken(string) error {
return nil
}
//TODO: need to implement
func (service *JWTService) RevokeOtherTokens(string) error {
return nil
}
func getInstance() ITokenService {
once.Do(func() {
service = &JWTService{secret: &jwtSecret{}}
service.loadSecretKey()
service.loadVerifyKey()
})
return service
}
func (service *JWTService) loadSecretKey() {
config := settings.GetInstance()
secretKey, err := ioutil.ReadFile(config.GetString("secret_key_path"))
if err != nil {
panic(err)
}
secretData, _ := pem.Decode(secretKey)
importedSecretKey, err := x509.ParsePKCS1PrivateKey(secretData.Bytes)
if err != nil {
panic(err)
}
service.secret.secretKey = importedSecretKey
}
func (service *JWTService) loadVerifyKey() {
config := settings.GetInstance()
verifyKey, err := ioutil.ReadFile(config.GetString("verify_key_path"))
if err != nil {
panic(err)
}
verifyData, _ := pem.Decode(verifyKey)
rawVerifyKey, err := x509.ParsePKIXPublicKey(verifyData.Bytes)
if err != nil {
panic(err)
}
importedVerifyKey, ok := rawVerifyKey.(*rsa.PublicKey)
if !ok {
panic("rsa public key cast")
}
service.secret.verifyKey = importedVerifyKey
}