Skip to content
Snippets Groups Projects
Verified Commit a7216b77 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

[Cache] Implement removal of user token file

parent 559454b2
No related merge requests found
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::BASE_NAME;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::From; use std::convert::From;
...@@ -24,9 +26,13 @@ use std::ffi::CString; ...@@ -24,9 +26,13 @@ use std::ffi::CString;
use oauth2::basic::BasicTokenResponse; use oauth2::basic::BasicTokenResponse;
use std::env; use std::env;
use std::fs::remove_file;
use std::io;
use std::path::PathBuf;
use xdg::{BaseDirectories,BaseDirectoriesError}; use xdg::{BaseDirectories,BaseDirectoriesError};
const TOKEN_DEFAULT_EXPIRES: u64 = 24 * 60 * 60; const TOKEN_DEFAULT_EXPIRES: u64 = 24 * 60 * 60;
const USER_TOKEN_FILENAME: &str = "user_token.json";
struct UserToken { struct UserToken {
access_token: String, access_token: String,
...@@ -61,7 +67,7 @@ impl From<BasicTokenResponse> for UserToken { ...@@ -61,7 +67,7 @@ impl From<BasicTokenResponse> for UserToken {
struct Cache { struct Cache {
user_tokens: HashMap<String, UserToken>, user_tokens: HashMap<String, UserToken>,
original_euid: uid_t original_euid: uid_t,
} }
impl Cache { impl Cache {
...@@ -103,7 +109,7 @@ impl Cache { ...@@ -103,7 +109,7 @@ impl Cache {
let user_home = CString::from_raw((*getpwnam(nam.as_ptr())).pw_dir).to_str().unwrap(); let user_home = CString::from_raw((*getpwnam(nam.as_ptr())).pw_dir).to_str().unwrap();
env::set_var("HOME", user_home); env::set_var("HOME", user_home);
let base_dirs = BaseDirectories::new()?; let base_dirs = BaseDirectories::with_prefix(BASE_NAME)?;
if saved_home != None { if saved_home != None {
env::set_var("HOME", saved_home.unwrap()); env::set_var("HOME", saved_home.unwrap());
...@@ -114,6 +120,13 @@ impl Cache { ...@@ -114,6 +120,13 @@ impl Cache {
return Ok(base_dirs); 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> { pub fn load_user_token(&self, owner: String) -> Option<&UserToken> {
return self.user_tokens.get(&owner); return self.user_tokens.get(&owner);
} }
...@@ -127,7 +140,10 @@ impl Cache { ...@@ -127,7 +140,10 @@ impl Cache {
// Try to remove user's token cache file // Try to remove user's token cache file
self.drop_privileges(owner).ok(); 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(); self.restore_privileges();
} }
......
const BASE_NAME: &str = "nss_pam_oidc";
// Modules and macro imports for our own code // Modules and macro imports for our own code
#[macro_use] extern crate log; #[macro_use] extern crate log;
mod cache; mod cache;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment