Skip to content

Commit 3e527d6

Browse files
committed
Add Authorizer Api with NoopAuthorizer.
1 parent a4605f7 commit 3e527d6

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

rust/api/src/auth.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

rust/api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#![deny(rustdoc::private_intra_doc_links)]
1010
#![deny(missing_docs)]
1111

12+
/// Contains interface for authorizer that is run before every request, and its corresponding implementations.
13+
pub mod auth;
1214
/// Implements the error type ([`error::VssError`]) which is eventually converted to [`ErrorResponse`] and returned to the client.
1315
///
1416
/// [`ErrorResponse`]: types::ErrorResponse

0 commit comments

Comments
 (0)