inth_oauth2_async/
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-async
10//! [github]: https://github.com/wfraser/inth-oauth2-async
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_async::Client;
35//! use inth_oauth2_async::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_async::Client;
49//! # use inth_oauth2_async::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//! # #[cfg(feature="reqwest-client")] {
59//! use std::io;
60//! use inth_oauth2_async::{Client, Token};
61//! # use inth_oauth2_async::provider::google::Installed;
62//! # #[tokio::main]
63//! # async 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()).await.unwrap();
71//! println!("{}", token.access_token());
72//! # } }
73//! ```
74//!
75//! ### Refreshing an access token
76//!
77//! ```no_run
78//! # #[cfg(feature="reqwest-client")] {
79//! # use inth_oauth2_async::Client;
80//! # use inth_oauth2_async::provider::google::Installed;
81//! # #[tokio::main]
82//! # async 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, "").await.unwrap();
86//! let token = client.refresh_token(&http, token, None).await.unwrap();
87//! # } }
88//! ```
89//!
90//! ### Ensuring an access token is still valid
91//!
92//! ```no_run
93//! # #[cfg(feature="reqwest-client")] {
94//! # use inth_oauth2_async::Client;
95//! # use inth_oauth2_async::provider::google::Installed;
96//! # #[tokio::main]
97//! # async 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, "").await.unwrap();
101//! // Refresh token only if it has expired.
102//! token = client.ensure_token(&http, token).await.unwrap();
103//! # } }
104//! ```
105//!
106//! ### Using bearer access tokens
107//!
108//! ```no_run
109//! # #[cfg(feature="reqwest-client")] {
110//! # use inth_oauth2_async::Client;
111//! # use inth_oauth2_async::provider::google::Installed;
112//! use inth_oauth2_async::Token;
113//!
114//! # #[tokio::main]
115//! # async 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, "").await.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//! # #[cfg(feature="reqwest-client")] {
131//! # use inth_oauth2_async::Client;
132//! # use inth_oauth2_async::provider::google::Installed;
133//! # #[tokio::main]
134//! # async fn main() {
135//! # let http = reqwest::Client::new();
136//! # let client = Client::new(Installed, String::new(), String::new(), None);
137//! # let token = client.request_token(&http, "").await.unwrap();
138//! let json = serde_json::to_string(&token).unwrap();
139//! # } }
140//! ```
141
142#![warn(
143    missing_docs,
144    missing_debug_implementations,
145    missing_copy_implementations,
146    trivial_casts,
147    trivial_numeric_casts,
148    unused_extern_crates,
149    unused_import_braces,
150    unused_qualifications,
151    variant_size_differences,
152)]
153
154#[macro_use]
155extern crate lazy_static;
156
157#[macro_use]
158extern crate serde_derive;
159
160pub mod token;
161pub mod provider;
162pub mod error;
163pub mod client;
164
165pub use token::{Token, Lifetime};
166pub use client::{Client, ClientError};