-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-token.go
344 lines (289 loc) · 10.5 KB
/
api-token.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
)
func (h *HTTPServer) setupTokenHandlers() {
h.router.POST("/token", tokenCreate())
h.router.POST("/token/refresh", tokenRefresh())
h.router.GET("/token", tokenInfo())
}
//TOKEN CREATION
func tokenCreate() func(*gin.Context) {
return func(c *gin.Context) {
pmethod := c.Request.Method
ppath := c.FullPath()
m := make(map[string]string)
data, _ := ioutil.ReadAll(c.Request.Body)
err := json.Unmarshal(data, &m)
if err != nil {
c.JSON(400, gin.H{"message": fmt.Sprintf("Couldn't parse body contents. err=%s", err)})
invocationCounter.WithLabelValues(pmethod, ppath, "400").Inc()
return
}
facebookToken, exists := m["facebookToken"]
if exists {
processFacebookLogin(m, facebookToken, c, pmethod, ppath)
return
}
googleAuthCode, exists := m["googleAuthCode"]
if exists {
processGoogleLogin(m, googleAuthCode, c, pmethod, ppath)
return
}
processLocalPasswordLogin(m, c, pmethod, ppath)
}
}
func processLocalPasswordLogin(m map[string]string, c *gin.Context, pmethod string, ppath string) {
logrus.Debugf("Authentication using local password")
email, exists := m["email"]
if !exists {
c.JSON(400, gin.H{"message": "Couldn't get email/password from body contents"})
invocationCounter.WithLabelValues(pmethod, ppath, "400").Inc()
return
}
u, success := processValidateUserActivated(email, c, pmethod, ppath)
if !success {
return
}
email = u.Email
password, exists := m["password"]
if !exists {
c.JSON(400, gin.H{"message": "Couldn't get email/password from body contents"})
invocationCounter.WithLabelValues(pmethod, ppath, "400").Inc()
return
}
logrus.Debugf("Verify wrong password retries")
if u.WrongPasswordDate != nil {
if int(u.WrongPasswordCount) >= opt.passwordRetriesMax {
logrus.Infof("Max wrong password retries reached for %s. Account locked", email)
c.JSON(465, gin.H{"message": "Max wrong password retries reached. Reset your password"})
invocationCounter.WithLabelValues(pmethod, ppath, "465").Inc()
return
}
delaySeconds := opt.passwordRetriesTimeSeconds * int(math.Pow(2, float64(u.WrongPasswordCount)))
if time.Now().Before(u.WrongPasswordDate.Add(time.Duration(delaySeconds) * time.Second)) {
logrus.Infof("Password retry time delay enforced for %s. delay=%d", email, delaySeconds)
c.JSON(450, gin.H{"message": "Email/password not valid"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
}
err := bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(password))
if err != nil {
logrus.Infof("Invalid password for %s", email)
logrus.Debugf("Increment wrong password counters")
err = db.Model(&u).UpdateColumn("wrong_password_count", u.WrongPasswordCount+1, "wrong_password_date", time.Now()).Error
if err != nil {
logrus.Warnf("Couldn't increment wrong password count for %s. err=%s", email, err)
}
c.JSON(450, gin.H{"message": "Email/password not valid"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
logrus.Debugf("Reset wrong password counters")
err = resetWrongPasswordCounters(u)
if err != nil {
logrus.Warnf("Couldn't zero wrong password count for %s. err=%s", email, err)
c.JSON(500, gin.H{"message": "Server error"})
invocationCounter.WithLabelValues(pmethod, ppath, "500").Inc()
return
}
validateUserAndOutputTokensToResponse(u, c, pmethod, ppath, "password", "")
logrus.Debugf("Local password login for %s", email)
}
func processValidateUserActivated(email string, c *gin.Context, pmethod string, ppath string) (*User, bool) {
var u User
db1 := db.First(&u, "email = ? AND activation_date IS NOT NULL", email)
if db1.RecordNotFound() {
c.JSON(450, gin.H{"message": "Email/password not valid"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return nil, false
}
if db1.Error != nil {
logrus.Warnf("Error authenticating user %s. err=%s", email, db1.Error)
c.JSON(500, gin.H{"message": "Server error"})
invocationCounter.WithLabelValues(pmethod, ppath, "500").Inc()
return nil, false
}
return &u, true
}
func validateUserAndOutputTokensToResponse(u *User, c *gin.Context, pmethod string, ppath string, authType string, socialRefreshToken string) {
if u.Enabled == 0 {
c.JSON(460, gin.H{"message": "Account disabled"})
invocationCounter.WithLabelValues(pmethod, ppath, "460").Inc()
return
}
customAccessTokenClaims := make(map[string]interface{})
customAccessTokenClaims["scope"] = strings.Split(opt.accessTokenDefaultScope, ",")
customRefreshTokenClaims := make(map[string]interface{})
if authType == "password" {
if u.PasswordValidUntil != nil {
if u.PasswordValidUntil.Before(time.Now()) {
c.JSON(455, gin.H{"message": "Password expired"})
invocationCounter.WithLabelValues(pmethod, ppath, "455").Inc()
return
}
}
}
if socialRefreshToken != "" {
customRefreshTokenClaims["socialToken"] = socialRefreshToken
}
logrus.Debugf("User %s authenticated and validated", u.Email)
tokensResponse, err := createAccessAndRefreshToken(u.Name, u.Email, authType, customAccessTokenClaims, customRefreshTokenClaims)
if err != nil {
logrus.Warnf("Error generating tokens for user %s. err=%s", u.Email, err)
c.JSON(500, gin.H{"message": "Server error"})
invocationCounter.WithLabelValues(pmethod, ppath, "500").Inc()
return
}
err = db.Model(&u).UpdateColumn("last_token_type", authType, "last_token_date", time.Now()).Error
if err != nil {
logrus.Warnf("Couldn't update last_token_type/date for %s. err=%s", u.Email, err)
}
c.JSON(200, tokensResponse)
invocationCounter.WithLabelValues(pmethod, ppath, "200").Inc()
logrus.Debugf("Tokens for %s generated and sent to response", u.Name)
}
//TOKEN REFRESH
func tokenRefresh() func(*gin.Context) {
return func(c *gin.Context) {
pmethod := c.Request.Method
ppath := c.FullPath()
claims, err := loadAndValidateToken(c.Request, "refresh", "")
if err != nil {
c.JSON(450, gin.H{"message": "Invalid refresh token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
email0, exists := claims["sub"]
if !exists {
logrus.Warnf("Refresh token valid but doesn't have 'sub' claim")
c.JSON(450, gin.H{"message": "Invalid refresh token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
email := email0.(string)
authType0, exists := claims["authType"]
if !exists {
logrus.Warnf("Refresh token valid but doesn't have 'authType' claim")
c.JSON(450, gin.H{"message": "Invalid refresh token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
authType := authType0.(string)
logrus.Debugf("Refresh token validated for %s. Verifying user account", email)
var u User
db1 := db.First(&u, "email = ? AND activation_date IS NOT NULL", email)
if db1.RecordNotFound() {
c.JSON(404, gin.H{"message": "Account not found"})
invocationCounter.WithLabelValues(pmethod, ppath, "404").Inc()
return
}
err = db1.Error
if err != nil {
logrus.Warnf("Error getting user during token refresh. email=%s err=%s", email, err)
c.JSON(500, gin.H{"message": "Server error"})
invocationCounter.WithLabelValues(pmethod, ppath, "500").Inc()
return
}
socialToken := ""
if authType != "password" {
socialToken0, exists := claims["socialToken"]
if !exists {
logrus.Warnf("Refresh token valid but doesn't have 'socialToken' claim")
c.JSON(450, gin.H{"message": "Invalid refresh token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
socialToken = socialToken0.(string)
if authType == "facebook" {
temail, _, success := processFacebookToken(c, socialToken, pmethod, ppath)
if !success {
return
}
socialToken1, success := processFacebookRefreshToken(c, socialToken, pmethod, ppath)
if !success {
return
}
socialToken = socialToken1
if email != temail {
logrus.Warnf("Refresh token valid but 'socialToken' is for another email. %s!=%s", temail, email)
c.JSON(450, gin.H{"message": "Invalid refresh token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
} else if authType == "google" {
temail, _, success := processGoogleRefreshToken(c, socialToken, pmethod, ppath)
if !success {
return
}
if email != temail {
logrus.Warnf("Refresh token valid but 'socialToken' is for another email. %s!=%s", temail, email)
c.JSON(450, gin.H{"message": "Invalid refresh token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
logrus.Debugf("Google refresh token valid for %s. Checking if account already exists")
}
}
validateUserAndOutputTokensToResponse(&u, c, pmethod, ppath, authType, socialToken)
logrus.Debugf("Token refresh for %s", email)
}
}
//TOKEN INFO
func tokenInfo() func(*gin.Context) {
return func(c *gin.Context) {
pmethod := c.Request.Method
ppath := c.FullPath()
claims, err := loadAndValidateToken(c.Request, "", "")
if err != nil {
logrus.Debugf("Invalid token. err=%s", err)
c.JSON(450, gin.H{"message": "Invalid token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
email, exists := claims["sub"]
if !exists {
logrus.Debugf("Invalid token. 'sub' claim not found")
c.JSON(450, gin.H{"message": "Invalid token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
var u User
db1 := db.First(&u, "email = ? AND enabled = 1", email)
if db1.RecordNotFound() {
c.JSON(455, gin.H{"message": "User not enabled"})
invocationCounter.WithLabelValues(pmethod, ppath, "455").Inc()
return
}
typ, exists1 := claims["typ"]
if !exists1 {
logrus.Debugf("Invalid token. 'typ' claim not found")
c.JSON(450, gin.H{"message": "Invalid token"})
invocationCounter.WithLabelValues(pmethod, ppath, "450").Inc()
return
}
if (typ == "access" || typ == "refresh") && u.ActivationDate == nil {
c.JSON(460, gin.H{"message": "Account not activated"})
invocationCounter.WithLabelValues(pmethod, ppath, "460").Inc()
return
}
if db1.Error != nil {
logrus.Warnf("Error finding user %s. err=%s", email, db1.Error)
c.JSON(500, gin.H{"message": "Server error"})
invocationCounter.WithLabelValues(pmethod, ppath, "500").Inc()
return
}
c.JSON(200, claims)
invocationCounter.WithLabelValues(pmethod, ppath, "200").Inc()
logrus.Debugf("Token info for %s", email)
}
}