-
Notifications
You must be signed in to change notification settings - Fork 60
/
d_api_login.go
65 lines (57 loc) · 1.41 KB
/
d_api_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
package uadmin
import "net/http"
func dAPILoginHandler(w http.ResponseWriter, r *http.Request, s *Session) {
_ = s
// Get request variables
username := r.FormValue("username")
password := r.FormValue("password")
otp := r.FormValue("otp")
session := r.FormValue("session")
optRequired := false
if otp != "" {
// Check if there is username and password or a session key
if session != "" {
s = Login2FAKey(r, session, otp)
} else {
s = Login2FA(r, username, password, otp)
}
} else {
s, optRequired = Login(r, username, password)
}
if optRequired {
w.WriteHeader(http.StatusAccepted)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "OTP Required",
"session": s.Key,
})
return
}
if s == nil {
w.WriteHeader(http.StatusUnauthorized)
ReturnJSON(w, r, map[string]interface{}{
"status": "error",
"err_msg": "Invalid credentials",
})
return
}
// Preload the user to get the group name
Preload(&s.User)
jwt := SetSessionCookie(w, r, s)
res := map[string]interface{}{
"status": "ok",
"session": s.Key,
"jwt": jwt,
"user": map[string]interface{}{
"username": s.User.Username,
"first_name": s.User.FirstName,
"last_name": s.User.LastName,
"group_name": s.User.UserGroup.GroupName,
"admin": s.User.Admin,
},
}
if CustomDAPILoginHandler != nil {
res = CustomDAPILoginHandler(r, &s.User, res)
}
ReturnJSON(w, r, res)
}