Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Feb 28, 2015
2 parents 515e128 + dea3e6b commit 501c792
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 226 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
21 changes: 16 additions & 5 deletions examples/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate open;
use chrono::{Local};
use getopts::{HasArg,Options,Occur,Fail};
use std::env;
use std::default::Default;
use std::time::Duration;
use std::old_io::timer::sleep;

Expand Down Expand Up @@ -46,13 +47,21 @@ fn main() {
return
}

let client_id = m.opt_str("c").unwrap();
let client_secret = m.opt_str("s").unwrap();
let secret = oauth2::ApplicationSecret {
client_id: m.opt_str("c").unwrap(),
client_secret: m.opt_str("s").unwrap(),
token_uri: Default::default(),
auth_uri: Default::default(),
redirect_uris: Default::default(),
client_email: None,
auth_provider_x509_cert_url: None,
client_x509_cert_url: None
};

println!("THIS PROGRAM PRINTS ALL COMMUNICATION TO STDERR !!!");

struct StdoutHandler;
impl oauth2::DeviceFlowHelperDelegate for StdoutHandler {
impl oauth2::AuthenticatorDelegate for StdoutHandler {
fn present_user_code(&mut self, pi: oauth2::PollInformation) {
println!("Please enter '{}' at {} and authenticate the application for the\n\
given scopes. This is not a test !\n\
Expand All @@ -70,8 +79,10 @@ fn main() {
let client = hyper::Client::with_connector(mock::TeeConnector {
connector: hyper::net::HttpConnector(None)
});
if let Some(t) = oauth2::DeviceFlowHelper::new(&mut StdoutHandler)
.retrieve_token(client, &client_id, &client_secret, &m.free) {

if let Some(t) = oauth2::Authenticator::new(&secret, StdoutHandler, client,
oauth2::NullStorage, None)
.token(&m.free) {
println!("Authentication granted !");
println!("You should store the following information for use, or revoke it.");
println!("All dates are given in UTC.");
Expand Down
33 changes: 16 additions & 17 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use chrono::{DateTime, UTC, TimeZone};
use std::marker::MarkerTrait;

/// A marker trait for all Flows
pub trait Flow : MarkerTrait {
fn type_id() -> FlowType;
}

/// Represents a token as returned by OAuth2 servers.
///
Expand All @@ -11,7 +17,7 @@ use chrono::{DateTime, UTC, TimeZone};
/// 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 @@ -23,21 +29,13 @@ pub struct Token {
/// access_token will expire after this amount of time.
/// Prefer using expiry_date()
pub expires_in: Option<i64>,

/// timestamp is seconds since epoch indicating when the token will expire in absolute terms.
/// use expiry_date() to convert to DateTime.
pub expires_in_timestamp: Option<i64>,
}

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 Expand Up @@ -68,23 +66,24 @@ impl Token {
}

/// All known authentication types, for suitable constants
pub enum AuthenticationType {
#[derive(Copy)]
pub enum FlowType {
/// [device authentication](https://developers.google.com/youtube/v3/guides/authentication#devices)
Device,
}

impl Str for AuthenticationType {
impl Str for FlowType {
/// Converts itself into a URL string
fn as_slice(&self) -> &'static str {
match *self {
AuthenticationType::Device => "https://accounts.google.com/o/oauth2/device/code",
FlowType::Device => "https://accounts.google.com/o/oauth2/device/code",
}
}
}

/// Represents either 'installed' or 'web' applications in a json secrets file.
/// See `ConsoleApplicationSecret` for more information
#[derive(RustcDecodable, RustcEncodable)]
#[derive(RustcDecodable, RustcEncodable, Clone)]
pub struct ApplicationSecret {
/// The client ID.
pub client_id: String,
Expand All @@ -109,16 +108,16 @@ pub struct ApplicationSecret {
/// as returned by the [google developer console](https://code.google.com/apis/console)
#[derive(RustcDecodable, RustcEncodable)]
pub struct ConsoleApplicationSecret {
web: Option<ApplicationSecret>,
installed: Option<ApplicationSecret>
pub web: Option<ApplicationSecret>,
pub installed: Option<ApplicationSecret>
}


#[cfg(test)]
mod tests {
pub mod tests {
use super::*;

const SECRET: &'static str = "{\"installed\":{\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\",\"client_secret\":\"UqkDJd5RFwnHoiG5x5Rub8SI\",\"token_uri\":\"https://accounts.google.com/o/oauth2/token\",\"client_email\":\"\",\"redirect_uris\":[\"urn:ietf:wg:oauth:2.0:oob\",\"oob\"],\"client_x509_cert_url\":\"\",\"client_id\":\"14070749909-vgip2f1okm7bkvajhi9jugan6126io9v.apps.googleusercontent.com\",\"auth_provider_x509_cert_url\":\"https://www.googleapis.com/oauth2/v1/certs\"}}";
pub const SECRET: &'static str = "{\"installed\":{\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\",\"client_secret\":\"UqkDJd5RFwnHoiG5x5Rub8SI\",\"token_uri\":\"https://accounts.google.com/o/oauth2/token\",\"client_email\":\"\",\"redirect_uris\":[\"urn:ietf:wg:oauth:2.0:oob\",\"oob\"],\"client_x509_cert_url\":\"\",\"client_id\":\"14070749909-vgip2f1okm7bkvajhi9jugan6126io9v.apps.googleusercontent.com\",\"auth_provider_x509_cert_url\":\"https://www.googleapis.com/oauth2/v1/certs\"}}";

#[test]
fn console_secret() {
Expand Down
Loading

0 comments on commit 501c792

Please sign in to comment.