/
auth.go
91 lines (73 loc) · 1.67 KB
/
auth.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
package tenant
import (
"errors"
"sync"
"github.com/askasoft/pango-xdemo/app"
"github.com/askasoft/pango-xdemo/app/models"
"github.com/askasoft/pango/xin"
"github.com/askasoft/pango/xmw"
"gorm.io/gorm"
)
// empty user
var noUser = &models.User{}
// USERS write lock
var muUSERS sync.Mutex
func FindUser(c *xin.Context, username string) (xmw.AuthUser, error) {
tt := FromCtx(c)
k := tt.String() + "\n" + username
if v, ok := app.USERS.Get(k); ok {
u := v.(*models.User)
if u.ID == 0 {
return nil, nil
}
return u, nil
}
muUSERS.Lock()
defer muUSERS.Unlock()
// get again to prevent duplicated load
if v, ok := app.USERS.Get(k); ok {
u := v.(*models.User)
if u.ID == 0 {
return nil, nil
}
return u, nil
}
u := &models.User{}
r := app.GDB.Table(tt.TableUsers()).Where("email = ? AND status = ?", username, models.UserActive).Take(u)
if r.Error != nil {
if errors.Is(r.Error, gorm.ErrRecordNotFound) {
app.USERS.Set(k, noUser)
return nil, nil
}
return nil, r.Error
}
app.USERS.Set(k, u)
return u, nil
}
// AuthUser get authenticated user
func AuthUser(c *xin.Context) *models.User {
au, ok := c.Get(app.XCA.AuthUserKey)
if ok {
return au.(*models.User)
}
panic("Invalid Authenticate User!")
}
func DeleteAuthUser(c *xin.Context) {
c.Del(app.XCA.AuthUserKey)
}
func IsClientBlocked(c *xin.Context) bool {
cip := c.ClientIP()
if v, ok := app.AFIPS.Get(cip); ok {
cnt := v.(int)
if cnt >= app.INI.GetInt("login", "maxFailure", 5) {
return true
}
}
return false
}
func CheckClientAndFindUser(c *xin.Context, username string) (xmw.AuthUser, error) {
if IsClientBlocked(c) {
return nil, nil
}
return FindUser(c, username)
}