-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
204 lines (168 loc) · 4.87 KB
/
login.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
/*
* Bulldozer Framework
* Copyright (C) DesertBit
*/
package auth
import (
tr "github.com/desertbit/bulldozer/translate"
"github.com/desertbit/bulldozer/callback"
"github.com/desertbit/bulldozer/log"
"github.com/desertbit/bulldozer/mux"
"github.com/desertbit/bulldozer/sessions"
"github.com/desertbit/bulldozer/settings"
"github.com/desertbit/bulldozer/template"
"github.com/desertbit/bulldozer/templates"
"github.com/desertbit/bulldozer/ui/messagebox"
"github.com/desertbit/bulldozer/utils"
"fmt"
"strings"
)
const (
passwordTokenLength = 15
sessionValueKeyPasswordToken = "budAuthPassTok"
finishLoginCallbackName = "budAuthFinishLogin"
)
func init() {
// Register the callback.
callback.Register(finishLoginCallbackName, finishLogin)
}
//####################//
//### Login Events ###//
//####################//
type loginEvents struct{}
func (e *loginEvents) EventLogin(c *template.Context, loginName string, passwordHash string) {
// Get the session pointer.
s := c.Session()
// Always hide the loading indicator on return.
defer s.HideLoadingIndicator()
// Get the password login random token.
passwordTokenI, ok := s.InstanceGet(sessionValueKeyPasswordToken)
if !ok {
log.L.Warning("failed to obtain password token from session store for session with remote address: '%s'", s.RemoteAddr())
showLoginErrorMsgBox(s)
return
}
passwordToken, ok := passwordTokenI.(string)
if !ok {
log.L.Warning("failed to cast password token for session with remote address: '%s'", s.RemoteAddr())
showLoginErrorMsgBox(s)
return
}
// Trim the login name.
loginName = strings.ToLower(strings.TrimSpace(loginName))
// Check if inputs are valid.
if len(loginName) == 0 || len(passwordHash) == 0 {
showLoginErrorMsgBox(s)
return
}
// Try to get the user.
u, err := dbGetUser(loginName)
if err != nil {
log.L.Error("%v", err)
showLoginErrorMsgBox(s)
return
} else if u == nil {
showLoginErrorMsgBox(s)
return
}
// Check if the user is enabled.
if !u.Enabled {
// Show a messagebox.
messagebox.New().
SetTitle(tr.S("bud.auth.login.errorNotEnabled.title")).
SetText(tr.S("bud.auth.login.errorNotEnabled.text")).
SetType(messagebox.TypeAlert).
Show(s)
return
}
// Decrypt and generate the temporary SHA256 hash with the session ID and random token.
hash, err := decryptPasswordHash(u.PasswordHash)
if err != nil {
log.L.Error("failed to decrypt password hash for user '%s': %v", loginName, err)
showLoginErrorMsgBox(s)
return
}
hash = utils.Sha256Sum(hash + s.SessionID() + passwordToken)
// Check if the password is valid.
if passwordHash != hash {
showLoginErrorMsgBox(s)
return
}
// If this is the first login, then request a new password.
if u.LastLogin <= 0 {
opts := ChangePasswordDialogOpts{
CallbackName: finishLoginCallbackName,
}
if err = ShowChangePasswordDialog(s, newUser(u), opts); err != nil {
log.L.Error(err.Error())
}
return
}
// Finish the login
finishLogin(s, newUser(u))
}
//###############//
//### Private ###//
//###############//
func onLoginTemplateGetData(c *template.Context) interface{} {
// Generate a new random password token.
passwordToken := utils.RandomString(passwordTokenLength)
// Save the password token to the session.
c.Session().InstanceSet(sessionValueKeyPasswordToken, passwordToken)
// Create the template render data.
data := struct {
RegistrationDisabled bool
PasswordToken string
}{
RegistrationDisabled: settings.Settings.RegistrationDisabled,
PasswordToken: passwordToken,
}
return data
}
func routeLoginPage(s *sessions.Session, req *mux.Request) {
// If already authenticated, then redirect to the default page.
if IsAuth(s) {
s.NavigateHome()
return
}
// Execute the login template.
o, _, _, err := templates.Templates.ExecuteTemplateToString(s, loginTemplate)
if err != nil {
req.Error(fmt.Errorf("failed to execute login template: %v", err))
return
}
// Set the body and title
req.Body = o
req.Title = tr.S("bud.auth.login.pageTitle")
return
}
func showLoginErrorMsgBox(s *sessions.Session) {
// Show a messagebox
messagebox.New().
SetTitle(tr.S("bud.auth.login.error.title")).
SetText(tr.S("bud.auth.login.error.text")).
SetType(messagebox.TypeAlert).
Show(s)
}
func finishLogin(s *sessions.Session, user *User) {
// Get the database user.
u := user.u
// Update the last login time
err := dbUpdateLastLogin(u)
if err != nil {
log.L.Error("failed to update last login time of user '%s': %v", u.LoginName, err)
showLoginErrorMsgBox(s)
return
}
// Create a new session authentication data value.
d := &sessionAuthData{
UserID: u.ID,
}
// Save the authentication data to the session values.
// This makes the user login public to the complete application.
s.Set(sessionValueKeyIsAuth, d)
// Redirect to the default page.
s.NavigateHome()
// Trigger the event
triggerOnNewAuthenticatedSession(s)
}