use std::sync::Arc;
#[cfg(feature = "biome-credentials")]
use jsonwebtoken::{decode, DecodingKey, Validation};
use crate::actix_web::HttpRequest;
#[cfg(feature = "biome-credentials")]
use crate::biome::credentials::rest_api::resources::authorize::AuthorizationResult;
use crate::rest_api::secrets::SecretManager;
#[cfg(feature = "biome-credentials")]
use crate::rest_api::{actix_web_1::get_authorization_token, sessions::Claims};
#[cfg(feature = "biome-credentials")]
pub(crate) fn authorize_user(
request: &HttpRequest,
secret_manager: &Arc<dyn SecretManager>,
validation: &Validation,
) -> AuthorizationResult {
let token = match get_authorization_token(request) {
Ok(token) => match token.split_once(':').map(|x| x.1) {
Some(token) => token.to_string(),
None => {
debug!("Invalid token; should be in the format 'Biome:<JWT>'");
return AuthorizationResult::Unauthorized;
}
},
Err(err) => {
debug!("Failed to get token: {}", err);
return AuthorizationResult::Unauthorized;
}
};
validate_claims(&token, secret_manager, validation)
}
#[cfg(feature = "biome-credentials")]
pub(crate) fn validate_claims(
token: &str,
secret_manager: &Arc<dyn SecretManager>,
validation: &Validation,
) -> AuthorizationResult {
let secret = match secret_manager.secret() {
Ok(secret) => secret,
Err(err) => {
debug!("Failed to fetch secret {}", err);
return AuthorizationResult::Failed;
}
};
match decode::<Claims>(
token,
&DecodingKey::from_secret(secret.as_ref()),
validation,
) {
Ok(claims) => AuthorizationResult::Authorized(claims.claims),
Err(err) => {
debug!("Invalid token: {}", err);
AuthorizationResult::Unauthorized
}
}
}