r1_api_layer/
auth.rs

1use crate::server::Authorization;
2use serde::{Deserialize, Serialize};
3use std::collections::BTreeSet;
4use swagger::{
5    auth::{Basic, Bearer},
6    ApiError,
7};
8
9#[derive(Debug, Serialize, Deserialize)]
10pub struct Claims {
11    pub sub: String,
12    pub iss: String,
13    pub aud: String,
14    pub company: String,
15    pub exp: u64,
16    pub scopes: String,
17}
18
19pub trait AuthenticationApi {
20    /// Method should be implemented (see example-code) to map Bearer-token to an Authorization
21    fn bearer_authorization(&self, token: &Bearer) -> Result<Authorization, ApiError>;
22
23    /// Method should be implemented (see example-code) to map ApiKey to an Authorization
24    fn apikey_authorization(&self, token: &str) -> Result<Authorization, ApiError>;
25
26    /// Method should be implemented (see example-code) to map Basic (Username:password) to an Authorization
27    fn basic_authorization(&self, basic: &Basic) -> Result<Authorization, ApiError>;
28}
29
30// Implement it for AllowAllAuthenticator (dummy is needed, but should not used as we have Bearer authorization)
31use swagger::auth::{AllowAllAuthenticator, RcBound, Scopes};
32
33fn dummy_authorization() -> Authorization {
34    // Is called when MakeAllowAllAuthenticator is added to the stack. This is not needed as we have Bearer-authorization in the example-code.
35    // However, if you want to use it anyway this can not be unimplemented, so dummy implementation added.
36    // unimplemented!()
37    Authorization {
38        subject: "Dummy".to_owned(),
39        scopes: Scopes::Some(BTreeSet::new()), // create an empty scope, as this should not be used
40        issuer: None,
41    }
42}
43
44impl<T, RC> AuthenticationApi for AllowAllAuthenticator<T, RC>
45where
46    RC: RcBound,
47    RC::Result: Send + 'static,
48{
49    /// Get method to map Bearer-token to an Authorization
50    fn bearer_authorization(&self, _token: &Bearer) -> Result<Authorization, ApiError> {
51        Ok(dummy_authorization())
52    }
53
54    /// Get method to map api-key to an Authorization
55    fn apikey_authorization(&self, _apikey: &str) -> Result<Authorization, ApiError> {
56        Ok(dummy_authorization())
57    }
58
59    /// Get method to map basic token to an Authorization
60    fn basic_authorization(&self, _basic: &Basic) -> Result<Authorization, ApiError> {
61        Ok(dummy_authorization())
62    }
63}