Skip to content

Commit

Permalink
port inth-oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
kilork committed Mar 1, 2020
0 parents commit de696cc
Show file tree
Hide file tree
Showing 12 changed files with 845 additions and 0 deletions.
Empty file added .cargo-ok
Empty file.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
3 changes: 3 additions & 0 deletions COPYING
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.
42 changes: 42 additions & 0 deletions Cargo.toml
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']
21 changes: 21 additions & 0 deletions MIT-LICENSE
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.
16 changes: 16 additions & 0 deletions README.md
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"
```
24 changes: 24 additions & 0 deletions UNLICENSE
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/>
100 changes: 100 additions & 0 deletions src/bearer.rs
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);
}
}
Loading

0 comments on commit de696cc

Please sign in to comment.