@@ -17,6 +17,7 @@ import (
17
17
"golang.org/x/oauth2"
18
18
"golang.org/x/xerrors"
19
19
20
+ "github.com/coder/coder/coderd/audit"
20
21
"github.com/coder/coder/coderd/database"
21
22
"github.com/coder/coder/coderd/httpapi"
22
23
"github.com/coder/coder/coderd/httpmw"
@@ -66,9 +67,18 @@ func (api *API) userAuthMethods(rw http.ResponseWriter, r *http.Request) {
66
67
// @Router /users/oauth2/github/callback [get]
67
68
func (api * API ) userOAuth2Github (rw http.ResponseWriter , r * http.Request ) {
68
69
var (
69
- ctx = r .Context ()
70
- state = httpmw .OAuth2 (r )
70
+ ctx = r .Context ()
71
+ state = httpmw .OAuth2 (r )
72
+ auditor = api .Auditor .Load ()
73
+ aReq , commitAudit = audit .InitRequest [database.APIKey ](rw , & audit.RequestParams {
74
+ Audit : * auditor ,
75
+ Log : api .Logger ,
76
+ Request : r ,
77
+ Action : database .AuditActionLogin ,
78
+ })
71
79
)
80
+ aReq .Old = database.APIKey {}
81
+ defer commitAudit ()
72
82
73
83
oauthClient := oauth2 .NewClient (ctx , oauth2 .StaticTokenSource (state .Token ))
74
84
@@ -81,6 +91,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
81
91
Message : "Internal error fetching authenticated Github user organizations." ,
82
92
Detail : err .Error (),
83
93
})
94
+ // We pass a disposable user ID just to force an audit diff
95
+ // and generate a log for a failed login
96
+ aReq .New = database.APIKey {UserID : uuid .New ()}
84
97
return
85
98
}
86
99
@@ -101,6 +114,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
101
114
httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
102
115
Message : "You aren't a member of the authorized Github organizations!" ,
103
116
})
117
+ // We pass a disposable user ID just to force an audit diff
118
+ // and generate a log for a failed login
119
+ aReq .New = database.APIKey {UserID : uuid .New ()}
104
120
return
105
121
}
106
122
}
@@ -111,6 +127,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
111
127
Message : "Internal error fetching authenticated Github user." ,
112
128
Detail : err .Error (),
113
129
})
130
+ // We pass a disposable user ID just to force an audit diff
131
+ // and generate a log for a failed login
132
+ aReq .New = database.APIKey {UserID : uuid .New ()}
114
133
return
115
134
}
116
135
@@ -139,6 +158,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
139
158
httpapi .Write (ctx , rw , http .StatusUnauthorized , codersdk.Response {
140
159
Message : fmt .Sprintf ("You aren't a member of an authorized team in the %v Github organization(s)!" , organizationNames ),
141
160
})
161
+ // We pass a disposable user ID just to force an audit diff
162
+ // and generate a log for a failed login
163
+ aReq .New = database.APIKey {UserID : uuid .New ()}
142
164
return
143
165
}
144
166
}
@@ -149,6 +171,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
149
171
Message : "Internal error fetching personal Github user." ,
150
172
Detail : err .Error (),
151
173
})
174
+ // We pass a disposable user ID just to force an audit diff
175
+ // and generate a log for a failed login
176
+ aReq .New = database.APIKey {UserID : uuid .New ()}
152
177
return
153
178
}
154
179
@@ -164,10 +189,28 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
164
189
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
165
190
Message : "Your primary email must be verified on GitHub!" ,
166
191
})
192
+ // We pass a disposable user ID just to force an audit diff
193
+ // and generate a log for a failed login
194
+ aReq .New = database.APIKey {UserID : uuid .New ()}
167
195
return
168
196
}
169
197
170
- cookie , err := api .oauthLogin (r , oauthLoginParams {
198
+ user , link , err := findLinkedUser (ctx , api .Database , githubLinkedID (ghUser ), verifiedEmail .GetEmail ())
199
+ if err != nil {
200
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
201
+ Message : "Failed to find linked user." ,
202
+ Detail : err .Error (),
203
+ })
204
+ // We pass a disposable user ID just to force an audit diff
205
+ // and generate a log for a failed login
206
+ aReq .New = database.APIKey {UserID : uuid .New ()}
207
+ return
208
+ }
209
+ aReq .UserID = user .ID
210
+
211
+ cookie , key , err := api .oauthLogin (r , oauthLoginParams {
212
+ User : user ,
213
+ Link : link ,
171
214
State : state ,
172
215
LinkedID : githubLinkedID (ghUser ),
173
216
LoginType : database .LoginTypeGithub ,
@@ -182,16 +225,24 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
182
225
Message : httpErr .msg ,
183
226
Detail : httpErr .detail ,
184
227
})
228
+ // We pass a disposable user ID just to force an audit diff
229
+ // and generate a log for a failed login
230
+ aReq .New = database.APIKey {UserID : uuid .New ()}
185
231
return
186
232
}
187
233
if err != nil {
188
234
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
189
235
Message : "Failed to process OAuth login." ,
190
236
Detail : err .Error (),
191
237
})
238
+ // We pass a disposable user ID just to force an audit diff
239
+ // and generate a log for a failed login
240
+ aReq .New = database.APIKey {UserID : uuid .New ()}
192
241
return
193
242
}
194
243
244
+ aReq .New = key
245
+
195
246
http .SetCookie (rw , cookie )
196
247
197
248
redirect := state .Redirect
@@ -362,7 +413,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
362
413
picture , _ = pictureRaw .(string )
363
414
}
364
415
365
- cookie , err := api .oauthLogin (r , oauthLoginParams {
416
+ cookie , _ , err := api .oauthLogin (r , oauthLoginParams {
366
417
State : state ,
367
418
LinkedID : oidcLinkedID (idToken ),
368
419
LoginType : database .LoginTypeOIDC ,
@@ -397,6 +448,8 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
397
448
}
398
449
399
450
type oauthLoginParams struct {
451
+ User database.User
452
+ Link database.UserLink
400
453
State httpmw.OAuth2State
401
454
LinkedID string
402
455
LoginType database.LoginType
@@ -423,7 +476,7 @@ func (e httpError) Error() string {
423
476
return e .msg
424
477
}
425
478
426
- func (api * API ) oauthLogin (r * http.Request , params oauthLoginParams ) (* http.Cookie , error ) {
479
+ func (api * API ) oauthLogin (r * http.Request , params oauthLoginParams ) (* http.Cookie , database. APIKey , error ) {
427
480
var (
428
481
ctx = r .Context ()
429
482
user database.User
@@ -435,10 +488,13 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
435
488
err error
436
489
)
437
490
438
- user , link , err = findLinkedUser (ctx , tx , params .LinkedID , params .Email )
439
- if err != nil {
440
- return xerrors .Errorf ("find linked user: %w" , err )
441
- }
491
+ user = params .User
492
+ link = params .Link
493
+ //
494
+ // user, link, err = findLinkedUser(ctx, tx, params.LinkedID, params.Email)
495
+ // if err != nil {
496
+ // return xerrors.Errorf("find linked user: %w", err)
497
+ // }
442
498
443
499
if user .ID == uuid .Nil && ! params .AllowSignups {
444
500
return httpError {
@@ -599,19 +655,19 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
599
655
return nil
600
656
}, nil )
601
657
if err != nil {
602
- return nil , xerrors .Errorf ("in tx: %w" , err )
658
+ return nil , database. APIKey {}, xerrors .Errorf ("in tx: %w" , err )
603
659
}
604
660
605
- cookie , _ , err := api .createAPIKey (ctx , createAPIKeyParams {
661
+ cookie , key , err := api .createAPIKey (ctx , createAPIKeyParams {
606
662
UserID : user .ID ,
607
663
LoginType : params .LoginType ,
608
664
RemoteAddr : r .RemoteAddr ,
609
665
})
610
666
if err != nil {
611
- return nil , xerrors .Errorf ("create API key: %w" , err )
667
+ return nil , database. APIKey {}, xerrors .Errorf ("create API key: %w" , err )
612
668
}
613
669
614
- return cookie , nil
670
+ return cookie , * key , nil
615
671
}
616
672
617
673
// githubLinkedID returns the unique ID for a GitHub user.
0 commit comments