Skip to content

Commit

Permalink
feat(util): new MemoryStorage and NullStorage
Browse files Browse the repository at this point in the history
Additionally, the Authenticator interface was scetched out.
It will replace the DeviceFlowHelper, and become the universal
do-it-all tool, as it supports storage as well.
  • Loading branch information
Byron committed Feb 27, 2015
1 parent 3f965c8 commit 091f1c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

It is implemented such that it makes no assumptions about the front-end, allowing more uses than just in yup.

Architecturally, it may never be implementing more than device authentication, yet is set up not to constrain itself.

### Usage

Please have a look at the [API landing page][API-docs] for all the examples you will ever need.
Expand Down
9 changes: 1 addition & 8 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait Flow : MarkerTrait {
/// for the two fields dealing with expiry - once in relative in and once in
/// absolute terms.
///
/// Utility methods make common queries easier, see `invalid()` or `expired()`.
/// Utility methods make common queries easier, see `expired()`.
#[derive(Clone, PartialEq, Debug, RustcDecodable, RustcEncodable)]
pub struct Token {
/// used when authenticating calls to oauth2 enabled services.
Expand All @@ -36,13 +36,6 @@ pub struct Token {

impl Token {

/// Returns true if the access token is expired or unset.
pub fn invalid(&self) -> bool {
self.access_token.len() == 0
|| self.refresh_token.len() == 0
|| self.expired()
}

/// Returns true if we are expired.
///
/// # Panics
Expand Down
7 changes: 4 additions & 3 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@ impl<'a> DeviceFlowHelper<'a> {
/// Blocks until a token was retrieved from the server, or the delegate
/// decided to abort the attempt, or the user decided not to authorize
/// the application.
pub fn retrieve_token<'b, NC, T, I>(&mut self,
client: hyper::Client<NC>,
pub fn retrieve_token<'b, C, NC, T, I>(&mut self,
client: C,
client_id: &str, client_secret: &str, scopes: I)
-> Option<Token>
where T: Str,
I: IntoIterator<Item=&'b T> + Clone,
NC: hyper::net::NetworkConnector {
NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> {
let mut flow = DeviceFlow::new(client);

// PHASE 1: REQUEST CODE
Expand Down
2 changes: 1 addition & 1 deletion src/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod tests {
"bogus", "secret", "bogus_refresh_token") {
RefreshResult::Success(ref t) => {
assert_eq!(t.access_token, "1/fFAGRNJru1FTz70BzhT3Zg");
assert!(!t.expired() && !t.invalid());
assert!(!t.expired());
},
_ => unreachable!()
}
Expand Down
59 changes: 55 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use common::{Token, AuthenticationType};
use std::borrow::BorrowMut;
use std::marker::PhantomData;
use std::collections::HashMap;

use common::{Token, AuthenticationType, ApplicationSecret};

use hyper;


/// Implements a specialised storage to set and retrieve `Token` instances.
/// The `scope_hash` represents the signature of the scopes for which the given token
Expand All @@ -7,17 +14,61 @@ pub trait TokenStorage {
/// If `token` is None, it is invalid or revoked and should be removed from storage.
fn set(&mut self, scope_hash: i64, token: Option<Token>);
/// A `None` result indicates that there is no token for the given scope_hash.
/// It is assumed that a token previously `set` will be retrievable using `get`
fn get(&self, scope_hash: i64) -> Option<Token>;
}

/// A storage that remembers nothing.
pub struct NullStorage;

impl TokenStorage for NullStorage {
fn set(&mut self, _: i64, _: Option<Token>) {}
fn get(&self, _: i64) -> Option<Token> { None }
}

/// A storage that remembers values for one session only.
pub struct MemoryStorage {
pub tokens: HashMap<i64, Token>
}

impl TokenStorage for MemoryStorage {
fn set(&mut self, scope_hash: i64, token: Option<Token>) {
match token {
Some(t) => self.tokens.insert(scope_hash, t),
None => self.tokens.remove(&scope_hash),
};
}

fn get(&self, scope_hash: i64) -> Option<Token> {
match self.tokens.get(&scope_hash) {
Some(t) => Some(t.clone()),
None => None,
}
}
}

/// A generalized authenticator which will keep tokens valid and store them.
///
/// It is the go-to helper to deal with any kind of supported authentication flow,
/// which will be kept valid and usable.
pub struct Authenticator<S> {
pub struct Authenticator<S, C, NC> {
auth_type: AuthenticationType,
storage: S,
// client ref ...
client: C,

_m: PhantomData<NC>
}

impl<S, C, NC> Authenticator<S, C, NC>
where S: TokenStorage,
NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> {

//
// fn new() -> Authenticator<S, C, NC> {

// }

// Will retrieve a token, from storage, retrieve a new one, or refresh
// an existing one.
// fn token() ->
}

0 comments on commit 091f1c0

Please sign in to comment.