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