-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
198 lines (175 loc) · 4.56 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package auth
import (
"crypto/rsa"
"errors"
"io/ioutil"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
type JwtToken interface {
GetToken(host string, data map[string]interface{}, exp uint8) (*string, error)
ParseToken(tokenStr string) (*jwt.Token, error)
// 對特定資源存取金鑰
GetJwtAccessToken(host string, source string, id interface{}, db string, perm UserPerm) (*string, error)
GetCompanyToken(host, compID, compName, userID, acc, userName string, perm UserPerm) (*string, error)
}
type JwtDI interface {
GetKid() string
NewJwt() JwtToken
}
type JwtConf struct {
PrivateKeyFile string `yaml:"privatekey"`
PublicKeyFile string `yaml:"publickey"`
Header struct {
Alg string `yaml:"alg"`
Typ string `yaml:"typ"`
Kid string `yaml:"kid"`
} `yaml:"header"`
Claims struct {
ExpDuration time.Duration `yaml:"exp"`
} `yaml:"claims"`
myHeader map[string]interface{}
publicKey *rsa.PublicKey
privateKey *rsa.PrivateKey
}
func (j *JwtConf) getHeader() map[string]interface{} {
if j.myHeader != nil {
delete(j.myHeader, "usa")
return j.myHeader
}
j.myHeader = map[string]interface{}{
"alg": j.Header.Alg,
"typ": j.Header.Typ,
"kid": j.Header.Kid,
}
return j.myHeader
}
func (j *JwtConf) getPublicKey() (*rsa.PublicKey, error) {
if j.publicKey != nil {
return j.publicKey, nil
}
publicData, err := ioutil.ReadFile(j.PublicKeyFile)
if err != nil {
return nil, err
}
j.publicKey, err = jwt.ParseRSAPublicKeyFromPEM(publicData)
return j.publicKey, err
}
func (j *JwtConf) getPrivateKey() (*rsa.PrivateKey, error) {
if j.privateKey != nil {
return j.privateKey, nil
}
privateData, err := ioutil.ReadFile(j.PrivateKeyFile)
if err != nil {
return nil, err
}
j.privateKey, err = jwt.ParseRSAPrivateKeyFromPEM(privateData)
return j.privateKey, err
}
func (j *JwtConf) GetKid() string {
return j.Header.Kid
}
func (j *JwtConf) NewJwt() JwtToken {
return j
}
func (j *JwtConf) ParseToken(tokenStr string) (*jwt.Token, error) {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
pk, err := j.getPublicKey()
return pk, err
})
if token == nil {
return nil, errors.New("token is nil")
}
if token.Valid {
return token, nil
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, errors.New("That's not even a token")
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
// Token is either expired or not active yet
return nil, errors.New("Timing is everything")
} else {
return nil, err
}
}
return nil, err
}
func (j *JwtConf) GetToken(host string, data map[string]interface{}, exp uint8) (*string, error) {
if j == nil {
return nil, errors.New("jwtConf not set")
}
if data == nil {
return nil, errors.New("no data")
}
if exp <= 0 {
exp = 60
} else if exp > 180 {
exp = 180
}
now := time.Now()
data["iss"] = host
data["iat"] = now.Unix()
data["exp"] = now.Add(time.Duration(exp) * time.Minute).Unix()
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims(data))
token.Header = j.getHeader()
pk, err := j.getPrivateKey()
if err != nil {
return nil, err
}
ss, err := token.SignedString(pk)
if err != nil {
return nil, err
}
return &ss, nil
}
func (j *JwtConf) GetJwtAccessToken(host string, source string, id interface{}, db string, perm UserPerm) (*string, error) {
if j == nil {
return nil, errors.New("jwtConf not set")
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims(map[string]interface{}{
"iss": host,
"source": source,
"sourceId": id,
"db": db,
"per": perm,
}))
token.Header = j.getHeader()
token.Header["usa"] = "access"
pk, err := j.getPrivateKey()
if err != nil {
return nil, err
}
ss, err := token.SignedString(pk)
if err != nil {
return nil, err
}
return &ss, nil
}
func (j *JwtConf) GetCompanyToken(host, compID, compName, userID, acc, userName string, perm UserPerm) (*string, error) {
if j == nil {
return nil, errors.New("jwtConf not set")
}
now := time.Now()
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims(map[string]interface{}{
"iss": host,
"iat": now.Unix(),
"exp": now.Add(time.Duration(180) * time.Minute).Unix(),
"comp": compName,
"compID": compID,
"per": perm,
"sub": userID,
"acc": acc,
"nam": userName,
}))
token.Header = j.getHeader()
token.Header["usa"] = "comp"
pk, err := j.getPrivateKey()
if err != nil {
return nil, err
}
ss, err := token.SignedString(pk)
if err != nil {
return nil, err
}
return &ss, nil
}