-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit de696cc
Showing
12 changed files
with
845 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
**/*.rs.bk | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This project is dual-licensed under the Unlicense and MIT licenses. | ||
|
||
You may use this code under the terms of either license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[package] | ||
name = 'openid' | ||
version = '0.1.0' | ||
authors = ['Alexander Korolev <[email protected]>'] | ||
edition = '2018' | ||
categories = [] | ||
description = ''' | ||
openid description. | ||
''' | ||
homepage = 'https://github.com/kilork/openid' | ||
keywords = [ | ||
'authentication', | ||
'authorization', | ||
'auth', | ||
'oauth', | ||
'openid', | ||
] | ||
license = 'Unlicense OR MIT' | ||
readme = 'README.md' | ||
repository = 'https://github.com/kilork/openid' | ||
|
||
[dependencies] | ||
lazy_static = '1.4' | ||
serde_json = '1' | ||
base64 = '0.11' | ||
biscuit = '0.4' | ||
failure = '0.1' | ||
|
||
[dependencies.url] | ||
version = '2.1' | ||
|
||
[dependencies.chrono] | ||
version = '0.4' | ||
features = ['serde'] | ||
|
||
[dependencies.serde] | ||
version = '1' | ||
features = ['derive'] | ||
|
||
[dependencies.reqwest] | ||
version = '0.10' | ||
features = ['json'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Alexander Korolev | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# openid description | ||
|
||
## Legal | ||
|
||
Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/). | ||
|
||
## Features | ||
|
||
## Usage | ||
|
||
Add dependency to Cargo.toml: | ||
|
||
```toml | ||
[dependencies] | ||
openid = "0.1" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <http://unlicense.org/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use chrono::{DateTime, Duration, Utc}; | ||
use serde::{de::Visitor, Deserialize, Deserializer}; | ||
use std::fmt; | ||
|
||
/// The bearer token type. | ||
/// | ||
/// See [RFC 6750](http://tools.ietf.org/html/rfc6750). | ||
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)] | ||
pub struct Bearer { | ||
pub access_token: String, | ||
pub scope: Option<String>, | ||
pub refresh_token: Option<String>, | ||
#[serde( | ||
default, | ||
rename = "expires_in", | ||
deserialize_with = "expire_in_to_instant" | ||
)] | ||
pub expires: Option<DateTime<Utc>>, | ||
} | ||
|
||
fn expire_in_to_instant<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct ExpireInVisitor; | ||
|
||
impl<'de> Visitor<'de> for ExpireInVisitor { | ||
type Value = Option<DateTime<Utc>>; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("an integer containing seconds") | ||
} | ||
|
||
fn visit_none<E>(self) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(None) | ||
} | ||
|
||
fn visit_some<D>(self, d: D) -> Result<Option<DateTime<Utc>>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let expire_in: u64 = serde::de::Deserialize::deserialize(d)?; | ||
Ok(Some(Utc::now() + Duration::seconds(expire_in as i64))) | ||
} | ||
} | ||
|
||
deserializer.deserialize_option(ExpireInVisitor) | ||
} | ||
|
||
impl Bearer { | ||
pub fn expired(&self) -> bool { | ||
if let Some(expires) = self.expires { | ||
expires < Utc::now() | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn from_response_refresh() { | ||
let json = r#" | ||
{ | ||
"token_type":"Bearer", | ||
"access_token":"aaaaaaaa", | ||
"expires_in":3600, | ||
"refresh_token":"bbbbbbbb" | ||
} | ||
"#; | ||
let bearer: Bearer = serde_json::from_str(json).unwrap(); | ||
assert_eq!("aaaaaaaa", bearer.access_token); | ||
assert_eq!(None, bearer.scope); | ||
assert_eq!(Some("bbbbbbbb".into()), bearer.refresh_token); | ||
let expires = bearer.expires.unwrap(); | ||
assert!(expires > (Utc::now() + Duration::seconds(3599))); | ||
assert!(expires <= (Utc::now() + Duration::seconds(3600))); | ||
} | ||
|
||
#[test] | ||
fn from_response_static() { | ||
let json = r#" | ||
{ | ||
"token_type":"Bearer", | ||
"access_token":"aaaaaaaa" | ||
} | ||
"#; | ||
let bearer: Bearer = serde_json::from_str(json).unwrap(); | ||
assert_eq!("aaaaaaaa", bearer.access_token); | ||
assert_eq!(None, bearer.scope); | ||
assert_eq!(None, bearer.refresh_token); | ||
assert_eq!(None, bearer.expires); | ||
} | ||
} |
Oops, something went wrong.