(
&self,
request: Request<api::OAuth2LoginRequest>,
)
| 537 | } |
| 538 | |
| 539 | async fn o_auth2_login( |
| 540 | &self, |
| 541 | request: Request<api::OAuth2LoginRequest>, |
| 542 | ) -> Result<Response<api::OAuth2LoginResponse>, Status> { |
| 543 | let req = request.get_ref(); |
| 544 | let conf = config::get(); |
| 545 | |
| 546 | let oauth_user = oauth2::get_user(&req.code, &req.state) |
| 547 | .await |
| 548 | .map_err(|e| e.status())?; |
| 549 | |
| 550 | let email_verified = |
| 551 | oauth_user.email_verified || conf.user_authentication.oauth2.assume_email_verified; |
| 552 | |
| 553 | if !email_verified { |
| 554 | return Err(Status::failed_precondition( |
| 555 | "email address must be verified before you can login", |
| 556 | )); |
| 557 | } |
| 558 | |
| 559 | // try to get user by external id |
| 560 | let mut u: Option<user::User> = |
| 561 | match user::get_by_external_id(&oauth_user.external_id).await { |
| 562 | Ok(v) => Some(v), |
| 563 | Err(e) => match e { |
| 564 | Error::NotFound(_) => None, |
| 565 | _ => { |
| 566 | return Err(e.status()); |
| 567 | } |
| 568 | }, |
| 569 | }; |
| 570 | |
| 571 | if u.is_none() { |
| 572 | u = match user::get_by_email(&oauth_user.email).await { |
| 573 | Ok(mut v) => { |
| 574 | v.external_id = Some(oauth_user.external_id.clone()); |
| 575 | Some(v) |
| 576 | } |
| 577 | Err(e) => match e { |
| 578 | Error::NotFound(_) => None, |
| 579 | _ => { |
| 580 | return Err(e.status()); |
| 581 | } |
| 582 | }, |
| 583 | }; |
| 584 | } |
| 585 | |
| 586 | // register the user (if enabled) |
| 587 | if u.is_none() && conf.user_authentication.oauth2.registration_enabled { |
| 588 | u = Some( |
| 589 | self.create_and_provision_user( |
| 590 | &oauth_user.external_id, |
| 591 | &oauth_user.email, |
| 592 | email_verified, |
| 593 | &oauth_user, |
| 594 | ) |
| 595 | .await |
| 596 | .map_err(|e| e.status())?, |
nothing calls this directly
no test coverage detected