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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use self::auth::{AuthErrorCode, AuthErrorResponse, AuthResponse};
pub mod auth;
pub mod token;
#[cfg(test)]
pub mod tests;
pub struct OAuthManager {}
#[derive(Copy, Clone)]
pub enum AuthErrorType {
MissingParameter = 10000,
MissingParameterClientId = 10001,
MissingParameterScope = 10002,
MissingParameterRedirectUri = 10003,
MissingParameterResponseType = 10004,
MissingParameterCodeChallenge = 10005,
MaxValue = 65535,
}
impl AuthErrorType {
fn as_u16(&mut self) -> u16 {
*self as u16
}
}
fn create_error(
error: AuthErrorCode,
error_type_code: AuthErrorType,
error_description: &str,
callback_uri: Option<String>,
) -> AuthErrorResponse {
let mut error_message = String::from("");
let error_type = format!("WSA{:?}", error_type_code.to_owned().as_u16());
error_message.push_str(error_type.to_owned().as_str());
error_message.push_str(": ");
error_message.push_str(error_description);
self::auth::AuthErrorResponse {
error: Some(error),
error_description: Some(error_message),
callback_uri: callback_uri.to_owned(),
}
}
fn validate_request(req: &auth::AuthRequest) -> Option<AuthErrorResponse> {
let mut callback_uri = String::from("http://192.168.88.220:9000/error");
if req.redirect_uri.is_some() {
callback_uri = req.redirect_uri.to_owned().unwrap();
}
if req.client_id.is_none() {
return Some(create_error(
AuthErrorCode::InvalidRequest,
AuthErrorType::MissingParameterClientId,
"Request is missing a required parameter, `client_id`",
Some(callback_uri),
));
}
if req.scope.is_none() {
return Some(create_error(
AuthErrorCode::InvalidRequest,
AuthErrorType::MissingParameterResponseType,
"Request is missing a required parameter, `scope`",
Some(callback_uri),
));
}
if req.redirect_uri.is_none() {
return Some(create_error(
AuthErrorCode::InvalidRequest,
AuthErrorType::MissingParameterRedirectUri,
"Request is missing a required parameter, `redirect_uri`",
Some(callback_uri),
));
}
if req.response_type.is_none() {
return Some(create_error(
AuthErrorCode::InvalidRequest,
AuthErrorType::MissingParameterScope,
"Request is missing a required parameter, `response_type`",
Some(callback_uri),
));
}
if !req.code_challenge_method.is_none() && req.code_challenge.is_none() {
return Some(create_error(
AuthErrorCode::InvalidRequest,
AuthErrorType::MissingParameterCodeChallenge,
"Request is missing a required parameter, `code_challenge`",
Some(callback_uri),
));
}
return Option::None;
}
impl OAuthManager {
pub fn new() -> OAuthManager {
OAuthManager {}
}
pub fn authorize(
&mut self,
req: &auth::AuthRequest,
) -> Result<AuthResponse, AuthErrorResponse> {
let validation_response = validate_request(req);
if !validation_response.is_none() {
return Err(validation_response.unwrap());
}
Ok(AuthResponse {
code: Option::None,
id_token: Option::None,
state: Option::None,
callback_uri: Option::None,
})
}
}