1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! # "It's not that hard" OAuth 2.0
//!
//! OAuth 2.0 really isn't that hard, you know?
//!
//! Implementation of [RFC 6749](http://tools.ietf.org/html/rfc6749).
//!
//! `inth_oauth2` is on [Crates.io][crate] and [GitHub][github].
//!
//! [crate]: https://crates.io/crates/inth-oauth2
//! [github]: https://github.com/programble/inth-oauth2
//!
//! ## Providers
//!
//! Support for the following OAuth 2.0 providers is included:
//!
//! - Google
//!   - Web
//!   - Installed
//! - GitHub
//! - Imgur
//!
//! Support for other providers can be added by implementing the `Provider` trait.
//!
//! ## Token types
//!
//! The only supported token type is Bearer. Support for others can be added by implementing the
//! `Token` trait.
//!
//! ## Examples
//!
//! ### Creating a client
//!
//! ```
//! use inth_oauth2::Client;
//! use inth_oauth2::provider::google::Installed;
//!
//! let client = Client::new(
//!     Installed,
//!     String::from("client_id"),
//!     String::from("client_secret"),
//!     Some(String::from("redirect_uri")),
//! );
//! ```
//!
//! ### Constructing an authorization URI
//!
//! ```
//! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::google::Installed;
//! # let client = Client::new(Installed, String::new(), String::new(), None);
//! let auth_uri = client.auth_uri(Some("scope"), Some("state"));
//! println!("Authorize the application by clicking on the link: {}", auth_uri);
//! ```
//!
//! ### Requesting an access token
//!
//! ```no_run
//! # extern crate inth_oauth2;
//! # extern crate reqwest;
//! use std::io;
//! use inth_oauth2::{Client, Token};
//! # use inth_oauth2::provider::google::Installed;
//! # fn main() {
//! # let client = Client::new(Installed, String::new(), String::new(), None);
//!
//! let mut code = String::new();
//! io::stdin().read_line(&mut code).unwrap();
//!
//! let http = reqwest::Client::new();
//! let token = client.request_token(&http, code.trim()).unwrap();
//! println!("{}", token.access_token());
//! # }
//! ```
//!
//! ### Refreshing an access token
//!
//! ```no_run
//! # extern crate inth_oauth2;
//! # extern crate reqwest;
//! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::google::Installed;
//! # fn main() {
//! # let client = Client::new(Installed, String::new(), String::new(), None);
//! # let http = reqwest::Client::new();
//! # let token = client.request_token(&http, "").unwrap();
//! let token = client.refresh_token(&http, token, None).unwrap();
//! # }
//! ```
//!
//! ### Ensuring an access token is still valid
//!
//! ```no_run
//! # extern crate inth_oauth2;
//! # extern crate reqwest;
//! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::google::Installed;
//! # fn main() {
//! # let client = Client::new(Installed, String::new(), String::new(), None);
//! # let http = reqwest::Client::new();
//! # let mut token = client.request_token(&http, "").unwrap();
//! // Refresh token only if it has expired.
//! token = client.ensure_token(&http, token).unwrap();
//! # }
//! ```
//!
//! ### Using bearer access tokens
//!
//! ```no_run
//! # extern crate inth_oauth2;
//! # extern crate reqwest;
//! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::google::Installed;
//! use inth_oauth2::Token;
//!
//! # fn main() {
//! # let oauth_client = Client::new(Installed, String::new(), String::new(), None);
//! # let http = reqwest::Client::new();
//! # let token = oauth_client.request_token(&http, "").unwrap();
//! let request = http.get("https://example.com/resource")
//!     .bearer_auth(token.access_token())
//!     .build();
//! # }
//! ```
//!
//! ### Persisting tokens
//!
//! All token types implement `Serialize` and `Deserialize` from `serde`.
//!
//! ```no_run
//! # extern crate inth_oauth2;
//! # extern crate reqwest;
//! extern crate serde_json;
//! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::google::Installed;
//! # fn main() {
//! # let http = reqwest::Client::new();
//! # let client = Client::new(Installed, String::new(), String::new(), None);
//! # let token = client.request_token(&http, "").unwrap();
//! let json = serde_json::to_string(&token).unwrap();
//! # }
//! ```

#![warn(
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications,
    variant_size_differences,
)]

#[macro_use]
extern crate lazy_static;

#[macro_use]
extern crate serde_derive;

extern crate chrono;
extern crate reqwest;
extern crate serde_json;
extern crate url;

pub mod token;
pub mod provider;
pub mod error;
pub mod client;

pub use token::{Token, Lifetime};
pub use client::{Client, ClientError};