forked from ramosbugs/oauth2-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.rs
407 lines (360 loc) · 12.1 KB
/
code.rs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::{
AuthUrl, Client, ClientId, CsrfToken, EndpointState, ErrorResponse, PkceCodeChallenge,
RedirectUrl, ResponseType, RevocableToken, Scope, TokenIntrospectionResponse, TokenResponse,
};
use url::Url;
use std::borrow::Cow;
impl<
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
>
Client<
TE,
TR,
TIR,
RT,
TRE,
HasAuthUrl,
HasDeviceAuthUrl,
HasIntrospectionUrl,
HasRevocationUrl,
HasTokenUrl,
>
where
TE: ErrorResponse + 'static,
TR: TokenResponse,
TIR: TokenIntrospectionResponse,
RT: RevocableToken,
TRE: ErrorResponse + 'static,
HasAuthUrl: EndpointState,
HasDeviceAuthUrl: EndpointState,
HasIntrospectionUrl: EndpointState,
HasRevocationUrl: EndpointState,
HasTokenUrl: EndpointState,
{
pub(crate) fn authorize_url_impl<'a, S>(
&'a self,
auth_url: &'a AuthUrl,
state_fn: S,
) -> AuthorizationRequest<'a>
where
S: FnOnce() -> CsrfToken,
{
AuthorizationRequest {
auth_url,
client_id: &self.client_id,
extra_params: Vec::new(),
pkce_challenge: None,
redirect_url: self.redirect_url.as_ref().map(Cow::Borrowed),
response_type: "code".into(),
scopes: Vec::new(),
state: state_fn(),
}
}
}
/// A request to the authorization endpoint
#[derive(Debug)]
pub struct AuthorizationRequest<'a> {
pub(crate) auth_url: &'a AuthUrl,
pub(crate) client_id: &'a ClientId,
pub(crate) extra_params: Vec<(Cow<'a, str>, Cow<'a, str>)>,
pub(crate) pkce_challenge: Option<PkceCodeChallenge>,
pub(crate) redirect_url: Option<Cow<'a, RedirectUrl>>,
pub(crate) response_type: Cow<'a, str>,
pub(crate) scopes: Vec<Cow<'a, Scope>>,
pub(crate) state: CsrfToken,
}
impl<'a> AuthorizationRequest<'a> {
/// Appends a new scope to the authorization URL.
pub fn add_scope(mut self, scope: Scope) -> Self {
self.scopes.push(Cow::Owned(scope));
self
}
/// Appends a collection of scopes to the token request.
pub fn add_scopes<I>(mut self, scopes: I) -> Self
where
I: IntoIterator<Item = Scope>,
{
self.scopes.extend(scopes.into_iter().map(Cow::Owned));
self
}
/// Appends an extra param to the authorization URL.
///
/// This method allows extensions to be used without direct support from
/// this crate. If `name` conflicts with a parameter managed by this crate, the
/// behavior is undefined. In particular, do not set parameters defined by
/// [RFC 6749](https://tools.ietf.org/html/rfc6749) or
/// [RFC 7636](https://tools.ietf.org/html/rfc7636).
///
/// # Security Warning
///
/// Callers should follow the security recommendations for any OAuth2 extensions used with
/// this function, which are beyond the scope of
/// [RFC 6749](https://tools.ietf.org/html/rfc6749).
pub fn add_extra_param<N, V>(mut self, name: N, value: V) -> Self
where
N: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
{
self.extra_params.push((name.into(), value.into()));
self
}
/// Enables the [Implicit Grant](https://tools.ietf.org/html/rfc6749#section-4.2) flow.
pub fn use_implicit_flow(mut self) -> Self {
self.response_type = "token".into();
self
}
/// Enables custom flows other than the `code` and `token` (implicit flow) grant.
pub fn set_response_type(mut self, response_type: &ResponseType) -> Self {
self.response_type = (**response_type).to_owned().into();
self
}
/// Enables the use of [Proof Key for Code Exchange](https://tools.ietf.org/html/rfc7636)
/// (PKCE).
///
/// PKCE is *highly recommended* for all public clients (i.e., those for which there
/// is no client secret or for which the client secret is distributed with the client,
/// such as in a native, mobile app, or browser app).
pub fn set_pkce_challenge(mut self, pkce_code_challenge: PkceCodeChallenge) -> Self {
self.pkce_challenge = Some(pkce_code_challenge);
self
}
/// Overrides the `redirect_url` to the one specified.
pub fn set_redirect_uri(mut self, redirect_url: Cow<'a, RedirectUrl>) -> Self {
self.redirect_url = Some(redirect_url);
self
}
/// Returns the full authorization URL and CSRF state for this authorization
/// request.
pub fn url(http://github.com/self) -> (Url, CsrfToken) {
let scopes = self
.scopes
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.join(" ");
let url = {
let mut pairs: Vec<(&str, &str)> = vec![
("response_type", self.response_type.as_ref()),
("client_id", self.client_id),
("state", self.state.secret()),
];
if let Some(ref pkce_challenge) = self.pkce_challenge {
pairs.push(("code_challenge", pkce_challenge.as_str()));
pairs.push(("code_challenge_method", pkce_challenge.method().as_str()));
}
if let Some(ref redirect_url) = self.redirect_url {
pairs.push(("redirect_uri", redirect_url.as_str()));
}
if !scopes.is_empty() {
pairs.push(("scope", &scopes));
}
let mut url: Url = self.auth_url.url().to_owned();
url.query_pairs_mut()
.extend_pairs(pairs.iter().map(|&(k, v)| (k, v)));
url.query_pairs_mut()
.extend_pairs(self.extra_params.iter().cloned());
url
};
(url, self.state)
}
}
#[cfg(test)]
mod tests {
use crate::basic::BasicClient;
use crate::tests::new_client;
use crate::{
AuthUrl, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge, PkceCodeVerifier,
RedirectUrl, ResponseType, Scope, TokenUrl,
};
use url::form_urlencoded::byte_serialize;
use url::Url;
use std::borrow::Cow;
#[test]
fn test_authorize_url() {
let client = new_client();
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.url();
assert_eq!(
Url::parse(
"https://example.com/auth?response_type=code&client_id=aaa&state=csrf_token"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_random() {
let client = new_client();
let (url, csrf_state) = client.authorize_url(/service/csrftoken::new_random).url();
assert_eq!(
Url::parse(&format!(
"https://example.com/auth?response_type=code&client_id=aaa&state={}",
byte_serialize(csrf_state.secret().clone().into_bytes().as_slice())
.collect::<Vec<_>>()
.join("")
))
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_pkce() {
// Example from https://tools.ietf.org/html/rfc7636#appendix-B
let client = new_client();
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.set_pkce_challenge(PkceCodeChallenge::from_code_verifier_sha256(
&PkceCodeVerifier::new("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string()),
))
.url();
assert_eq!(
Url::parse(concat!(
"https://example.com/auth",
"?response_type=code&client_id=aaa",
"&state=csrf_token",
"&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
"&code_challenge_method=S256",
))
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_implicit() {
let client = new_client();
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.use_implicit_flow()
.url();
assert_eq!(
Url::parse(
"https://example.com/auth?response_type=token&client_id=aaa&state=csrf_token"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_with_param() {
let client = BasicClient::new(ClientId::new("aaa".to_string()))
.set_client_secret(ClientSecret::new("bbb".to_string()))
.set_auth_uri(AuthUrl::new("https://example.com/auth?foo=bar".to_string()).unwrap())
.set_token_uri(TokenUrl::new("https://example.com/token".to_string()).unwrap());
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.url();
assert_eq!(
Url::parse(
"https://example.com/auth?foo=bar&response_type=code&client_id=aaa&state=csrf_token"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_with_scopes() {
let scopes = vec![
Scope::new("read".to_string()),
Scope::new("write".to_string()),
];
let (url, _) = new_client()
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.add_scopes(scopes)
.url();
assert_eq!(
Url::parse(
"https://example.com/auth\
?response_type=code\
&client_id=aaa\
&state=csrf_token\
&scope=read+write"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_with_one_scope() {
let (url, _) = new_client()
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.add_scope(Scope::new("read".to_string()))
.url();
assert_eq!(
Url::parse(
"https://example.com/auth\
?response_type=code\
&client_id=aaa\
&state=csrf_token\
&scope=read"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_with_extension_response_type() {
let client = new_client();
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.set_response_type(&ResponseType::new("code token".to_string()))
.add_extra_param("foo", "bar")
.url();
assert_eq!(
Url::parse(
"https://example.com/auth?response_type=code+token&client_id=aaa&state=csrf_token\
&foo=bar"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_with_redirect_url() {
let client = new_client()
.set_redirect_uri(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.url();
assert_eq!(
Url::parse(
"https://example.com/auth?response_type=code\
&client_id=aaa\
&state=csrf_token\
&redirect_uri=https%3A%2F%2Flocalhost%2Fredirect"
)
.unwrap(),
url
);
}
#[test]
fn test_authorize_url_with_redirect_url_override() {
let client = new_client()
.set_redirect_uri(RedirectUrl::new("https://localhost/redirect".to_string()).unwrap());
let (url, _) = client
.authorize_url(http://github.com/||%20CsrfToken::new("csrf_token".to_string()))
.set_redirect_uri(Cow::Owned(
RedirectUrl::new("https://localhost/alternative".to_string()).unwrap(),
))
.url();
assert_eq!(
Url::parse(
"https://example.com/auth?response_type=code\
&client_id=aaa\
&state=csrf_token\
&redirect_uri=https%3A%2F%2Flocalhost%2Falternative"
)
.unwrap(),
url
);
}
}