@@ -19,24 +19,28 @@ import (
19
19
"github.com/coder/coder/httpmw"
20
20
)
21
21
22
+ // User is the JSON representation of a Coder user.
22
23
type User struct {
23
24
ID string `json:"id" validate:"required"`
24
25
Email string `json:"email" validate:"required"`
25
26
CreatedAt time.Time `json:"created_at" validate:"required"`
26
27
Username string `json:"username" validate:"required"`
27
28
}
28
29
30
+ // CreateUserRequest enables callers to create a new user.
29
31
type CreateUserRequest struct {
30
32
Email string `json:"email" validate:"required,email"`
31
33
Username string `json:"username" validate:"required,username"`
32
34
Password string `json:"password" validate:"required"`
33
35
}
34
36
37
+ // LoginWithPasswordRequest enables callers to authenticate with email and password.
35
38
type LoginWithPasswordRequest struct {
36
39
Email string `json:"email" validate:"required,email"`
37
40
Password string `json:"password" validate:"required"`
38
41
}
39
42
43
+ // LoginWithPasswordResponse contains a session token for the newly authenticated user.
40
44
type LoginWithPasswordResponse struct {
41
45
SessionToken string `json:"session_token" validate:"required"`
42
46
}
@@ -51,13 +55,15 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
51
55
if ! httpapi .Read (rw , r , & createUser ) {
52
56
return
53
57
}
58
+ // This should only function for the first user.
54
59
userCount , err := users .Database .GetUserCount (r .Context ())
55
60
if err != nil {
56
61
httpapi .Write (rw , http .StatusInternalServerError , httpapi.Response {
57
62
Message : fmt .Sprintf ("get user count: %s" , err .Error ()),
58
63
})
59
64
return
60
65
}
66
+ // If a user already exists, the initial admin user no longer can be created.
61
67
if userCount != 0 {
62
68
httpapi .Write (rw , http .StatusConflict , httpapi.Response {
63
69
Message : "the initial user has already been created" ,
@@ -116,6 +122,7 @@ func (users *users) getAuthenticatedUser(rw http.ResponseWriter, r *http.Request
116
122
})
117
123
}
118
124
125
+ // Authenticates the user with an email and password.
119
126
func (users * users ) loginWithPassword (rw http.ResponseWriter , r * http.Request ) {
120
127
var loginWithPassword LoginWithPasswordRequest
121
128
if ! httpapi .Read (rw , r , & loginWithPassword ) {
@@ -170,6 +177,7 @@ func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
170
177
return
171
178
}
172
179
180
+ // This format is consumed by the APIKey middleware.
173
181
sessionToken := fmt .Sprintf ("%s-%s" , id , secret )
174
182
http .SetCookie (rw , & http.Cookie {
175
183
Name : httpmw .AuthCookie ,
@@ -185,6 +193,7 @@ func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
185
193
})
186
194
}
187
195
196
+ // Generates a new ID and secret for an API key.
188
197
func generateAPIKeyIDSecret () (string , string , error ) {
189
198
// Length of an API Key ID.
190
199
id , err := cryptorand .String (10 )
0 commit comments