/
auth.go
121 lines (105 loc) · 2.88 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
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
package controller
import (
"bytes"
template2 "html/template"
"net/http"
"net/url"
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/auth"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/modules/logger"
"github.com/GoAdminGroup/go-admin/modules/system"
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/captcha"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/response"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/types"
)
// Auth check the input password and username for authentication.
func (h *Handler) Auth(ctx *context.Context) {
var (
user models.UserModel
ok bool
errMsg = "fail"
s, exist = h.services.GetOrNot(auth.ServiceKey)
)
if capDriver, ok := h.captchaConfig["driver"]; ok {
capt, ok := captcha.Get(capDriver)
if ok {
if !capt.Validate(ctx.FormValue("token")) {
response.BadRequest(ctx, "wrong captcha")
return
}
}
}
if !exist {
password := ctx.FormValue("password")
username := ctx.FormValue("username")
if password == "" || username == "" {
response.BadRequest(ctx, "wrong password or username")
return
}
user, ok = auth.Check(password, username, h.conn)
} else {
user, ok, errMsg = auth.GetService(s).P(ctx)
}
if !ok {
response.BadRequest(ctx, errMsg)
return
}
err := auth.SetCookie(ctx, user, h.conn)
if err != nil {
response.Error(ctx, err.Error())
return
}
if ref := ctx.Referer(); ref != "" {
if u, err := url.Parse(ref); err == nil {
v := u.Query()
if r := v.Get("ref"); r != "" {
rr, _ := url.QueryUnescape(r)
response.OkWithData(ctx, map[string]interface{}{
"url": rr,
})
return
}
}
}
response.OkWithData(ctx, map[string]interface{}{
"url": h.config.GetIndexURL(),
})
}
// Logout delete the cookie.
func (h *Handler) Logout(ctx *context.Context) {
err := auth.DelCookie(ctx, db.GetConnection(h.services))
if err != nil {
logger.Error("logout error", err)
}
ctx.AddHeader("Location", h.config.Url(config.GetLoginUrl()))
ctx.SetStatusCode(302)
}
// ShowLogin show the login page.
func (h *Handler) ShowLogin(ctx *context.Context) {
tmpl, name := template.GetComp("login").GetTemplate()
buf := new(bytes.Buffer)
if err := tmpl.ExecuteTemplate(buf, name, struct {
UrlPrefix string
Title string
Logo template2.HTML
CdnUrl string
System types.SystemInfo
}{
UrlPrefix: h.config.AssertPrefix(),
Title: h.config.LoginTitle,
Logo: h.config.LoginLogo,
System: types.SystemInfo{
Version: system.Version(),
},
CdnUrl: h.config.AssetUrl,
}); err == nil {
ctx.HTML(http.StatusOK, buf.String())
} else {
logger.Error(err)
ctx.HTML(http.StatusOK, "parse template error (;′⌒`)")
}
}