-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:barduinor/box-rust-sdk
- Loading branch information
Showing
17 changed files
with
296 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Cargo.lock | |
|
||
.DS_Store | ||
.access_token.json | ||
*.cache.json | ||
.env | ||
*.env | ||
*.cache.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
mod oauth; | ||
|
||
use rusty_box::{BoxAPIError, BoxClient, Config, OAuth}; | ||
|
||
use crate::oauth::{authorize_app, storage}; | ||
use std::env; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), BoxAPIError> { | ||
dotenv::from_filename(".oauth.env").expect("Failed to read .env file"); | ||
|
||
let client_id = env::var("CLIENT_ID").expect("CLIENT_ID not set"); | ||
let client_secret = env::var("CLIENT_SECRET").expect("CLIENT_SECRET not set"); | ||
let redirect_uri = env::var("REDIRECT_URI").expect("REDIRECT_URI not set"); | ||
|
||
let config = Config::new(); | ||
let oauth = OAuth::new( | ||
config, | ||
client_id, | ||
client_secret, | ||
Some(storage::save_access_token), | ||
); | ||
|
||
// Load the OAuth token from the cache file | ||
let oauth_json = oauth::storage::load_access_token(); | ||
|
||
let oauth = match oauth_json { | ||
Ok(oauth_json) => { | ||
println!("Cached token found, refreshing"); | ||
let oauth: OAuth = | ||
serde_json::from_str(&oauth_json).expect("Failed to parse cached token"); | ||
oauth | ||
} | ||
Err(_) => { | ||
println!("No cached token found, authorizing app"); | ||
authorize_app::authorize_app(oauth, Some(redirect_uri)).await? | ||
} | ||
}; | ||
|
||
let mut client = BoxClient::new(Box::new(oauth)); | ||
|
||
let me = rusty_box::users_api::me(&mut client, None).await; | ||
println!("Me:\n{me:#?}\n"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use super::http_request_listener; | ||
use http_request_listener::request_process; | ||
use rusty_box::{AuthError, BoxAPIError, OAuth}; | ||
|
||
static HOSTNAME: &str = "127.0.0.1"; | ||
const PORT: i16 = 5000; | ||
|
||
pub async fn authorize_app( | ||
mut oauth: OAuth, | ||
redirect_uri: Option<String>, | ||
) -> Result<OAuth, BoxAPIError> { | ||
let (authorization_url, state_out) = oauth.authorization_url(redirect_uri, None, None)?; | ||
|
||
webbrowser::open(&authorization_url).expect("Failed to open browser"); | ||
|
||
let hostname_port = HOSTNAME.to_owned() + ":" + &PORT.to_string(); | ||
let server = tiny_http::Server::http(hostname_port).unwrap(); | ||
println!("Listening on {}", server.server_addr()); | ||
|
||
let (code, state_in) = match request_process(server) { | ||
Ok((code, state)) => (code, state), | ||
Err(e) => return Err(BoxAPIError::AuthError(AuthError::Generic(e))), | ||
}; | ||
if state_in != state_out { | ||
return Err(BoxAPIError::AuthError(AuthError::Generic( | ||
"State mismatch".to_string(), | ||
))); | ||
} | ||
|
||
oauth.request_access_token(code).await?; | ||
Ok(oauth) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use url::Url; | ||
|
||
#[derive(Debug, serde::Serialize, serde::Deserialize)] | ||
pub struct UrlParams { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub code: Option<String>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub state: Option<String>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub error: Option<String>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub error_description: Option<String>, | ||
} | ||
|
||
pub fn request_process(server: tiny_http::Server) -> Result<(String, String), String> { | ||
match server.recv() { | ||
Ok(rq) => { | ||
let base_url = Url::parse("http://127.0.0.1").expect("Error parsing base URL"); | ||
|
||
let url = base_url | ||
.join(rq.url()) | ||
.expect("Error parsing URL from request"); | ||
|
||
let query_params: UrlParams = match serde_qs::from_str(url.query().unwrap_or_default()) | ||
{ | ||
Ok(query_params) => query_params, | ||
Err(_) => return Err("Error srializing url query".to_string()), | ||
}; | ||
|
||
match query_params.code { | ||
Some(code) => { | ||
let response = tiny_http::Response::empty(200); | ||
rq.respond(response) | ||
.expect("Error sending response local server"); | ||
Ok((code, query_params.state.unwrap_or_default())) | ||
} | ||
None => Err(format!( | ||
"{} {}", | ||
query_params.error.unwrap_or_default(), | ||
query_params.error_description.unwrap_or_default() | ||
)), | ||
} | ||
} | ||
Err(_) => Err("Error receiving request from local server".to_string()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod authorize_app; | ||
pub mod http_request_listener; | ||
pub mod storage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub fn save_access_token(json: String) { | ||
std::fs::write(".token.cache.json", json).expect("Unable to save access token") | ||
} | ||
|
||
pub fn load_access_token() -> std::io::Result<String> { | ||
std::fs::read_to_string(".token.cache.json") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.