[][src]Crate inth_oauth2

"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::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

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

Refreshing an access token

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

Ensuring an access token is still valid

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

Using bearer access tokens

use inth_oauth2::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.

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

Re-exports

pub use token::Token;
pub use token::Lifetime;
pub use client::Client;

Modules

client

Client.

error

Errors.

provider

Providers.

token

Tokens.

Enums

ClientError

Errors that can occur during authorization.