Skip to content

Commit de696cc

Browse files
committed
port inth-oauth2
0 parents  commit de696cc

12 files changed

+845
-0
lines changed

.cargo-ok

Whitespace-only changes.

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

COPYING

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This project is dual-licensed under the Unlicense and MIT licenses.
2+
3+
You may use this code under the terms of either license.

Cargo.toml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[package]
2+
name = 'openid'
3+
version = '0.1.0'
4+
authors = ['Alexander Korolev <[email protected]>']
5+
edition = '2018'
6+
categories = []
7+
description = '''
8+
openid description.
9+
'''
10+
homepage = 'https://github.com/kilork/openid'
11+
keywords = [
12+
'authentication',
13+
'authorization',
14+
'auth',
15+
'oauth',
16+
'openid',
17+
]
18+
license = 'Unlicense OR MIT'
19+
readme = 'README.md'
20+
repository = 'https://github.com/kilork/openid'
21+
22+
[dependencies]
23+
lazy_static = '1.4'
24+
serde_json = '1'
25+
base64 = '0.11'
26+
biscuit = '0.4'
27+
failure = '0.1'
28+
29+
[dependencies.url]
30+
version = '2.1'
31+
32+
[dependencies.chrono]
33+
version = '0.4'
34+
features = ['serde']
35+
36+
[dependencies.serde]
37+
version = '1'
38+
features = ['derive']
39+
40+
[dependencies.reqwest]
41+
version = '0.10'
42+
features = ['json']

MIT-LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Alexander Korolev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# openid description
2+
3+
## Legal
4+
5+
Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/).
6+
7+
## Features
8+
9+
## Usage
10+
11+
Add dependency to Cargo.toml:
12+
13+
```toml
14+
[dependencies]
15+
openid = "0.1"
16+
```

UNLICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org/>

src/bearer.rs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use chrono::{DateTime, Duration, Utc};
2+
use serde::{de::Visitor, Deserialize, Deserializer};
3+
use std::fmt;
4+
5+
/// The bearer token type.
6+
///
7+
/// See [RFC 6750](http://tools.ietf.org/html/rfc6750).
8+
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
9+
pub struct Bearer {
10+
pub access_token: String,
11+
pub scope: Option<String>,
12+
pub refresh_token: Option<String>,
13+
#[serde(
14+
default,
15+
rename = "expires_in",
16+
deserialize_with = "expire_in_to_instant"
17+
)]
18+
pub expires: Option<DateTime<Utc>>,
19+
}
20+
21+
fn expire_in_to_instant<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error>
22+
where
23+
D: Deserializer<'de>,
24+
{
25+
struct ExpireInVisitor;
26+
27+
impl<'de> Visitor<'de> for ExpireInVisitor {
28+
type Value = Option<DateTime<Utc>>;
29+
30+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
31+
formatter.write_str("an integer containing seconds")
32+
}
33+
34+
fn visit_none<E>(self) -> Result<Self::Value, E>
35+
where
36+
E: serde::de::Error,
37+
{
38+
Ok(None)
39+
}
40+
41+
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error>
42+
where
43+
D: Deserializer<'de>,
44+
{
45+
let expire_in: u64 = serde::de::Deserialize::deserialize(d)?;
46+
Ok(Some(Utc::now() + Duration::seconds(expire_in as i64)))
47+
}
48+
}
49+
50+
deserializer.deserialize_option(ExpireInVisitor)
51+
}
52+
53+
impl Bearer {
54+
pub fn expired(&self) -> bool {
55+
if let Some(expires) = self.expires {
56+
expires < Utc::now()
57+
} else {
58+
false
59+
}
60+
}
61+
}
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn from_response_refresh() {
69+
let json = r#"
70+
{
71+
"token_type":"Bearer",
72+
"access_token":"aaaaaaaa",
73+
"expires_in":3600,
74+
"refresh_token":"bbbbbbbb"
75+
}
76+
"#;
77+
let bearer: Bearer = serde_json::from_str(json).unwrap();
78+
assert_eq!("aaaaaaaa", bearer.access_token);
79+
assert_eq!(None, bearer.scope);
80+
assert_eq!(Some("bbbbbbbb".into()), bearer.refresh_token);
81+
let expires = bearer.expires.unwrap();
82+
assert!(expires > (Utc::now() + Duration::seconds(3599)));
83+
assert!(expires <= (Utc::now() + Duration::seconds(3600)));
84+
}
85+
86+
#[test]
87+
fn from_response_static() {
88+
let json = r#"
89+
{
90+
"token_type":"Bearer",
91+
"access_token":"aaaaaaaa"
92+
}
93+
"#;
94+
let bearer: Bearer = serde_json::from_str(json).unwrap();
95+
assert_eq!("aaaaaaaa", bearer.access_token);
96+
assert_eq!(None, bearer.scope);
97+
assert_eq!(None, bearer.refresh_token);
98+
assert_eq!(None, bearer.expires);
99+
}
100+
}

0 commit comments

Comments
 (0)