/
gojwt_encrypt_functions.go
202 lines (187 loc) · 6.14 KB
/
gojwt_encrypt_functions.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
199
200
201
202
package gojwt
import (
"fmt";
"time";
"github.com/dgrijalva/jwt-go";
)
// JWT schema of the data it will store
type Claims struct {
NameServer string `json:"nameServer,omitempty"`
jwt.StandardClaims
}
// Create token with a specific encrypt method
func (o *Gojwt) CreateToken(username string) (tokenString string, err error) {
method := o.GetEncryptMethod()
lenByte := o.GetEncryptLenBytes()
if method == "RSA" {
tokenString, err = o.createRSAToken(lenByte, username)
} else if method == "ECDSA" {
tokenString, err = o.createECDSAToken(lenByte, username)
} else if method == "HMAC-SHA" {
tokenString, err = o.createHMACSHAToken(lenByte, username)
} else {
return "", GojwtErrInvalidAlgorithm
}
return tokenString, err
}
// validate token with a specific encrypt method
func (o *Gojwt) ValidateToken(tokenString string) (isValid bool, username string, err error) {
method := o.GetEncryptMethod()
if method == "RSA" || method == "ECDSA" {
isValid, username, err = o.validateECD_RSAToken(tokenString)
} else if method == "HMAC-SHA" {
isValid, username, err = o.validateHMACSHAToken(tokenString)
} else {
return false, "", GojwtErrInvalidEmptyToken
}
return isValid, username, err
}
// Create token with RSA algorithm
func (o *Gojwt) createRSAToken(lenBytes, username string) (string, error) {
var token *jwt.Token
// Create the Claims
claims := Claims{
o.GetNameServer(),
jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * o.GetNumHoursDuration()).Unix(), //time.Unix(c.ExpiresAt, 0),
Issuer: username,
IssuedAt: time.Now().Unix(),
},
}
if lenBytes == "256" {
token = jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
} else if lenBytes == "384"{
token = jwt.NewWithClaims(jwt.SigningMethodRS384, claims)
} else if lenBytes == "512"{
token = jwt.NewWithClaims(jwt.SigningMethodRS512, claims)
} else {
return "", GojwtErrInvalidRSABytes
}
tokenString, err := token.SignedString(o.GetRSAPrivKey())
fatal(err)
return tokenString, err
}
// Create token with ECDSA algorithm
func (o *Gojwt) createECDSAToken(lenBytes, username string) (string, error) {
var token *jwt.Token
// Create the Claims
claims := Claims{
o.GetNameServer(),
jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * o.GetNumHoursDuration()).Unix(), //time.Unix(c.ExpiresAt, 0),
Issuer: username,
IssuedAt: time.Now().Unix(),
},
}
if lenBytes == "256" {
token = jwt.NewWithClaims(jwt.SigningMethodES256, claims)
} else if lenBytes == "384"{
token = jwt.NewWithClaims(jwt.SigningMethodES384, claims)
//} else if lenBytes == "512"{
// token = jwt.NewWithClaims(jwt.SigningMethodES512, claims)
} else {
return "", GojwtErrInvalidECDSABytes
}
tokenString, err := token.SignedString(o.GetECDSAPrivKey())
fatal(err)
return tokenString, err
}
// Create token with HMAC-SHA algorithm
func (o *Gojwt) createHMACSHAToken(lenBytes, username string) (string, error) {
var token *jwt.Token
// Create the Claims
claims := Claims{
o.GetNameServer(),
jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * o.GetNumHoursDuration()).Unix(), //time.Unix(c.ExpiresAt, 0),
Issuer: username,
IssuedAt: time.Now().Unix(),
},
}
if lenBytes == "256"{
token = jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
} else if lenBytes == "384"{
token = jwt.NewWithClaims(jwt.SigningMethodHS384, claims)
} else if lenBytes == "512"{
token = jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
} else {
return "", GojwtErrInvalidHMACHSABytes
}
tokenString, err := token.SignedString(o.GetSecretByte())
fatal(err)
return tokenString, err
}
// Validate Token RSA/ECDSA algorithm
func (o *Gojwt) validateECD_RSAToken(tokenString string) (bool, string, error) {
method := o.GetEncryptMethod()
if tokenString == "" {
return false, "", GojwtErrInvalidEmptyToken
}
token, err := jwt.Parse(tokenString,func(token *jwt.Token) (interface{}, error) {
if method == "RSA"{
return o.GetRSAPubKey(), nil
} else if method == "ECDSA" {
return o.GetECDSAPubKey(), nil
} else {
return nil, GojwtErrInvalidToken
}
})
if token == nil {
return false, "", GojwtErrNotWorkToken
}
if token.Valid {
//"You look nice today"
claims, _ := token.Claims.(jwt.MapClaims)
//var user string = claims["username"].(string)
iss := claims["iss"].(string)
return true, iss, nil
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return false, "", GojwtErrBadFormatToken
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
// Token is either expired or not active yet
return false, "", GojwtErrTokenExpired
} else {
//"Couldn't handle this token:"
return false, "", err
}
} else {
//"Couldn't handle this token:"
return false, "", err
}
}
// Validate Token HMAC-SHA algorithm
func (o *Gojwt) validateHMACSHAToken(tokenString string) (bool, string, error) {
if tokenString == "" {
return false, "", GojwtErrInvalidEmptyToken
}
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method")
}
return o.GetSecretByte(), nil
})
if token == nil {
return false, "", GojwtErrNotWorkToken
}
if token.Valid {
//"You look nice today"
claims, _ := token.Claims.(*Claims)
//var user string = claims["username"].(string)
iss := claims.Issuer
return true, iss, err
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return false, "", GojwtErrBadFormatToken
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
// Token is either expired or not active yet
return false, "", GojwtErrTokenExpired
} else {
//"Couldn't handle this token:"
return false, "", err
}
} else {
//"Couldn't handle this token:"
return false, "", err
}
}