(externalAuthConfig *externalauth.Config)
| 257 | } |
| 258 | |
| 259 | func (api *API) externalAuthCallback(externalAuthConfig *externalauth.Config) http.HandlerFunc { |
| 260 | return func(rw http.ResponseWriter, r *http.Request) { |
| 261 | var ( |
| 262 | ctx = r.Context() |
| 263 | state = httpmw.OAuth2(r) |
| 264 | apiKey = httpmw.APIKey(r) |
| 265 | ) |
| 266 | |
| 267 | extra, err := externalAuthConfig.GenerateTokenExtra(state.Token) |
| 268 | if err != nil { |
| 269 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 270 | Message: "Failed to generate token extra.", |
| 271 | Detail: err.Error(), |
| 272 | }) |
| 273 | return |
| 274 | } |
| 275 | _, err = api.Database.GetExternalAuthLink(ctx, database.GetExternalAuthLinkParams{ |
| 276 | ProviderID: externalAuthConfig.ID, |
| 277 | UserID: apiKey.UserID, |
| 278 | }) |
| 279 | if err != nil { |
| 280 | if !errors.Is(err, sql.ErrNoRows) { |
| 281 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 282 | Message: "Failed to get external auth link.", |
| 283 | Detail: err.Error(), |
| 284 | }) |
| 285 | return |
| 286 | } |
| 287 | |
| 288 | _, err = api.Database.InsertExternalAuthLink(ctx, database.InsertExternalAuthLinkParams{ |
| 289 | ProviderID: externalAuthConfig.ID, |
| 290 | UserID: apiKey.UserID, |
| 291 | CreatedAt: dbtime.Now(), |
| 292 | UpdatedAt: dbtime.Now(), |
| 293 | OAuthAccessToken: state.Token.AccessToken, |
| 294 | OAuthAccessTokenKeyID: sql.NullString{}, // dbcrypt will set as required |
| 295 | OAuthRefreshToken: state.Token.RefreshToken, |
| 296 | OAuthRefreshTokenKeyID: sql.NullString{}, // dbcrypt will set as required |
| 297 | OAuthExpiry: state.Token.Expiry, |
| 298 | OAuthExtra: extra, |
| 299 | }) |
| 300 | if err != nil { |
| 301 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 302 | Message: "Failed to insert external auth link.", |
| 303 | Detail: err.Error(), |
| 304 | }) |
| 305 | return |
| 306 | } |
| 307 | } else { |
| 308 | _, err = api.Database.UpdateExternalAuthLink(ctx, database.UpdateExternalAuthLinkParams{ |
| 309 | ProviderID: externalAuthConfig.ID, |
| 310 | UserID: apiKey.UserID, |
| 311 | UpdatedAt: dbtime.Now(), |
| 312 | OAuthAccessToken: state.Token.AccessToken, |
| 313 | OAuthAccessTokenKeyID: sql.NullString{}, // dbcrypt will update as required |
| 314 | OAuthRefreshToken: state.Token.RefreshToken, |
| 315 | OAuthRefreshTokenKeyID: sql.NullString{}, // dbcrypt will update as required |
| 316 | OAuthExpiry: state.Token.Expiry, |
no test coverage detected