Skip to content

Commit

Permalink
add error for login with unlinked snap account
Browse files Browse the repository at this point in the history
  • Loading branch information
samankittani committed Jun 17, 2024
1 parent 8b95bbc commit 511a883
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
3 changes: 3 additions & 0 deletions crates/cloud/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub enum UserError {
SnapConnectionError,
#[display(fmt = "Account already linked to NetsBlox user.")]
AccountAlreadyLinkedError,
#[display(fmt = "Account not linked to NetsBlox user. Please link account to NetsBlox user.")]
LinkedAccountNotFoundError,
#[display(fmt = "Invalid account type.")]
InvalidAccountTypeError,
#[display(fmt = "Login from Tor not allowed.")]
Expand Down Expand Up @@ -189,6 +191,7 @@ impl error::ResponseError for UserError {
| Self::InviteNotFoundError
| Self::MagicLinkNotFoundError
| Self::UserNotFoundError
| Self::LinkedAccountNotFoundError
| Self::FriendNotFoundError
| Self::OAuthClientNotFoundError
| Self::OAuthTokenNotFoundError
Expand Down
31 changes: 3 additions & 28 deletions crates/cloud/src/users/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,11 @@ pub async fn login(users: &Collection<User>, credentials: Credentials) -> Result
};

let query = doc! {"linkedAccounts": {"$elemMatch": &account}};
let user_opt = users
let user = users
.find_one(query, None)
.await
.map_err(InternalError::DatabaseConnectionError)?;

let user = if let Some(user) = user_opt {
user
} else {
let cookie = response
.cookies()
.next()
.ok_or(UserError::SnapConnectionError)?;
let url = &format!("https://snap.berkeley.edu/api/v1/users/{}", username);

let client = reqwest::Client::new();
let user_data = client
.request(Method::GET, url)
.header("Cookie", format!("{}={}", cookie.name(), cookie.value()))
.send()
.await
.map_err(|_err| UserError::SnapConnectionError)?
.json::<SnapUser>()
.await
.map_err(|_err| UserError::SnapConnectionError)?;

// FIX: snap route is not available anymore, snap does not provide email addresses
// TODO: ensure email isn't banned?

create_account(users, user_data.email, &account).await?
};
.map_err(InternalError::DatabaseConnectionError)?
.ok_or(UserError::LinkedAccountNotFoundError)?;

Ok(user)
}
Expand Down

0 comments on commit 511a883

Please sign in to comment.