|
| 1 | +use std::sync::{Arc, OnceLock}; |
| 2 | + |
| 3 | +use biscuit_auth::macros::authorizer; |
| 4 | +use biscuit_auth::{Authorizer, Biscuit, RootKeyProvider}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +pub type AuthorizationToken = Biscuit; |
| 8 | + |
| 9 | +tokio::task_local! { |
| 10 | + pub static AUTHORIZATION_TOKEN: AuthorizationToken; |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(thiserror::Error, Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)] |
| 14 | +pub enum AuthorizationError { |
| 15 | + #[error("authorization token missing")] |
| 16 | + AuthorizationTokenMissing, |
| 17 | + #[error("invalid token")] |
| 18 | + InvalidToken, |
| 19 | + #[error("permission denied")] |
| 20 | + PermissionDenied, |
| 21 | +} |
| 22 | + |
| 23 | +const AUTHORIZATION_VALUE_PREFIX: &str = "Bearer "; |
| 24 | + |
| 25 | +fn default_operation_authorizer<T: ?Sized>( |
| 26 | + auth_token: &AuthorizationToken, |
| 27 | +) -> Result<Authorizer, AuthorizationError> { |
| 28 | + let request_type = std::any::type_name::<T>(); |
| 29 | + let operation: &str = request_type.strip_suffix("Request").unwrap(); |
| 30 | + let mut authorizer: Authorizer = authorizer!( |
| 31 | + r#" |
| 32 | + operation({operation}); |
| 33 | +
|
| 34 | + // We generate the actual user role, by doing an union of the rights granted via roles. |
| 35 | + user_right($operation) <- role($role), right($role, $operation); |
| 36 | + user_right($operation, $resource) <- role($role), right($role, $operation, $resource); |
| 37 | + user_right($operation) <- role("root"), operation($operation); |
| 38 | + user_right($operation, $resource) <- role("root"), operation($operation), resource($resource); |
| 39 | +
|
| 40 | + // Finally we check that we have access to index1 and index2. |
| 41 | + check all operation($operation), right($operation); |
| 42 | +
|
| 43 | + allow if true; |
| 44 | + "# |
| 45 | + ); |
| 46 | + authorizer.set_time(); |
| 47 | + authorizer.add_token(auth_token)?; |
| 48 | + Ok(authorizer) |
| 49 | +} |
| 50 | + |
| 51 | +pub trait Authorization { |
| 52 | + fn attenuate( |
| 53 | + &self, |
| 54 | + auth_token: AuthorizationToken, |
| 55 | + ) -> Result<AuthorizationToken, AuthorizationError>; |
| 56 | + fn authorizer( |
| 57 | + &self, |
| 58 | + auth_token: &AuthorizationToken, |
| 59 | + ) -> Result<Authorizer, AuthorizationError> { |
| 60 | + default_operation_authorizer::<Self>(auth_token) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub trait StreamAuthorization { |
| 65 | + fn attenuate( |
| 66 | + auth_token: AuthorizationToken, |
| 67 | + ) -> std::result::Result<AuthorizationToken, AuthorizationError> { |
| 68 | + Ok(auth_token) |
| 69 | + } |
| 70 | + fn authorizer( |
| 71 | + auth_token: &AuthorizationToken, |
| 72 | + ) -> std::result::Result<Authorizer, AuthorizationError> { |
| 73 | + default_operation_authorizer::<Self>(&auth_token) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +static ROOT_KEY_PROVIDER: OnceLock<Arc<dyn RootKeyProvider + Sync + Send>> = OnceLock::new(); |
| 78 | + |
| 79 | +pub fn set_root_key_provider(key_provider: Arc<dyn RootKeyProvider + Sync + Send>) { |
| 80 | + if ROOT_KEY_PROVIDER.set(key_provider).is_err() { |
| 81 | + tracing::error!("root key provider was already initialized"); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn get_root_key_provider() -> Arc<dyn RootKeyProvider> { |
| 86 | + ROOT_KEY_PROVIDER |
| 87 | + .get() |
| 88 | + .expect("root key provider should have been initialized beforehand") |
| 89 | + .clone() |
| 90 | +} |
| 91 | + |
| 92 | +impl From<biscuit_auth::error::Token> for AuthorizationError { |
| 93 | + fn from(_token_error: biscuit_auth::error::Token) -> AuthorizationError { |
| 94 | + AuthorizationError::InvalidToken |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +pub fn get_auth_token( |
| 99 | + req_metadata: &tonic::metadata::MetadataMap, |
| 100 | +) -> Result<AuthorizationToken, AuthorizationError> { |
| 101 | + let authorization_header_value: &str = req_metadata |
| 102 | + .get(http::header::AUTHORIZATION.as_str()) |
| 103 | + .ok_or(AuthorizationError::AuthorizationTokenMissing)? |
| 104 | + .to_str() |
| 105 | + .map_err(|_| AuthorizationError::InvalidToken)?; |
| 106 | + let authorization_token_str: &str = authorization_header_value |
| 107 | + .strip_prefix(AUTHORIZATION_VALUE_PREFIX) |
| 108 | + .ok_or(AuthorizationError::InvalidToken)?; |
| 109 | + let biscuit: Biscuit = Biscuit::from_base64(authorization_token_str, get_root_key_provider())?; |
| 110 | + Ok(biscuit) |
| 111 | +} |
| 112 | + |
| 113 | +pub fn set_auth_token( |
| 114 | + auth_token: &AuthorizationToken, |
| 115 | + req_metadata: &mut tonic::metadata::MetadataMap, |
| 116 | +) { |
| 117 | + let authorization_header_value = format!("{AUTHORIZATION_VALUE_PREFIX}{auth_token}"); |
| 118 | + req_metadata.insert( |
| 119 | + http::header::AUTHORIZATION.as_str(), |
| 120 | + authorization_header_value.parse().unwrap(), |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +pub fn authorize<R: Authorization>( |
| 125 | + req: &R, |
| 126 | + auth_token: &Biscuit, |
| 127 | +) -> Result<(), AuthorizationError> { |
| 128 | + let mut authorizer = req.authorizer(auth_token)?; |
| 129 | + authorizer.add_token(&auth_token)?; |
| 130 | + authorizer.authorize()?; |
| 131 | + Ok(()) |
| 132 | +} |
| 133 | + |
| 134 | +pub fn build_tonic_stream_request_with_auth_token<R>( |
| 135 | + req: R, |
| 136 | +) -> Result<tonic::Request<R>, AuthorizationError> { |
| 137 | + AUTHORIZATION_TOKEN |
| 138 | + .try_with(|token| { |
| 139 | + let mut request = tonic::Request::new(req); |
| 140 | + set_auth_token(token, request.metadata_mut()); |
| 141 | + Ok(request) |
| 142 | + }) |
| 143 | + .unwrap_or(Err(AuthorizationError::AuthorizationTokenMissing)) |
| 144 | +} |
| 145 | + |
| 146 | +pub fn build_tonic_request_with_auth_token<R: Authorization>( |
| 147 | + req: R, |
| 148 | +) -> Result<tonic::Request<R>, AuthorizationError> { |
| 149 | + AUTHORIZATION_TOKEN |
| 150 | + .try_with(|token| { |
| 151 | + let mut request = tonic::Request::new(req); |
| 152 | + set_auth_token(token, request.metadata_mut()); |
| 153 | + Ok(request) |
| 154 | + }) |
| 155 | + .unwrap_or(Err(AuthorizationError::AuthorizationTokenMissing)) |
| 156 | +} |
| 157 | + |
| 158 | +pub fn authorize_stream<R: StreamAuthorization>( |
| 159 | + auth_token: &Biscuit, |
| 160 | +) -> Result<(), AuthorizationError> { |
| 161 | + let mut authorizer = R::authorizer(auth_token)?; |
| 162 | + authorizer.add_token(&auth_token)?; |
| 163 | + authorizer.authorize()?; |
| 164 | + Ok(()) |
| 165 | +} |
| 166 | + |
| 167 | +impl From<AuthorizationError> for tonic::Status { |
| 168 | + fn from(authorization_error: AuthorizationError) -> tonic::Status { |
| 169 | + match authorization_error { |
| 170 | + AuthorizationError::AuthorizationTokenMissing => { |
| 171 | + tonic::Status::unauthenticated("Authorization token missing") |
| 172 | + } |
| 173 | + AuthorizationError::InvalidToken => { |
| 174 | + tonic::Status::unauthenticated("Invalid authorization token") |
| 175 | + } |
| 176 | + AuthorizationError::PermissionDenied => { |
| 177 | + tonic::Status::permission_denied("Permission denied") |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +#[cfg(test)] |
| 184 | +mod tests { |
| 185 | + use super::*; |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn test_auth_token() { |
| 189 | + let mut req_metadata = tonic::metadata::MetadataMap::new(); |
| 190 | + let auth_token = "test_token".to_string(); |
| 191 | + set_auth_token(&auth_token, &mut req_metadata); |
| 192 | + let auth_token_retrieved = get_auth_token(&req_metadata).unwrap(); |
| 193 | + assert_eq!(auth_token_retrieved, auth_token); |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn test_auth_token_missing() { |
| 198 | + let req_metadata = tonic::metadata::MetadataMap::new(); |
| 199 | + let missing_error = get_auth_token(&req_metadata).unwrap_err(); |
| 200 | + assert!(matches!( |
| 201 | + missing_error, |
| 202 | + AuthorizationError::AuthorizationTokenMissing |
| 203 | + )); |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn test_auth_token_invalid() { |
| 208 | + let mut req_metadata = tonic::metadata::MetadataMap::new(); |
| 209 | + req_metadata.insert( |
| 210 | + http::header::AUTHORIZATION.as_str(), |
| 211 | + "some_token".parse().unwrap(), |
| 212 | + ); |
| 213 | + let missing_error = get_auth_token(&req_metadata).unwrap_err(); |
| 214 | + assert!(matches!(missing_error, AuthorizationError::InvalidToken)); |
| 215 | + } |
| 216 | +} |
0 commit comments