forked from u5surf/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.go
167 lines (146 loc) · 4.33 KB
/
signup.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
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"unicode/utf8"
"github.com/moov-io/base"
moovhttp "github.com/moov-io/base/http"
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
)
const (
minPasswordLength = 8
)
type signupRequest struct {
Email string `json:"email"`
Password string `json:"password"`
// misc profile information
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Phone string `json:"phone"`
CompanyURL string `json:"companyUrl,omitempty"`
}
func addSignupRoutes(router *mux.Router, logger log.Logger, auth authable, userService userRepository) {
router.Methods("POST").Path("/users/create").HandlerFunc(signupRoute(auth, userService))
}
func signupRoute(auth authable, userService userRepository) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w = wrapResponseWriter(w, r, "signupRoute")
if r.Body == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
bs, err := read(r.Body)
if err != nil {
internalError(w, err)
return
}
// read request body
var signup signupRequest
if err := json.Unmarshal(bs, &signup); err != nil {
logger.Log("signup", err)
w.WriteHeader(http.StatusBadRequest)
return
}
requestId := moovhttp.GetRequestId(r)
// Basic data sanity checks
if err := validateEmail(signup.Email); err != nil {
moovhttp.Problem(w, err)
if requestId != "" && logger != nil {
logger.Log("signup", fmt.Sprintf("(requestId=%s) invalid email: %v", requestId, err))
}
return
}
if err := validatePassword(signup.Password); err != nil {
moovhttp.Problem(w, err)
if requestId != "" && logger != nil {
logger.Log("signup", fmt.Sprintf("(requestId=%s) invalid password: %v", requestId, err))
}
return
}
if err := validatePhone(signup.Phone); err != nil {
moovhttp.Problem(w, err)
if requestId != "" && logger != nil {
logger.Log("signup", fmt.Sprintf("(requestId=%s) invalid phone number: %v", requestId, err))
}
return
}
// find user
u, err := userService.lookupByEmail(signup.Email)
if err != nil {
internalError(w, fmt.Errorf("problem looking up user email %q: %v", signup.Email, err))
return
}
if u == nil {
var signup signupRequest
if err := json.Unmarshal(bs, &signup); err != nil {
moovhttp.Problem(w, err)
logger.Log("signup", fmt.Sprintf("failed parsing request json: %v", err))
return
}
// store user
userId := generateID()
if userId == "" {
internalError(w, fmt.Errorf("problem creating userId, err=%v", err))
return
}
u = &User{
ID: userId,
Email: signup.Email,
FirstName: signup.FirstName,
LastName: signup.LastName,
Phone: signup.Phone,
CompanyURL: signup.CompanyURL,
CreatedAt: base.NewTime(time.Now()),
}
if err := userService.upsert(u); err != nil {
internalError(w, fmt.Errorf("problem writing user: %v", err))
return
}
if err := auth.writePassword(u.ID, signup.Password); err != nil {
internalError(w, fmt.Errorf("problem writing user credentials: %v", err))
return
}
// signup worked, yay!
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
// TODO(adam): email approval link and clickthrough
} else {
// user found, so reject signup
moovhttp.Problem(w, fmt.Errorf("user already exists - %s", signup.Email))
}
}
}
func validateEmail(email string) error {
if email == "" || !strings.Contains(email, "@") {
return errors.New("no email provided")
}
return nil
}
func validatePassword(pass string) error {
if pass == "" {
return errors.New("no password provided")
}
if n := utf8.RuneCountInString(pass); n < minPasswordLength {
return fmt.Errorf("password required to be at least %d characters", minPasswordLength)
}
return nil
}
func validatePhone(phone string) error {
phone = strings.Replace(phone, "-", "", -1)
phone = strings.Replace(phone, ".", "", -1)
phone = strings.Replace(phone, " ", "", -1)
if m, _ := regexp.MatchString("^\\+?[1-9]\\d{1,14}$", phone); !m {
return errors.New("phone number is invalid")
}
return nil
}