Wait to create access token until we impersonate a user

We need to wait to create the token until we decide on whether we're impersonating a different user, otherwise we'll create the token for the original user and the impersonated user won't be able to authenticate.
This commit is contained in:
Nathan Sobo 2022-04-04 15:07:55 -06:00
parent cbf6d827db
commit 6a1be11aa6

View file

@ -111,7 +111,6 @@ async fn create_access_token(request: Request) -> tide::Result {
.get_user_by_github_login(request.param("github_login")?) .get_user_by_github_login(request.param("github_login")?)
.await? .await?
.ok_or_else(|| surf::Error::from_str(StatusCode::NotFound, "user not found"))?; .ok_or_else(|| surf::Error::from_str(StatusCode::NotFound, "user not found"))?;
let access_token = auth::create_access_token(request.db().as_ref(), user.id).await?;
#[derive(Deserialize)] #[derive(Deserialize)]
struct QueryParams { struct QueryParams {
@ -123,9 +122,6 @@ async fn create_access_token(request: Request) -> tide::Result {
surf::Error::from_str(StatusCode::UnprocessableEntity, "invalid query params") surf::Error::from_str(StatusCode::UnprocessableEntity, "invalid query params")
})?; })?;
let encrypted_access_token =
auth::encrypt_access_token(&access_token, query_params.public_key.clone())?;
let mut user_id = user.id; let mut user_id = user.id;
if let Some(impersonate) = query_params.impersonate { if let Some(impersonate) = query_params.impersonate {
if user.admin { if user.admin {
@ -151,6 +147,10 @@ async fn create_access_token(request: Request) -> tide::Result {
} }
} }
let access_token = auth::create_access_token(request.db().as_ref(), user_id).await?;
let encrypted_access_token =
auth::encrypt_access_token(&access_token, query_params.public_key.clone())?;
Ok(tide::Response::builder(StatusCode::Ok) Ok(tide::Response::builder(StatusCode::Ok)
.body(json!({"user_id": user_id, "encrypted_access_token": encrypted_access_token})) .body(json!({"user_id": user_id, "encrypted_access_token": encrypted_access_token}))
.build()) .build())