inth_oauth2_async/
provider.rs

1//! Providers.
2
3use url::Url;
4
5use crate::token::{Token, Lifetime, Bearer, Static, Refresh};
6
7/// OAuth 2.0 providers.
8pub trait Provider {
9    /// The lifetime of tokens issued by the provider.
10    type Lifetime: Lifetime;
11
12    /// The type of token issued by the provider.
13    type Token: Token<Self::Lifetime>;
14
15    /// The authorization endpoint URI.
16    ///
17    /// See [RFC 6749, section 3.1](http://tools.ietf.org/html/rfc6749#section-3.1).
18    fn auth_uri(&self) -> &Url;
19
20    /// The token endpoint URI.
21    ///
22    /// See [RFC 6749, section 3.2](http://tools.ietf.org/html/rfc6749#section-3.2).
23    fn token_uri(&self) -> &Url;
24
25    /// Provider requires credentials via request body.
26    ///
27    /// Although not recommended by the RFC, some providers require `client_id` and `client_secret`
28    /// as part of the request body.
29    ///
30    /// See [RFC 6749, section 2.3.1](http://tools.ietf.org/html/rfc6749#section-2.3.1).
31    fn credentials_in_body(&self) -> bool { false }
32}
33
34/// Google OAuth 2.0 providers.
35///
36/// See [Using OAuth 2.0 to Access Google
37/// APIs](https://developers.google.com/identity/protocols/OAuth2).
38pub mod google {
39    use super::*;
40    use crate::token::Expiring;
41
42    /// Signals the server to return the authorization code by prompting the user to copy and
43    /// paste.
44    ///
45    /// See [Choosing a redirect URI][uri].
46    ///
47    /// [uri]: https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi
48    pub const REDIRECT_URI_OOB: &str = "urn:ietf:wg:oauth:2.0:oob";
49
50    /// Signals the server to return the authorization code in the page title.
51    ///
52    /// See [Choosing a redirect URI][uri].
53    ///
54    /// [uri]: https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi
55    pub const REDIRECT_URI_OOB_AUTO: &str = "urn:ietf:wg:oauth:2.0:oob:auto";
56
57    lazy_static! {
58        static ref AUTH_URI: Url = Url::parse("https://accounts.google.com/o/oauth2/v2/auth").unwrap();
59        static ref TOKEN_URI: Url = Url::parse("https://www.googleapis.com/oauth2/v4/token").unwrap();
60    }
61
62    /// Google OAuth 2.0 provider for web applications.
63    ///
64    /// See [Using OAuth 2.0 for Web Server
65    /// Applications](https://developers.google.com/identity/protocols/OAuth2WebServer).
66    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
67    pub struct Web;
68    impl Provider for Web {
69        type Lifetime = Expiring;
70        type Token = Bearer<Expiring>;
71        fn auth_uri(&self) -> &Url { &AUTH_URI }
72        fn token_uri(&self) -> &Url { &TOKEN_URI }
73    }
74
75    /// Google OAuth 2.0 provider for installed applications.
76    ///
77    /// See [Using OAuth 2.0 for Installed
78    /// Applications](https://developers.google.com/identity/protocols/OAuth2InstalledApp).
79    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
80    pub struct Installed;
81    impl Provider for Installed {
82        type Lifetime = Refresh;
83        type Token = Bearer<Refresh>;
84        fn auth_uri(&self) -> &Url { &AUTH_URI }
85        fn token_uri(&self) -> &Url { &TOKEN_URI }
86    }
87}
88
89lazy_static! {
90    static ref GITHUB_AUTH_URI: Url = Url::parse("https://github.com/login/oauth/authorize").unwrap();
91    static ref GITHUB_TOKEN_URI: Url = Url::parse("https://github.com/login/oauth/access_token").unwrap();
92    static ref IMGUR_AUTH_URI: Url = Url::parse("https://api.imgur.com/oauth2/authorize").unwrap();
93    static ref IMGUR_TOKEN_URI: Url = Url::parse("https://api.imgur.com/oauth2/token").unwrap();
94}
95
96/// GitHub OAuth 2.0 provider.
97///
98/// See [OAuth, GitHub Developer Guide](https://developer.github.com/v3/oauth/).
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub struct GitHub;
101impl Provider for GitHub {
102    type Lifetime = Static;
103    type Token = Bearer<Static>;
104    fn auth_uri(&self) -> &Url { &GITHUB_AUTH_URI }
105    fn token_uri(&self) -> &Url { &GITHUB_TOKEN_URI }
106}
107
108/// Imgur OAuth 2.0 provider.
109///
110/// See [OAuth 2.0, Imgur](https://api.imgur.com/oauth2).
111#[derive(Debug, Clone, Copy, PartialEq, Eq)]
112pub struct Imgur;
113impl Provider for Imgur {
114    type Lifetime = Refresh;
115    type Token = Bearer<Refresh>;
116    fn auth_uri(&self) -> &Url { &IMGUR_AUTH_URI }
117    fn token_uri(&self) -> &Url { &IMGUR_TOKEN_URI }
118}
119
120#[test]
121fn google_urls() {
122    let prov = google::Web;
123    prov.auth_uri();
124    prov.token_uri();
125    let prov = google::Installed;
126    prov.auth_uri();
127    prov.token_uri();
128}
129
130#[test]
131fn github_urls() {
132    let prov = GitHub;
133    prov.auth_uri();
134    prov.token_uri();
135}
136
137#[test]
138fn imgur_urls() {
139    let prov = Imgur;
140    prov.auth_uri();
141    prov.token_uri();
142}