/
users.go
208 lines (172 loc) · 5.34 KB
/
users.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
package main
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"time"
"golang.org/x/net/context"
"github.com/nicolai86/dash-annotations/dash"
)
var (
// ErrMissingUsername is returned for registration if missing the username parameter
ErrMissingUsername = errors.New("Missing parameter: username")
// ErrMissingPassword is returned for registration is missing the password parameter
ErrMissingPassword = errors.New("Missing parameter: password")
// ErrUsernameExists is returned for registration attempts where the username is taken
ErrUsernameExists = errors.New("A user with this username already exists")
// ErrInvalidLogin is returned if the login request fails, either because the username or password is wrong
ErrInvalidLogin = errors.New("Login failed: invalid username or password")
// ErrEmailExists is returned when a user wants to change his email to an already taken email address
ErrEmailExists = errors.New("A user with this email already exists")
)
type userRegisterRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
// UserRegister tries to create a new user inside the dash annotations database
func UserRegister(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
var payload userRegisterRequest
json.NewDecoder(req.Body).Decode(&payload)
if payload.Username == "" {
return ErrMissingUsername
}
if payload.Password == "" {
return ErrMissingPassword
}
var store = ctx.Value(UserStoreKey).(UserCreater)
if err := store.InsertUser(payload.Username, payload.Password); err != nil {
return err
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{
"status": "success",
})
return nil
}
type userLoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
func generateRandomBytes(n int) ([]byte, error) {
var b = make([]byte, n)
var _, err = rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func generateRandomString(s int) (string, error) {
var b, err = generateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b), err
}
type userLoginStore interface {
UserFinderByUsername
UserTokenUpdater
}
// UserLogin tries to authenticate an existing user using username/ password combination
func UserLogin(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
var payload userLoginRequest
json.NewDecoder(req.Body).Decode(&payload)
if payload.Username == "" {
return ErrMissingUsername
}
if payload.Password == "" {
return ErrMissingPassword
}
var loginStore = ctx.Value(UserStoreKey).(userLoginStore)
var user, err = loginStore.FindUserByUsername(payload.Username)
if err != nil {
return ErrInvalidLogin
}
if !user.PasswordsMatch(payload.Password) {
return ErrInvalidLogin
}
var sessionID, _ = generateRandomString(32)
if err := loginStore.UpdateUserWithToken(user.Username, sessionID); err != nil {
return err
}
var ckie *http.Cookie
for _, cookie := range req.Cookies() {
if cookie.Name == "laravel_session" {
ckie = cookie
break
}
}
if ckie == nil {
ckie = &http.Cookie{
Name: "laravel_session",
}
}
if encryptedSessionID, err := encrypt([]byte(sessionID)); err != nil {
return err
} else {
ckie.Value = string(encryptedSessionID)
}
ckie.MaxAge = 7200
ckie.Expires = time.Now().Add(7200 * time.Second)
ckie.Path = "/"
ckie.HttpOnly = true
http.SetCookie(w, ckie)
w.WriteHeader(http.StatusOK)
var data = map[string]string{
"status": "success",
}
if user.Email.String != "" {
data["email"] = user.Email.String
}
json.NewEncoder(w).Encode(data)
return nil
}
// UserLogout destroys the session of the current user
func UserLogout(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
var tokenStore = ctx.Value(UserStoreKey).(UserTokenUpdater)
var user = ctx.Value(UserKey).(*dash.User)
http.SetCookie(w, &http.Cookie{
Name: "laravel_session",
Value: "",
MaxAge: -1,
})
if err := tokenStore.UpdateUserWithToken(user.Username, ""); err != nil {
return err
}
json.NewEncoder(w).Encode(map[string]string{
"status": "success",
})
return nil
}
type userChangePasswordRequest struct {
Password string `json:"password"`
}
// UserChangePassword changes the encrypted password of the current user
func UserChangePassword(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
var user = ctx.Value(UserKey).(*dash.User)
var passwordUpdater = ctx.Value(UserStoreKey).(UserPasswordUpdater)
var payload userChangePasswordRequest
json.NewDecoder(req.Body).Decode(&payload)
if err := passwordUpdater.UpdateUserWithPassword(user.Username, payload.Password); err != nil {
return err
}
json.NewEncoder(w).Encode(map[string]string{
"status": "success",
})
return nil
}
type userChangeEmailRequest struct {
Email string `json:"email"`
}
// UserChangeEmail changes the email address of the current user
func UserChangeEmail(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
var emailUpdater = ctx.Value(UserStoreKey).(UserEmailUpdater)
var user = ctx.Value(UserKey).(*dash.User)
var payload userChangeEmailRequest
json.NewDecoder(req.Body).Decode(&payload)
if err := emailUpdater.UpdateUserWithEmail(user.Username, payload.Email); err != nil {
return err
}
json.NewEncoder(w).Encode(map[string]string{
"status": "success",
})
return nil
}