/
accesshandlers.go
203 lines (172 loc) · 4.41 KB
/
accesshandlers.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
package router
import (
"net/http"
"strconv"
"time"
"github.com/binhonglee/GlobeTrotte/src/turbine/access"
"github.com/binhonglee/GlobeTrotte/src/turbine/config"
"github.com/binhonglee/GlobeTrotte/src/turbine/flags"
"github.com/binhonglee/GlobeTrotte/src/turbine/logger"
"github.com/binhonglee/GlobeTrotte/src/turbine/nonprod"
"github.com/binhonglee/GlobeTrotte/src/turbine/user"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
var store *sessions.CookieStore
func init() {
key := config.GetConfigStringMap("access")["key"]
store = sessions.NewCookieStore([]byte(key))
}
func login(res http.ResponseWriter, req *http.Request) {
var item *access.LoginCredential
var ok bool
unpackJSON(&res, req, &item)
dummyUser := user.DummyUserObj()
if item == nil {
respond(res, dummyUser)
return
}
item.Email, ok = handleEmails(item.Email)
if !ok {
respond(res, dummyUser)
return
}
user, success := access.Login(*item)
if success {
newCookie(res, req, user.ID)
}
respond(res, user)
}
func logout(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "logged-in")
id, _ := session.Values["userid"].(int)
session.Values["authenticated"] = false
session.Save(req, res)
logger.Print(
logger.Router,
"Session deletion successful for "+strconv.Itoa(id),
)
allowCORS(&res)
}
func newCookie(
res http.ResponseWriter,
req *http.Request,
userID int,
) {
session, _ := store.Get(req, "logged-in")
session.Values["authenticated"] = true
session.Values["userid"] = userID
session.Values["startTime"] = time.Now().Unix()
session.Save(req, res)
}
func sendResetEmail(res http.ResponseWriter, req *http.Request) {
var email string
var ok bool
unpackJSON(&res, req, &email)
email, ok = handleEmails(email)
if !ok {
logger.Failure(logger.Router, "Invalid email")
respond(res, false)
return
}
respond(res, access.SendPasswordResetEmail(email))
}
func resetPassword(res http.ResponseWriter, req *http.Request) {
var item access.ResetPassword
var ok bool
unpackJSON(&res, req, &item)
item.Email, ok = handleEmails(item.Email)
if !ok {
logger.Failure(logger.Router, "Invalid email")
respond(res, false)
return
}
respond(res, access.TriggerResetPassword(item))
}
func whoamiV2(
res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "logged-in")
var val int
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
val = -1
} else if id, ok := session.Values["userid"].(int); ok {
refreshSession := true
if startTime, ok := session.Values["startTime"].(int64); ok {
if time.Now().Unix()-startTime <= (86400 * 30 / 2) {
refreshSession = false
}
}
if refreshSession {
logout(res, req)
newCookie(res, req, id)
}
val = id
} else {
val = -1
}
respond(res, user.GetUserObj(val, val))
}
func whoamiV3(
res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "logged-in")
var val int
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
val = -1
} else if id, ok := session.Values["userid"].(int); ok {
refreshSession := true
if startTime, ok := session.Values["startTime"].(int64); ok {
if time.Now().Unix()-startTime <= (86400 * 30 / 2) {
refreshSession = false
}
}
if refreshSession {
logout(res, req)
newCookie(res, req, id)
}
val = id
} else {
val = -1
}
respond(res, access.GetAuth(val))
}
// Non-prod functions below this line
// -----------------------------------------------------------------------------
func forceConfirmEmail(res http.ResponseWriter, req *http.Request) {
if flags.ProdServer() {
logger.Failure(logger.Router, "Force confirm email attempted on prod.")
respond(res, false)
return
}
vars := mux.Vars(req)
var id int
var err error
if id, err = strconv.Atoi(vars["id"]); err != nil {
logger.Err(logger.Router, err, "")
respond(res, false)
return
}
if verifyUser(req, id) {
if nonprod.ForceConfirm(id) {
respond(res, true)
return
}
}
respond(res, false)
}
func testGetConfirmCode(res http.ResponseWriter, req *http.Request) {
if flags.ProdServer() {
logger.Failure(logger.Router, "Get confirm email code attempted on prod.")
respond(res, false)
return
}
var email string
var ok bool
unpackJSON(&res, req, &email)
email, ok = handleEmails(email)
if !ok {
logger.Failure(logger.Router, "Invalid email")
respond(res, false)
return
}
respond(res, nonprod.GetConfirmEmailCode(email, getUserID(req)))
}