inth_oauth2/
lib.rs

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