use url::Url;
use token::{Token, Lifetime, Bearer, Static, Refresh};
pub trait Provider {
type Lifetime: Lifetime;
type Token: Token<Self::Lifetime>;
fn auth_uri(&self) -> &Url;
fn token_uri(&self) -> &Url;
fn credentials_in_body(&self) -> bool { false }
}
pub mod google {
use url::Url;
use token::{Bearer, Expiring, Refresh};
use super::Provider;
pub const REDIRECT_URI_OOB: &'static str = "urn:ietf:wg:oauth:2.0:oob";
pub const REDIRECT_URI_OOB_AUTO: &'static str = "urn:ietf:wg:oauth:2.0:oob:auto";
lazy_static! {
static ref AUTH_URI: Url = Url::parse("https://accounts.google.com/o/oauth2/v2/auth").unwrap();
static ref TOKEN_URI: Url = Url::parse("https://www.googleapis.com/oauth2/v4/token").unwrap();
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Web;
impl Provider for Web {
type Lifetime = Expiring;
type Token = Bearer<Expiring>;
fn auth_uri(&self) -> &Url { &AUTH_URI }
fn token_uri(&self) -> &Url { &TOKEN_URI }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Installed;
impl Provider for Installed {
type Lifetime = Refresh;
type Token = Bearer<Refresh>;
fn auth_uri(&self) -> &Url { &AUTH_URI }
fn token_uri(&self) -> &Url { &TOKEN_URI }
}
}
lazy_static! {
static ref GITHUB_AUTH_URI: Url = Url::parse("https://github.com/login/oauth/authorize").unwrap();
static ref GITHUB_TOKEN_URI: Url = Url::parse("https://github.com/login/oauth/access_token").unwrap();
static ref IMGUR_AUTH_URI: Url = Url::parse("https://api.imgur.com/oauth2/authorize").unwrap();
static ref IMGUR_TOKEN_URI: Url = Url::parse("https://api.imgur.com/oauth2/token").unwrap();
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GitHub;
impl Provider for GitHub {
type Lifetime = Static;
type Token = Bearer<Static>;
fn auth_uri(&self) -> &Url { &GITHUB_AUTH_URI }
fn token_uri(&self) -> &Url { &GITHUB_TOKEN_URI }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Imgur;
impl Provider for Imgur {
type Lifetime = Refresh;
type Token = Bearer<Refresh>;
fn auth_uri(&self) -> &Url { &IMGUR_AUTH_URI }
fn token_uri(&self) -> &Url { &IMGUR_TOKEN_URI }
}
#[test]
fn google_urls() {
let prov = google::Web;
prov.auth_uri();
prov.token_uri();
let prov = google::Installed;
prov.auth_uri();
prov.token_uri();
}
#[test]
fn github_urls() {
let prov = GitHub;
prov.auth_uri();
prov.token_uri();
}
#[test]
fn imgur_urls() {
let prov = Imgur;
prov.auth_uri();
prov.token_uri();
}