4GPLIIZEZEFNPTRC5VHM35FS5OSAEKE5TFW6BLUWMZ4BUULULW7QC
Err(HashMap<String, String>),
Err(WclErrResponse),
Unk(HashMap<String, String>),
}
#[derive(Debug, Error, Clone)]
pub enum Error {
#[error("Token has been revoked on the WCL end.")]
TokenRevoked,
}
fn process_wcl_response(res: TokenResponse, scope: &str) -> Result<WclToken> {
match res {
TokenResponse::Ok(token) => Ok(token),
TokenResponse::Err(err) => match err.hint.as_str() {
"Authorization code has been revoked" => Err(Error::TokenRevoked.into()),
_ => {
error!(
"an error occurred while {}ing a user token: {:?}",
scope, err
);
return Err(anyhow!("unable to refresh token"));
}
},
TokenResponse::Unk(err) => {
error!(
"un unknown error occurred while {}ing a user token: {:?}",
scope, err
);
return Err(anyhow!("unable to {} token", scope));
}
}
Ok(TokenResponse::Ok(token)) => Ok(token),
Ok(TokenResponse::Err(err)) => {
error!("an error occurred while acquiring a user token: {:?}", err);
return Err(anyhow!("unable to acquire token"));
}
Ok(res) => process_wcl_response(res, "acquire"),
let token = refresh_token(client, secrets, &refresh).await?;
diesel::update(auth::auth_codes.filter(auth::team.eq(team).and(auth::zone.eq(zone))))
.set((
auth::access_token.eq(&token.access_token),
auth::refresh_token.eq(token.refresh_token.as_ref().unwrap_or(&refresh)),
auth::expiration.eq(Utc::now() + Duration::seconds(token.expires_in as i64)),
))
.execute(&*con)?;
token
match refresh_token(client, secrets, &refresh).await {
Ok(token) => {
diesel::update(
auth::auth_codes.filter(auth::team.eq(team).and(auth::zone.eq(zone))),
)
.set((
auth::access_token.eq(&token.access_token),
auth::refresh_token.eq(token.refresh_token.as_ref().unwrap_or(&refresh)),
auth::expiration
.eq(Utc::now() + Duration::seconds(token.expires_in as i64)),
))
.execute(&*con)?;
token
}
Err(e) => match e.downcast_ref::<Error>() {
Some(Error::TokenRevoked) => {
info!("user revoked token for {} on WCL. deleting token.", team);
diesel::delete(
auth::auth_codes.filter(auth::team.eq(team).and(auth::zone.eq(zone))),
)
.execute(&*con)?;
return Ok(None);
}
None => return Err(e),
},
}
let token = crate::user_auth::token_for_code(&client, &secrets, &code).await?;
let token = match crate::user_auth::token_for_code(&client, &secrets, &code).await {
Ok(t) => t,
Err(e) => {
match e.downcast_ref::<crate::user_auth::Error>() {
Some(crate::user_auth::Error::TokenRevoked) => {
// not allowed to use this token. possible race condition with WCL?
warn!("token revoked during store_code handling for {}", team);
return Ok(false);
}
None => return Err(e.into()),
}
}
};
let token = crate::user_auth::token_for_code(&client, &secrets, &code).await?;
let token = match crate::user_auth::token_for_code(&client, &secrets, &code).await {
Ok(t) => t,
Err(e) => {
match e.downcast_ref::<crate::user_auth::Error>() {
Some(crate::user_auth::Error::TokenRevoked) => {
// not allowed to use this token. possible race condition with WCL?
warn!("token revoked during revoke_team handling for {}", team);
return Ok(false);
}
None => return Err(e.into()),
}
}
};