use super::grant::{Value, Extensions, Grant};
use super::{Url, Time};
use super::scope::Scope;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use base64::{encode, decode};
use ring::digest::SHA256;
use ring::rand::{SystemRandom, SecureRandom};
use ring::hmac::SigningKey;
use rmp_serde;
pub trait TagGrant {
fn tag(&mut self, usage: u64, grant: &Grant) -> Result<String, ()>;
}
pub struct RandomGenerator {
random: SystemRandom,
len: usize
}
impl RandomGenerator {
pub fn new(length: usize) -> RandomGenerator {
RandomGenerator {
random: SystemRandom::new(),
len: length
}
}
fn generate(&self) -> String {
let mut result = vec![0; self.len];
self.random.fill(result.as_mut_slice())
.expect("Failed to generate random token");
encode(&result)
}
}
pub struct Assertion {
secret: SigningKey,
}
#[derive(Serialize, Deserialize)]
struct SerdeAssertionGrant {
owner_id: String,
client_id: String,
#[serde(with = "scope_serde")]
scope: Scope,
#[serde(with = "url_serde")]
redirect_uri: Url,
#[serde(with = "time_serde")]
until: Time,
public_extensions: HashMap<String, Option<String>>,
}
#[derive(Serialize, Deserialize)]
struct AssertGrant(Vec<u8>, Vec<u8>);
pub struct TaggedAssertion<'a>(&'a Assertion, &'a str);
impl Assertion {
pub fn new<S: Into<Self>>(secret: S) -> Self {
secret.into()
}
#[deprecated = "Use the correctly named `ephemeral` instead."]
#[doc(hidden)]
pub fn ephermal() -> Assertion {
Self::ephemeral()
}
pub fn ephemeral() -> Self {
SigningKey::generate(&SHA256, &SystemRandom::new()).unwrap().into()
}
pub fn tag<'a>(&'a self, tag: &'a str) -> TaggedAssertion<'a> {
TaggedAssertion(self, tag)
}
fn extract<'a>(&self, token: &'a str) -> Result<(Grant, String), ()> {
let decoded = decode(token).map_err(|_| ())?;
let assertion: AssertGrant = rmp_serde::from_slice(&decoded).map_err(|_| ())?;
ring::hmac::verify_with_own_key(&self.secret, &assertion.0, &assertion.1).map_err(|_| ())?;
let (_, serde_grant, tag): (u64, SerdeAssertionGrant, String)
= rmp_serde::from_slice(&assertion.0).map_err(|_| ())?;
Ok((serde_grant.grant(), tag))
}
fn signature(&self, data: &[u8]) -> ring::hmac::Signature {
ring::hmac::sign(&self.secret, data)
}
fn counted_signature(&self, counter: u64, grant: &Grant)
-> Result<String, ()>
{
let serde_grant = SerdeAssertionGrant::try_from(grant)?;
let tosign = rmp_serde::to_vec(&(serde_grant, counter)).unwrap();
let signature = self.signature(&tosign);
Ok(base64::encode(&signature))
}
fn generate_tagged(&self, counter: u64, grant: &Grant, tag: &str) -> Result<String, ()> {
let serde_grant = SerdeAssertionGrant::try_from(grant)?;
let tosign = rmp_serde::to_vec(&(counter, serde_grant, tag)).unwrap();
let signature = self.signature(&tosign);
Ok(encode(&rmp_serde::to_vec(&AssertGrant(tosign, signature.as_ref().to_vec())).unwrap()))
}
}
impl<'a> TaggedAssertion<'a> {
pub fn sign(&self, counter: u64, grant: &Grant) -> Result<String, ()> {
self.0.generate_tagged(counter, grant, self.1)
}
pub fn extract<'b>(&self, token: &'b str) -> Result<Grant, ()> {
self.0.extract(token).and_then(|(token, tag)| {
if tag == self.1 {
Ok(token)
} else {
Err(())
}
})
}
}
impl From<SigningKey> for Assertion {
fn from(secret: SigningKey) -> Self {
Assertion {
secret,
}
}
}
impl<'a, T: TagGrant + ?Sized + 'a> TagGrant for Box<T> {
fn tag(&mut self, counter: u64, grant: &Grant) -> Result<String, ()> {
(&mut **self).tag(counter, grant)
}
}
impl<'a, T: TagGrant + ?Sized + 'a> TagGrant for &'a mut T {
fn tag(&mut self, counter: u64, grant: &Grant) -> Result<String, ()> {
(&mut **self).tag(counter, grant)
}
}
impl TagGrant for RandomGenerator {
fn tag(&mut self, _: u64, _: &Grant) -> Result<String, ()> {
Ok(self.generate())
}
}
impl<'a> TagGrant for &'a RandomGenerator {
fn tag(&mut self, _: u64, _: &Grant) -> Result<String, ()> {
Ok(self.generate())
}
}
impl TagGrant for Rc<RandomGenerator> {
fn tag(&mut self, _: u64, _: &Grant) -> Result<String, ()> {
Ok(self.generate())
}
}
impl TagGrant for Arc<RandomGenerator> {
fn tag(&mut self, _: u64, _: &Grant) -> Result<String, ()> {
Ok(self.generate())
}
}
impl TagGrant for Assertion {
fn tag(&mut self, counter: u64, grant: &Grant) -> Result<String, ()> {
self.counted_signature(counter, grant)
}
}
impl<'a> TagGrant for &'a Assertion {
fn tag(&mut self, counter: u64, grant: &Grant) -> Result<String, ()> {
self.counted_signature(counter, grant)
}
}
impl TagGrant for Rc<Assertion> {
fn tag(&mut self, counter: u64, grant: &Grant) -> Result<String, ()> {
self.counted_signature(counter, grant)
}
}
impl TagGrant for Arc<Assertion> {
fn tag(&mut self, counter: u64, grant: &Grant) -> Result<String, ()> {
self.counted_signature(counter, grant)
}
}
mod scope_serde {
use primitives::scope::Scope;
use serde::ser::{Serializer};
use serde::de::{Deserialize, Deserializer, Error};
pub fn serialize<S: Serializer>(scope: &Scope, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&scope.to_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Scope, D::Error> {
let as_string: &str = <(&str)>::deserialize(deserializer)?;
as_string.parse().map_err(Error::custom)
}
}
mod url_serde {
use super::Url;
use serde::ser::{Serializer};
use serde::de::{Deserialize, Deserializer, Error};
pub fn serialize<S: Serializer>(url: &Url, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&url.to_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Url, D::Error> {
let as_string: &str = <(&str)>::deserialize(deserializer)?;
as_string.parse().map_err(Error::custom)
}
}
mod time_serde {
use super::Time;
use chrono::{TimeZone, Utc};
use serde::ser::{Serializer};
use serde::de::{Deserialize, Deserializer};
pub fn serialize<S: Serializer>(time: &Time, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_i64(time.timestamp())
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Time, D::Error> {
let as_timestamp: i64 = <i64>::deserialize(deserializer)?;
Ok(Utc.timestamp(as_timestamp, 0))
}
}
impl SerdeAssertionGrant {
fn try_from(grant: &Grant) -> Result<Self, ()> {
let mut public_extensions: HashMap<String, Option<String>> = HashMap::new();
if grant.extensions.private().any(|_| true) {
return Err(())
}
for (name, content) in grant.extensions.public() {
public_extensions.insert(name.to_string(), content.map(str::to_string));
}
Ok(SerdeAssertionGrant {
owner_id: grant.owner_id.clone(),
client_id: grant.client_id.clone(),
scope: grant.scope.clone(),
redirect_uri: grant.redirect_uri.clone(),
until: grant.until,
public_extensions,
})
}
fn grant(self) -> Grant {
let mut extensions = Extensions::new();
for (name, content) in self.public_extensions.into_iter() {
extensions.set_raw(name, Value::public(content))
}
Grant {
owner_id: self.owner_id,
client_id: self.client_id,
scope: self.scope,
redirect_uri: self.redirect_uri,
until: self.until,
extensions,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use ring::digest::SHA256;
use ring::hmac::SigningKey;
#[test]
#[allow(dead_code, unused)]
fn assert_send_sync_static() {
fn uses<T: Send + Sync + 'static>(arg: T) { }
let _ = uses(RandomGenerator::new(16));
let fake_key = SigningKey::new(&SHA256, &[0u8; 16]);
let _ = uses(Assertion::new(fake_key));
}
}