|
| 1 | +use crate::error::VssError; |
| 2 | +use async_trait::async_trait; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::string::ToString; |
| 5 | + |
| 6 | +/// Response returned for [`Authorizer`] request if user is authenticated and authorized. |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +pub struct AuthResponse { |
| 9 | + /// A `user_token` identifying the authenticated and authorized user. |
| 10 | + pub user_token: String, |
| 11 | +} |
| 12 | + |
| 13 | +/// Interface for authorizer that is run before executing every request. |
| 14 | +#[async_trait] |
| 15 | +pub trait Authorizer: Send + Sync { |
| 16 | + /// Verifies authentication and authorization based on request headers. |
| 17 | + /// Returns [`AuthResponse`] for an authenticated and authorized user or [`VssError::AuthError`] |
| 18 | + /// for an unauthorized request. |
| 19 | + async fn verify(&self, headers_map: &HashMap<String, String>) |
| 20 | + -> Result<AuthResponse, VssError>; |
| 21 | +} |
| 22 | + |
| 23 | +/// A no-operation authorizer, which lets any user-request go through. |
| 24 | +pub struct NoopAuthorizer {} |
| 25 | + |
| 26 | +const UNAUTHENTICATED_USER: &str = "unauth-user"; |
| 27 | + |
| 28 | +#[async_trait] |
| 29 | +impl Authorizer for NoopAuthorizer { |
| 30 | + async fn verify( |
| 31 | + &self, _headers_map: &HashMap<String, String>, |
| 32 | + ) -> Result<AuthResponse, VssError> { |
| 33 | + Ok(AuthResponse { user_token: UNAUTHENTICATED_USER.to_string() }) |
| 34 | + } |
| 35 | +} |
0 commit comments