Expand description

“It’s not that hard” OAuth 2.0

OAuth 2.0 really isn’t that hard, you know?

Implementation of RFC 6749.

inth_oauth2 is on Crates.io and GitHub.

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_async::Client;
use inth_oauth2_async::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

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

use std::io;
use inth_oauth2_async::{Client, Token};

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()).await.unwrap();
println!("{}", token.access_token());

Refreshing an access token

let token = client.refresh_token(&http, token, None).await.unwrap();

Ensuring an access token is still valid

// Refresh token only if it has expired.
token = client.ensure_token(&http, token).await.unwrap();

Using bearer access tokens

use inth_oauth2_async::Token;

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.

let json = serde_json::to_string(&token).unwrap();

Re-exports

Modules