inth_oauth2_async/
provider.rs1use url::Url;
4
5use crate::token::{Token, Lifetime, Bearer, Static, Refresh};
6
7pub trait Provider {
9 type Lifetime: Lifetime;
11
12 type Token: Token<Self::Lifetime>;
14
15 fn auth_uri(&self) -> &Url;
19
20 fn token_uri(&self) -> &Url;
24
25 fn credentials_in_body(&self) -> bool { false }
32}
33
34pub mod google {
39 use super::*;
40 use crate::token::Expiring;
41
42 pub const REDIRECT_URI_OOB: &str = "urn:ietf:wg:oauth:2.0:oob";
49
50 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 #[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 #[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#[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#[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}