/
signup.go
177 lines (145 loc) · 5.18 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
168
169
170
171
172
173
174
175
176
177
package api
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/netlify/gotrue/models"
)
// SignupParams are the parameters the Signup endpoint accepts
type SignupParams struct {
Email string `json:"email"`
Password string `json:"password"`
Data map[string]interface{} `json:"data"`
Provider string `json:"provider"`
Code string `json:"code"`
}
func (a *API) signupExternalProvider(ctx context.Context, w http.ResponseWriter, r *http.Request, params *SignupParams) error {
config := getConfig(ctx)
instanceID := getInstanceID(ctx)
provider, err := a.Provider(ctx, params.Provider)
if err != nil {
return badRequestError("Unsupported provider: %+v", err)
}
tok, err := provider.GetOAuthToken(ctx, params.Code)
if err != nil {
return internalServerError("Unable to exchange external code")
}
aud := a.requestAud(ctx, r)
params.Email, err = provider.GetUserEmail(ctx, tok)
if err != nil {
return internalServerError("Error getting user email from external provider").WithInternalError(err)
}
if exists, err := a.db.IsDuplicatedEmail(instanceID, params.Email, aud); exists {
return badRequestError("User already exists")
} else if err != nil {
return internalServerError("Error checking for duplicate users").WithInternalError(err)
}
user, err := a.signupNewUser(ctx, params, aud)
if err != nil {
return err
}
if config.Mailer.Autoconfirm {
user.Confirm()
} else if user.ConfirmationSentAt.Add(config.Mailer.MaxFrequency).Before(time.Now()) {
mailer := getMailer(ctx)
if err := mailer.ConfirmationMail(user); err != nil {
return internalServerError("Error sending confirmation mail").WithInternalError(err)
}
}
user.SetRole(config.JWT.DefaultGroupName)
if err := a.db.UpdateUser(user); err != nil {
return internalServerError("Error updating user in database").WithInternalError(err)
}
return sendJSON(w, http.StatusOK, user)
}
// Signup is the endpoint for registering a new user
func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
config := getConfig(ctx)
instanceID := getInstanceID(ctx)
params := &SignupParams{}
jsonDecoder := json.NewDecoder(r.Body)
err := jsonDecoder.Decode(params)
if err != nil {
return badRequestError("Could not read Signup params: %v", err)
}
if params.Provider != "" {
if params.Code == "" {
return unprocessableEntityError("Invalid code from OAuth provider")
}
return a.signupExternalProvider(ctx, w, r, params)
}
if params.Email == "" || params.Password == "" {
return unprocessableEntityError("Signup requires a valid email and password")
}
aud := a.requestAud(ctx, r)
user, err := a.db.FindUserByEmailAndAudience(instanceID, params.Email, aud)
if err != nil {
if !models.IsNotFoundError(err) {
return internalServerError("Database error finding user").WithInternalError(err)
}
user, err = a.signupNewUser(ctx, params, aud)
if err != nil {
return err
}
} else {
user, err = a.confirmUser(user, params)
if err != nil {
return err
}
}
mailer := getMailer(ctx)
if err = mailer.ValidateEmail(params.Email); err != nil {
return unprocessableEntityError("Unable to validate email address: " + err.Error())
}
if config.Mailer.Autoconfirm {
user.Confirm()
} else if user.ConfirmationSentAt.Add(config.Mailer.MaxFrequency).Before(time.Now()) {
if err := mailer.ConfirmationMail(user); err != nil {
return internalServerError("Error sending confirmation mail").WithInternalError(err)
}
}
user.SetRole(config.JWT.DefaultGroupName)
if err = a.db.UpdateUser(user); err != nil {
return internalServerError("Database error updating user").WithInternalError(err)
}
return sendJSON(w, http.StatusOK, user)
}
func (a *API) signupNewUser(ctx context.Context, params *SignupParams, aud string) (*models.User, error) {
config := getConfig(ctx)
instanceID := getInstanceID(ctx)
user, err := models.NewUser(instanceID, params.Email, params.Password, aud, params.Data)
if err != nil {
return nil, internalServerError("Database error creating user").WithInternalError(err)
}
if params.Password == "" {
user.EncryptedPassword = ""
}
if err := a.db.CreateUser(user); err != nil {
return nil, internalServerError("Database error saving new user").WithInternalError(err)
}
userCount, err := a.db.CountOtherUsers(instanceID, user.ID)
if err != nil {
return nil, internalServerError("Database error counting users").WithInternalError(err)
}
// first user automatically becomes admin
if userCount == 0 && !config.JWT.AdminGroupDisabled {
user.SetRole(config.JWT.AdminGroupName)
if err = a.db.UpdateUser(user); err != nil {
return nil, internalServerError("Database error updating user").WithInternalError(err)
}
}
return user, nil
}
func (a *API) confirmUser(user *models.User, params *SignupParams) (*models.User, error) {
if user.IsRegistered() {
return nil, badRequestError("A user with this email address has already been registered")
}
user.UpdateUserMetaData(params.Data)
user.GenerateConfirmationToken()
if err := a.db.UpdateUser(user); err != nil {
return nil, internalServerError("Database error updating user").WithInternalError(err)
}
return user, nil
}