diff options
| author | Teddy Wing | 2021-05-22 21:08:40 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-05-22 21:09:30 +0200 |
| commit | 7c9166dcb66433ca3c37351ed5c892aa04bf589a (patch) | |
| tree | bd240aa06a4bb9f4beb3364ad243345e243d75fd | |
| parent | 5eea25e9129b7d56a17ed3075dcb2a115ba78cda (diff) | |
| download | google-calendar-rsvp-7c9166dcb66433ca3c37351ed5c892aa04bf589a.tar.bz2 | |
Get OAuth secret and token from XDG data directory
Stop using the `google-calendar3` directory and secret file and use an
application-specific one.
| -rw-r--r-- | Cargo.lock | 17 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 31 |
3 files changed, 29 insertions, 21 deletions
@@ -261,13 +261,13 @@ dependencies = [ "chrono", "exitcode", "google-calendar3", - "home", "hyper", "hyper-rustls", "mailparse", "regex", "serde_json", "tokio", + "xdg", "yup-oauth2", ] @@ -323,15 +323,6 @@ dependencies = [ ] [[package]] -name = "home" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654" -dependencies = [ - "winapi", -] - -[[package]] name = "http" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1128,6 +1119,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] +name = "xdg" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" + +[[package]] name = "yup-oauth2" version = "5.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -9,11 +9,11 @@ base64 = "0.13.0" chrono = "0.4.19" exitcode = "1.1.2" google-calendar3 = "2.0.4+20210327" -home = "0.5.3" hyper = "0.14.7" hyper-rustls = "0.22.1" mailparse = "0.13.4" regex = "1.5.4" serde_json = "1.0.64" tokio = { version = "1.6.0", features = ["rt-multi-thread"] } +xdg = "2.2.0" yup-oauth2 = "5.1.0" diff --git a/src/main.rs b/src/main.rs index fb5853c..f8b7d26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,21 @@ -use anyhow; +use anyhow::{self, Context}; use base64; use chrono::DateTime; use google_calendar3::api::{Event, EventAttendee}; use google_calendar3::CalendarHub; -use home; use hyper; use hyper_rustls; use mailparse; use regex::Regex; use tokio; +use xdg; use yup_oauth2 as oauth2; use std::env; use std::fmt; use std::fs; use std::io::{self, Read}; +use std::path::{Path, PathBuf}; use std::process; use std::str; @@ -154,11 +155,7 @@ async fn rsvp(event_id: &str, response: &EventResponseStatus) -> anyhow::Result< secret, oauth2::InstalledFlowReturnMethod::HTTPRedirect, ) - .persist_tokens_to_disk( - home::home_dir() - .ok_or(anyhow::anyhow!("error getting home directory"))? - .join(".google-service-cli/google-calendar-rsvp") - ) + .persist_tokens_to_disk(local_data_file("token.json")?) .build() .await?; @@ -202,9 +199,14 @@ async fn rsvp(event_id: &str, response: &EventResponseStatus) -> anyhow::Result< fn secret_from_file() -> anyhow::Result<oauth2::ApplicationSecret> { let f = fs::File::open( - home::home_dir() - .ok_or(anyhow::anyhow!("error getting home directory"))? - .join(".google-service-cli/calendar3-secret.json"), + local_data_file("oauth-secret.json") + .context(format!( + "Missing OAuth2 secret file. Create an application on the Google Developer Console (https://console.developers.google.com/) and download the JSON secret file to '{}'.", + xdg::BaseDirectories::with_prefix("google-calendar-rsvp")? + .get_data_home() + .join("oauth-secret.json") + .display() + ))?, )?; let console_secret: oauth2::ConsoleApplicationSecret = serde_json::from_reader(f)?; @@ -213,6 +215,15 @@ fn secret_from_file() -> anyhow::Result<oauth2::ApplicationSecret> { .ok_or(anyhow::anyhow!("OAuth2 application secret not found")) } +fn local_data_file<P: AsRef<Path>>(file: P) -> anyhow::Result<PathBuf> { + let xdg_dirs = xdg::BaseDirectories::with_prefix("google-calendar-rsvp")?; + + Ok( + xdg_dirs.find_data_file(&file) + .ok_or(anyhow::anyhow!("error getting XDG data path"))? + ) +} + fn event_id_from_base64(event_id: &str) -> anyhow::Result<String> { let decoded = match base64::decode(event_id) { Ok(d) => d, |
