This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathaccount.go
234 lines (183 loc) · 4.79 KB
/
account.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package photoshare
import (
"net/http"
"strings"
"time"
)
func getAuthRedirectURL(ctx *context, w http.ResponseWriter, r *http.Request) error {
url, err := ctx.auth.getRedirectURL(r, ctx.params.get("provider"))
if err != nil {
return err
}
return renderString(w, http.StatusOK, url)
}
func authCallback(ctx *context, w http.ResponseWriter, r *http.Request) error {
info, err := ctx.auth.getUserInfo(r, ctx.params.get("provider"))
if err != nil {
return err
}
// tbd: handle new users
user, err := ctx.datamapper.getUserByEmail(info.email)
if err != nil {
return err
}
authToken, err := ctx.session.createToken(user.ID)
if err != nil {
return err
}
cookie := &http.Cookie{
Name: "authToken",
Value: authToken,
Path: "/",
Expires: time.Now().AddDate(0, 0, 1),
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/", http.StatusSeeOther)
return nil
}
func logout(ctx *context, w http.ResponseWriter, r *http.Request) error {
if err := ctx.session.writeToken(w, 0); err != nil {
return err
}
sendMessage(&socketMessage{ctx.user.Name, "", 0, "logout"})
return renderJSON(w, newSessionInfo(&user{}), http.StatusOK)
}
func getSessionInfo(ctx *context, w http.ResponseWriter, r *http.Request) error {
return renderJSON(w, newSessionInfo(ctx.user), http.StatusOK)
}
func login(ctx *context, w http.ResponseWriter, r *http.Request) error {
s := &struct {
Identifier string `json:"identifier"`
Password string `json:"password"`
}{}
var invalidLogin = httpError{http.StatusBadRequest, "Invalid email or password"}
if err := decodeJSON(r, s); err != nil {
return err
}
if s.Identifier == "" || s.Password == "" {
return invalidLogin
}
user, err := ctx.datamapper.getUserByNameOrEmail(s.Identifier)
if err != nil {
if isErrSqlNoRows(err) {
return invalidLogin
}
return err
}
if !user.checkPassword(s.Password) {
return invalidLogin
}
if err := ctx.session.writeToken(w, user.ID); err != nil {
return err
}
user.IsAuthenticated = true
sendMessage(&socketMessage{user.Name, "", 0, "login"})
return renderJSON(w, newSessionInfo(user), http.StatusCreated)
}
func signup(ctx *context, w http.ResponseWriter, r *http.Request) error {
s := &struct {
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
}{}
if err := decodeJSON(r, s); err != nil {
return err
}
user := &user{
Name: s.Name,
Email: strings.ToLower(s.Email),
Password: s.Password,
}
if err := ctx.validate(user, r); err != nil {
return err
}
if err := ctx.datamapper.createUser(user); err != nil {
return err
}
if err := ctx.session.writeToken(w, user.ID); err != nil {
return err
}
user.IsAuthenticated = true
go func() {
if err := ctx.mailer.sendWelcomeMail(user); err != nil {
logError(err)
}
}()
return renderJSON(w, newSessionInfo(user), http.StatusCreated)
}
func changePassword(ctx *context, w http.ResponseWriter, r *http.Request) error {
var (
user *user
err error
)
s := &struct {
Password string `json:"password"`
RecoveryCode string `json:"code"`
}{}
if err = decodeJSON(r, s); err != nil {
return err
}
if s.RecoveryCode == "" {
if user, err = ctx.authenticate(r, authLevelLogin); err != nil {
return err
}
} else {
if user, err = ctx.datamapper.getUserByRecoveryCode(s.RecoveryCode); err != nil {
return err
}
user.resetRecoveryCode()
}
if err = user.changePassword(s.Password); err != nil {
return err
}
if err := ctx.validate(user, r); err != nil {
return err
}
if err := ctx.datamapper.updateUser(user); err != nil {
return err
}
return renderString(w, http.StatusOK, "Password changed")
}
func emailExists(ctx *context, w http.ResponseWriter, r *http.Request) error {
email := r.FormValue("email")
if email == "" {
return httpError{http.StatusBadRequest, "Missing email address"}
}
u := &user{Email: email}
used, err := ctx.datamapper.isUserEmailAvailable(u)
if err != nil {
return err
}
s := &struct {
Exists bool `json:"exists"`
}{!used}
return renderJSON(w, s, http.StatusOK)
}
func recoverPassword(ctx *context, w http.ResponseWriter, r *http.Request) error {
s := &struct {
Email string `json:"email"`
}{}
if err := decodeJSON(r, s); err != nil {
return err
}
if s.Email == "" {
return httpError{http.StatusBadRequest, "Missing email address"}
}
user, err := ctx.datamapper.getUserByEmail(s.Email)
if err != nil {
if isErrSqlNoRows(err) {
return httpError{http.StatusBadRequest, "Email address not found"}
}
return err
}
code, err := user.generateRecoveryCode()
if err := ctx.datamapper.updateUser(user); err != nil {
return err
}
go func() {
if err := ctx.mailer.sendResetPasswordMail(user, code, r); err != nil {
logError(err)
}
}()
return renderString(w, http.StatusOK, "Password reset")
}