From a7216b773ab52c08728ad841a935381d9c2e3050 Mon Sep 17 00:00:00 2001 From: Dominik George <dominik.george@teckids.org> Date: Sat, 8 May 2021 17:40:05 +0200 Subject: [PATCH] [Cache] Implement removal of user token file --- src/cache.rs | 22 +++++++++++++++++++--- src/lib.rs | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index f1d0f14..fa9cb0e 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -13,6 +13,8 @@ * limitations under the License. */ +use crate::BASE_NAME; + use lazy_static::lazy_static; use std::collections::HashMap; use std::convert::From; @@ -24,9 +26,13 @@ use std::ffi::CString; use oauth2::basic::BasicTokenResponse; use std::env; +use std::fs::remove_file; +use std::io; +use std::path::PathBuf; use xdg::{BaseDirectories,BaseDirectoriesError}; const TOKEN_DEFAULT_EXPIRES: u64 = 24 * 60 * 60; +const USER_TOKEN_FILENAME: &str = "user_token.json"; struct UserToken { access_token: String, @@ -61,7 +67,7 @@ impl From<BasicTokenResponse> for UserToken { struct Cache { user_tokens: HashMap<String, UserToken>, - original_euid: uid_t + original_euid: uid_t, } impl Cache { @@ -103,7 +109,7 @@ impl Cache { let user_home = CString::from_raw((*getpwnam(nam.as_ptr())).pw_dir).to_str().unwrap(); env::set_var("HOME", user_home); - let base_dirs = BaseDirectories::new()?; + let base_dirs = BaseDirectories::with_prefix(BASE_NAME)?; if saved_home != None { env::set_var("HOME", saved_home.unwrap()); @@ -114,6 +120,13 @@ impl Cache { return Ok(base_dirs); } + fn place_user_cache_file(&self, username: String, filename: &str) -> Result<PathBuf, io::Error> { + match self.get_user_xdg_base_directories(username) { + Ok(b) => b.place_cache_file(filename), + Err(e) => Err(io::Error::new(io::ErrorKind::NotFound, e)) + } + } + pub fn load_user_token(&self, owner: String) -> Option<&UserToken> { return self.user_tokens.get(&owner); } @@ -127,7 +140,10 @@ impl Cache { // Try to remove user's token cache file self.drop_privileges(owner).ok(); - // FIXME Add delete code here + match self.place_user_cache_file(owner, USER_TOKEN_FILENAME) { + Ok(path) => remove_file(path), + Err(e) => Err(e) + }; self.restore_privileges(); } diff --git a/src/lib.rs b/src/lib.rs index c4eae87..9f87bf0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +const BASE_NAME: &str = "nss_pam_oidc"; + // Modules and macro imports for our own code #[macro_use] extern crate log; mod cache; -- GitLab