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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use http::Method;
use mime::Mime;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::types::{
ClientId, ClientPassword, ClientSecret, Code, CodeVerifier, Scope, ScopeFromStrError,
ScopeParameter,
};
pub const METHOD: Method = Method::POST;
pub const CONTENT_TYPE: Mime = mime::APPLICATION_WWW_FORM_URLENCODED;
pub const GRANT_TYPE_WITH_AUTHORIZATION_CODE_GRANT: &str = "authorization_code";
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "grant_type")]
pub enum Body<SCOPE>
where
SCOPE: Scope,
{
#[serde(rename = "authorization_code")]
AuthorizationCodeGrant(BodyWithAuthorizationCodeGrant),
#[serde(rename = "urn:ietf:params:oauth:grant-type:device_code")]
DeviceAuthorizationGrant(BodyWithDeviceAuthorizationGrant),
#[serde(rename = "client_credentials")]
ClientCredentialsGrant(BodyWithClientCredentialsGrant<SCOPE>),
#[serde(rename = "password")]
ResourceOwnerPasswordCredentialsGrant(BodyWithResourceOwnerPasswordCredentialsGrant<SCOPE>),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BodyWithAuthorizationCodeGrant {
pub code: Code,
#[serde(skip_serializing_if = "Option::is_none")]
pub redirect_uri: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_id: Option<ClientId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_secret: Option<ClientSecret>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code_verifier: Option<CodeVerifier>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
_extra: Option<Map<String, Value>>,
}
impl BodyWithAuthorizationCodeGrant {
pub fn new(
code: Code,
redirect_uri: Option<String>,
client_id: Option<ClientId>,
client_secret: Option<ClientSecret>,
) -> Self {
Self::internal_new(code, redirect_uri, client_id, client_secret, None)
}
fn internal_new(
code: Code,
redirect_uri: Option<String>,
client_id: Option<ClientId>,
client_secret: Option<ClientSecret>,
code_verifier: Option<CodeVerifier>,
) -> Self {
Self {
code,
redirect_uri,
client_id,
client_secret,
code_verifier,
_extra: None,
}
}
pub fn set_extra(&mut self, extra: Map<String, Value>) {
self._extra = Some(extra);
}
pub fn extra(&self) -> Option<&Map<String, Value>> {
self._extra.as_ref()
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BodyWithDeviceAuthorizationGrant {
pub device_code: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_id: Option<ClientId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_secret: Option<ClientSecret>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
_extra: Option<Map<String, Value>>,
}
impl BodyWithDeviceAuthorizationGrant {
pub fn new(
device_code: String,
client_id: Option<ClientId>,
client_secret: Option<ClientSecret>,
) -> Self {
Self {
device_code,
client_id,
client_secret,
_extra: None,
}
}
pub fn set_extra(&mut self, extra: Map<String, Value>) {
self._extra = Some(extra);
}
pub fn extra(&self) -> Option<&Map<String, Value>> {
self._extra.as_ref()
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BodyWithClientCredentialsGrant<SCOPE>
where
SCOPE: Scope,
{
#[serde(skip_serializing_if = "Option::is_none")]
pub scope: Option<ScopeParameter<SCOPE>>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub client_password: Option<ClientPassword>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
_extra: Option<Map<String, Value>>,
}
impl<SCOPE> BodyWithClientCredentialsGrant<SCOPE>
where
SCOPE: Scope,
{
pub fn new(scope: Option<ScopeParameter<SCOPE>>) -> Self {
Self {
scope,
client_password: None,
_extra: None,
}
}
pub fn new_with_client_password(
scope: Option<ScopeParameter<SCOPE>>,
client_password: ClientPassword,
) -> Self {
Self {
scope,
client_password: Some(client_password),
_extra: None,
}
}
pub fn set_extra(&mut self, extra: Map<String, Value>) {
self._extra = Some(extra);
}
pub fn extra(&self) -> Option<&Map<String, Value>> {
self._extra.as_ref()
}
pub fn try_from_t_with_string(
body: &BodyWithClientCredentialsGrant<String>,
) -> Result<Self, ScopeFromStrError> {
let scope = if let Some(x) = &body.scope {
Some(ScopeParameter::<SCOPE>::try_from_t_with_string(x)?)
} else {
None
};
let mut this = Self::new(scope);
this.client_password = body.client_password.to_owned();
if let Some(extra) = body.extra() {
this.set_extra(extra.to_owned());
}
Ok(this)
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BodyWithResourceOwnerPasswordCredentialsGrant<SCOPE>
where
SCOPE: Scope,
{
pub username: String,
pub password: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub scope: Option<ScopeParameter<SCOPE>>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub client_password: Option<ClientPassword>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
_extra: Option<Map<String, Value>>,
}
impl<SCOPE> BodyWithResourceOwnerPasswordCredentialsGrant<SCOPE>
where
SCOPE: Scope,
{
pub fn new(
username: impl AsRef<str>,
password: impl AsRef<str>,
scope: Option<ScopeParameter<SCOPE>>,
) -> Self {
Self {
username: username.as_ref().to_owned(),
password: password.as_ref().to_owned(),
scope,
client_password: None,
_extra: None,
}
}
pub fn new_with_client_password(
username: impl AsRef<str>,
password: impl AsRef<str>,
scope: Option<ScopeParameter<SCOPE>>,
client_password: ClientPassword,
) -> Self {
Self {
username: username.as_ref().to_owned(),
password: password.as_ref().to_owned(),
scope,
client_password: Some(client_password),
_extra: None,
}
}
pub fn set_extra(&mut self, extra: Map<String, Value>) {
self._extra = Some(extra);
}
pub fn extra(&self) -> Option<&Map<String, Value>> {
self._extra.as_ref()
}
pub fn try_from_t_with_string(
body: &BodyWithResourceOwnerPasswordCredentialsGrant<String>,
) -> Result<Self, ScopeFromStrError> {
let scope = if let Some(x) = &body.scope {
Some(ScopeParameter::<SCOPE>::try_from_t_with_string(x)?)
} else {
None
};
let mut this = Self::new(&body.username, &body.password, scope);
this.client_password = body.client_password.to_owned();
if let Some(extra) = body.extra() {
this.set_extra(extra.to_owned());
}
Ok(this)
}
}
#[cfg(test)]
mod tests_with_authorization_code_grant {
use super::*;
#[test]
fn test_ser_de() {
let body_str = "grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::AuthorizationCodeGrant(body)) => {
assert_eq!(body.code, "SplxlOBeZQQYbYS6WxSbIA");
assert_eq!(
body.redirect_uri,
Some("https://client.example.com/cb".parse().unwrap())
);
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
}
}
#[cfg(test)]
mod tests_with_device_authorization_grant {
use super::*;
#[test]
fn test_ser_de() {
let body_str = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&device_code=GmRhmhcxhwAzkoEqiMEg_DnyEysNkuNhszIySk9eS&client_id=1406020730";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::DeviceAuthorizationGrant(body)) => {
assert_eq!(
body.device_code,
"GmRhmhcxhwAzkoEqiMEg_DnyEysNkuNhszIySk9eS"
);
assert_eq!(body.client_id, Some("1406020730".to_owned()));
assert_eq!(
body_str,
serde_urlencoded::to_string(Body::<String>::DeviceAuthorizationGrant(body))
.unwrap()
);
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
}
#[test]
fn test_ser_de_extra() {
let mut extra = Map::new();
extra.insert("foo".to_owned(), Value::String("bar".to_owned()));
let mut body = BodyWithDeviceAuthorizationGrant::new(
"your_device_code".to_owned(),
Some("your_client_id".to_owned()),
Some("your_client_secret".to_owned()),
);
body.set_extra(extra.to_owned());
let body = Body::<String>::DeviceAuthorizationGrant(body);
let body_str = serde_urlencoded::to_string(body).unwrap();
assert_eq!(body_str, "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&device_code=your_device_code&client_id=your_client_id&client_secret=your_client_secret&foo=bar");
match serde_urlencoded::from_str::<Body<String>>(body_str.as_str()) {
Ok(Body::DeviceAuthorizationGrant(body)) => {
assert_eq!(body.extra(), Some(&extra));
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
}
}
#[cfg(test)]
mod tests_with_client_credentials_grant {
use super::*;
#[test]
fn test_ser_de() {
let body_str = "grant_type=client_credentials";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ClientCredentialsGrant(body)) => {
assert_eq!(body.client_password, None);
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
let body_str =
"grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ClientCredentialsGrant(body)) => {
assert_eq!(body.client_password.unwrap().client_id, "CLIENT_ID");
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
let body_str = "grant_type=client_credentials&client_id=CLIENT_ID";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ClientCredentialsGrant(body)) => {
assert_eq!(body.client_password, None);
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
}
#[test]
fn test_ser_de_extra() {
let body_str = "grant_type=client_credentials&foo=bar";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ClientCredentialsGrant(body)) => {
assert_eq!(body.client_password, None);
assert_eq!(
body.extra().unwrap().get("foo").unwrap().as_str(),
Some("bar")
)
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
let body_str =
"grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&foo=bar";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ClientCredentialsGrant(body)) => {
assert_eq!(
body.client_password.to_owned().unwrap().client_id,
"CLIENT_ID"
);
assert_eq!(
body.extra().unwrap().get("foo").unwrap().as_str(),
Some("bar")
)
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
}
}
#[cfg(test)]
mod tests_with_resource_owner_password_credentials_grant {
use super::*;
#[test]
fn test_ser_de() {
let body_str = "grant_type=password&username=USERNAME&password=PASSWORD";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ResourceOwnerPasswordCredentialsGrant(body)) => {
assert_eq!(body.username, "USERNAME");
assert_eq!(body.password, "PASSWORD");
assert_eq!(body.client_password, None);
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
let body_str =
"grant_type=password&username=USERNAME&password=PASSWORD&client_id=CLIENT_ID&client_secret=CLIENT_SECRET";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ResourceOwnerPasswordCredentialsGrant(body)) => {
assert_eq!(body.username, "USERNAME");
assert_eq!(body.password, "PASSWORD");
assert_eq!(body.client_password.unwrap().client_id, "CLIENT_ID");
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
let body_str =
"grant_type=password&username=USERNAME&password=PASSWORD&client_id=CLIENT_ID";
match serde_urlencoded::from_str::<Body<String>>(body_str) {
Ok(Body::ResourceOwnerPasswordCredentialsGrant(body)) => {
assert_eq!(body.username, "USERNAME");
assert_eq!(body.password, "PASSWORD");
assert_eq!(body.client_password, None);
}
#[allow(unreachable_patterns)]
Ok(body) => panic!("{:?}", body),
Err(err) => panic!("{}", err),
}
}
}