Crate oauth2 [−] [src]
A simple implementation of the OAuth2 flow, trying to adhere as much as possible to the RFC.
Getting started
Example
use oauth2::Config; // Create an OAuth2 config by specifying the client ID, client secret, authorization URL and token URL. let mut config = Config::new("client_id", "client_secret", "http://authorize", "http://token"); // Set the desired scopes. config = config.add_scope("read"); config = config.add_scope("write"); // Set the URL the user will be redirected to after the authorization process. config = config.set_redirect_url("http://redirect"); // Set a state parameter (optional, but recommended). config = config.set_state("1234"); // Generate the full authorization URL. // This is the URL you should redirect the user to, in order to trigger the authorization process. println!("Browse to: {}", config.authorize_url()); // Once the user has been redirected to the redirect URL, you'll have access to the authorization code. // Now you can trade it for an access token. let token_result = config.exchange_code("some authorization code"); // Unwrapping token_result will either produce a Token or a TokenError.
The client credentials grant type
You can ask for a client credentials access token by calling the Config::exchange_client_credentials
method.
Example
use oauth2::Config; let mut config = Config::new("client_id", "client_secret", "http://authorize", "http://token"); config = config.add_scope("read"); config = config.set_redirect_url("http://redirect"); let token_result = config.exchange_client_credentials();
The password grant type
You can ask for a password access token by calling the Config::exchange_password
method, while including
the username and password.
Example
use oauth2::Config; let mut config = Config::new("client_id", "client_secret", "http://authorize", "http://token"); config = config.add_scope("read"); config = config.set_redirect_url("http://redirect"); let token_result = config.exchange_password("user", "pass");
Setting a different response type
The RFC specifies various response types.
The crate defaults to the code response type, but you can configure it to other values as well, by
calling the Config::set_response_type
method.
Example
use oauth2::{Config, ResponseType}; let mut config = Config::new("client_id", "client_secret", "http://authorize", "http://token"); config = config.set_response_type(ResponseType::Token);
Other examples
More specific implementations are available as part of the examples:
Structs
Config |
Stores the configuration for an OAuth2 client. |
Token |
The token returned after a successful authorization process. |
TokenError |
An error that occured after a failed authorization process. |
Enums
ErrorType |
An OAuth2-specific error type or other. |
ResponseType |
The possible values for the |