openauth_oauth/oauth2/
validate_authorization_code.rs1use std::collections::BTreeMap;
2
3use super::error::OAuthError;
4use super::request::{
5 apply_client_authentication, post_form, ClientAuthentication, OAuthFormRequest,
6};
7use super::tokens::{get_oauth2_tokens, OAuth2Tokens, ProviderOptions};
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct AuthorizationCodeRequest {
11 pub code: String,
12 pub redirect_uri: String,
13 pub options: ProviderOptions,
14 pub code_verifier: Option<String>,
15 pub device_id: Option<String>,
16 pub authentication: ClientAuthentication,
17 pub headers: BTreeMap<String, String>,
18 pub additional_params: BTreeMap<String, String>,
19 pub override_params: BTreeMap<String, String>,
20 pub resource: Vec<String>,
21}
22
23impl Default for AuthorizationCodeRequest {
24 fn default() -> Self {
25 Self {
26 code: String::new(),
27 redirect_uri: String::new(),
28 options: ProviderOptions::default(),
29 code_verifier: None,
30 device_id: None,
31 authentication: ClientAuthentication::Post,
32 headers: BTreeMap::new(),
33 additional_params: BTreeMap::new(),
34 override_params: BTreeMap::new(),
35 resource: Vec::new(),
36 }
37 }
38}
39
40impl AuthorizationCodeRequest {
41 pub fn try_new(
42 code: impl Into<String>,
43 redirect_uri: impl Into<String>,
44 options: ProviderOptions,
45 ) -> Result<Self, OAuthError> {
46 let code = code.into();
47 if code.is_empty() {
48 return Err(OAuthError::InvalidConfiguration(
49 "authorization code cannot be empty".to_owned(),
50 ));
51 }
52 let redirect_uri = redirect_uri.into();
53 url::Url::parse(options.redirect_uri.as_deref().unwrap_or(&redirect_uri))?;
54 Ok(Self {
55 code,
56 redirect_uri,
57 options,
58 ..Self::default()
59 })
60 }
61
62 pub fn code_verifier(mut self, code_verifier: impl Into<String>) -> Self {
63 self.code_verifier = Some(code_verifier.into());
64 self
65 }
66
67 pub fn authentication(mut self, authentication: ClientAuthentication) -> Self {
68 self.authentication = authentication;
69 self
70 }
71
72 pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
73 self.headers.insert(key.into(), value.into());
74 self
75 }
76
77 pub fn additional_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
78 self.additional_params.insert(key.into(), value.into());
79 self
80 }
81
82 pub fn override_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
83 self.override_params.insert(key.into(), value.into());
84 self
85 }
86
87 pub fn resource(mut self, resource: impl Into<String>) -> Self {
88 self.resource.push(resource.into());
89 self
90 }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct ClientTokenRequest<T> {
95 pub token_endpoint: String,
96 pub request: T,
97}
98
99pub fn create_authorization_code_request(
100 input: AuthorizationCodeRequest,
101) -> Result<OAuthFormRequest, OAuthError> {
102 validate_authorization_code_request(&input)?;
103 let mut request = OAuthFormRequest::new();
104 for (key, value) in input.headers {
105 request.set_header(key, value);
106 }
107 request.set_body("grant_type", "authorization_code");
108 request.set_body("code", input.code);
109 if let Some(code_verifier) = input.code_verifier {
110 request.set_body("code_verifier", code_verifier);
111 }
112 if let Some(client_key) = &input.options.client_key {
113 request.set_body("client_key", client_key);
114 }
115 if let Some(device_id) = input.device_id {
116 request.set_body("device_id", device_id);
117 }
118 request.set_body(
119 "redirect_uri",
120 input
121 .options
122 .redirect_uri
123 .as_deref()
124 .unwrap_or(&input.redirect_uri),
125 );
126 for resource in input.resource {
127 request.push_body("resource", resource);
128 }
129 apply_client_authentication(&mut request, &input.options, input.authentication, false)?;
130 for (key, value) in input.additional_params {
131 if !request.has_body(&key) {
132 request.push_body(key, value);
133 }
134 }
135 for (key, value) in input.override_params {
136 request.set_body(key, value);
137 }
138 Ok(request)
139}
140
141fn validate_authorization_code_request(input: &AuthorizationCodeRequest) -> Result<(), OAuthError> {
142 if input.code.is_empty() {
143 return Err(OAuthError::InvalidConfiguration(
144 "authorization code cannot be empty".to_owned(),
145 ));
146 }
147 let redirect_uri = input
148 .options
149 .redirect_uri
150 .as_deref()
151 .unwrap_or(&input.redirect_uri);
152 url::Url::parse(redirect_uri)?;
153 Ok(())
154}
155
156pub fn authorization_code_request(
157 input: AuthorizationCodeRequest,
158) -> Result<OAuthFormRequest, OAuthError> {
159 create_authorization_code_request(input)
160}
161
162pub async fn validate_authorization_code(
163 input: ClientTokenRequest<AuthorizationCodeRequest>,
164) -> Result<OAuth2Tokens, OAuthError> {
165 let request = authorization_code_request(input.request)?;
166 let data = post_form(&input.token_endpoint, request).await?;
167 get_oauth2_tokens(data)
168}