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 /src | |
| 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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 31 |
1 files changed, 21 insertions, 10 deletions
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, |
