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
use actix_web::http::{HeaderMap, HeaderValue};
use async_trait::async_trait;
use crate::authentication::error::error_type::AuthenticationError;
use crate::authentication::scheme::authentication::Authentication;
#[async_trait]
pub trait AuthorizationHeaderExtractor: Send + Sync {
async fn extract_token(
&self,
request: &HeaderMap,
) -> Result<Box<dyn Authentication>, AuthenticationError>;
}
pub fn extract_auth_header(
header: &HeaderValue,
auth_scheme: &str,
header_length: usize,
) -> Result<String, AuthenticationError> {
if header.len() < header_length {
return Err(AuthenticationError::InvalidAuthorizationHeader);
}
let token: &str;
if let Ok(header_str) = header.to_str() {
let mut parts = header_str.splitn(2, ' ');
match parts.next() {
Some(scheme) if scheme == auth_scheme => (),
_ => return Err(AuthenticationError::InvalidAuthorizationHeader),
}
token = parts
.next()
.ok_or(AuthenticationError::InvalidAuthorizationHeader)?
} else {
return Err(AuthenticationError::InvalidAuthorizationHeader);
}
Ok(token.to_string())
}