Crates.io | oauth2_noserver |
lib.rs | oauth2_noserver |
version | 0.1.2 |
source | src |
created_at | 2019-02-21 22:37:05.011348 |
updated_at | 2019-03-05 22:55:52.858008 |
description | Handles the oauth2 flow for installed apps that don't have a server side |
homepage | |
repository | https://github.com/nmrshll/oauth2-noserver-rs |
max_upload_size | |
id | 116374 |
size | 13,829 |
Handles the oauth2 flow for installed apps that don't have a server side.
Oauth was designed for letting third party servers access your data. Which makes it a bit overcomplicated for when you simply want your users to access their data through software that runs only on their computer. This project aims to simplify that a lot for Rust projects, as a library.
Once you have an oauth2 config...
let oauthConfig = oauth2::Config::new(
"someID",
"someSECRET",
"https://accounts.google.com/o/oauth2/v2/auth",
"https://www.googleapis.com/oauth2/v3/token",
)
.add_scope("https://www.googleapis.com/auth/calendar")
.add_scope("https://www.googleapis.com/auth/plus.me");
...use this to open the browser, ask your user to connect to the service, and get a token back to your program to act on behalf of the user:
let authenticator = oauth2_noserver::Authenticator::new(oauthConfig);
authenticator.authenticate().unwrap();
Add this to your Cargo.toml:
oauth2_noserver = "0.1.2"
http://localhost:14565/oauth/callback
, or replace with what you specified in Authenticator.set_redirect_url()
Authenticator.set_port(port)
: the lib will spin up a temporary server to await the token from the service on this port.
Use the same port in your redirect_url, both in the Authenticator config and when you create your Oauth credentials on
Optional. 14565 by default.
Authenticator.set_redirect_url(Into<String>)
: set it to the same redirect_url you used when creating your oauth app on the service
Optional. By default, http://localhost:14565/oauth/callback
.
fn main() -> Result<(), Box<std::error::Error>> {
let oauthConfig = Oauth2Config::new(
"someID",
"someSECRET",
"https://accounts.google.com/o/oauth2/v2/auth",
"https://www.googleapis.com/oauth2/v3/token",
)
.add_scope("https://www.googleapis.com/auth/calendar")
.add_scope("https://www.googleapis.com/auth/plus.me");
let authenticator = oauth2_noserver::Authenticator::new(oauthConfig);
authenticator.authenticate().unwrap();
Ok(())
}
## Fully configured
fn main() -> Result<(), Box<std::error::Error>> {
let oauthConfig = Oauth2Config::new(
"someID",
"someSECRET",
"https://accounts.google.com/o/oauth2/v2/auth",
"https://www.googleapis.com/oauth2/v3/token",
)
.add_scope("https://www.googleapis.com/auth/calendar")
.add_scope("https://www.googleapis.com/auth/plus.me");
const PORT: u16 = 14565;
let authenticator = oauth2_noserver::Authenticator::new(oauthConfig)
.set_port(PORT)
.set_redirect_url(format!("http://localhost:{}/oauth/callback", PORT));
authenticator.authenticate().unwrap();
Ok(())
}