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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::errors::RequestError;
use http::{header::CONTENT_TYPE, Request, Uri};
use std::convert::TryInto;
use tame_oauth::Error;
use url::form_urlencoded::Serializer;
#[derive(serde::Deserialize, Debug)]
struct TokenExchangeResponse {
access_token: String,
token_type: String,
expires_in: i64,
scope: String,
id_token: Option<String>,
refresh_token: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
pub access_token: String,
pub token_type: String,
pub expires_in: i64,
pub expires_in_timestamp: i64,
pub scope: String,
pub id_token: Option<String>,
pub refresh_token: Option<String>,
}
impl Token {
pub fn from_response<S>(response: http::Response<S>) -> Result<Self, Error>
where
S: AsRef<[u8]>,
{
parse_token_response(response)
}
}
impl From<TokenExchangeResponse> for Token {
fn from(t: TokenExchangeResponse) -> Token {
let expires_ts = chrono::Utc::now().timestamp() + t.expires_in;
Token {
access_token: t.access_token,
token_type: t.token_type,
refresh_token: t.refresh_token,
expires_in: t.expires_in,
expires_in_timestamp: expires_ts,
scope: t.scope,
id_token: t.id_token,
}
}
}
pub fn exchange_token_request<ReqUri, RedirectUri>(
uri: ReqUri,
redirect_uri: RedirectUri,
client_id: &str,
auth_code: &str,
client_secret: Option<&str>,
code_verifier: Option<&str>,
) -> Result<Request<Vec<u8>>, RequestError>
where
ReqUri: TryInto<Uri>,
RedirectUri: TryInto<Uri>,
{
let mut serializer = Serializer::new(String::new());
serializer.append_pair("client_id", client_id);
serializer.append_pair("redirect_uri", &into_uri(redirect_uri)?.to_string());
serializer.append_pair("grant_type", "authorization_code");
serializer.append_pair("code", auth_code);
if let Some(cs) = client_secret {
serializer.append_pair("client_secret", cs);
}
if let Some(cv) = code_verifier {
serializer.append_pair("code_verifier", cv);
}
let body = serializer.finish();
http_post_req(body, uri)
}
pub(crate) fn into_uri<U: TryInto<Uri>>(uri: U) -> Result<Uri, RequestError> {
uri.try_into().map_err(|_err| RequestError::InvalidUri)
}
pub fn parse_token_response<S>(response: http::Response<S>) -> Result<Token, Error>
where
S: AsRef<[u8]>,
{
let (parts, body) = response.into_parts();
if !parts.status.is_success() {
return Err(Error::HttpStatus(parts.status));
}
let token_res: TokenExchangeResponse = serde_json::from_slice(body.as_ref())?;
let token: Token = token_res.into();
Ok(token)
}
pub fn refresh_token_request<ReqUri>(
uri: ReqUri,
client_id: &str,
client_secret: &str,
refresh_token: &str,
) -> Result<Request<Vec<u8>>, RequestError>
where
ReqUri: TryInto<Uri>,
{
let body = Serializer::new(String::new())
.append_pair("client_id", client_id)
.append_pair("client_secret", client_secret)
.append_pair("grant_type", "refresh_token")
.append_pair("refresh_token", refresh_token)
.finish();
http_post_req(body, uri)
}
fn http_post_req<ReqUri>(body: String, uri: ReqUri) -> Result<Request<Vec<u8>>, RequestError>
where
ReqUri: TryInto<Uri>,
{
let req_body = Vec::from(body);
Ok(Request::builder()
.method("POST")
.uri(into_uri(uri)?)
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.body(req_body)?)
}
#[cfg(test)]
mod test {
use super::*;
use std::str;
#[test]
fn pkce_flow_exchange() {
let request = exchange_token_request(
"https://www.example.com/",
"http://localhost:8000/",
"client_id",
"auth-code",
None,
Some("the_secret_code_verifier"),
)
.unwrap();
let body = str::from_utf8(request.body()).unwrap();
assert_eq!(body.contains("client_secret"), false);
assert_eq!(
body.contains("code_verifier=the_secret_code_verifier"),
true
);
}
}