forked from googleworkspace/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_storage.rs
More file actions
153 lines (129 loc) · 4.84 KB
/
Copy pathtoken_storage.rs
File metadata and controls
153 lines (129 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex;
use yup_oauth2::storage::{TokenInfo, TokenStorage, TokenStorageError};
/// A custom token storage implementation for `yup-oauth2` that encrypts
/// the cached tokens at rest using the AES key derived from the OS keyring.
pub struct EncryptedTokenStorage {
file_path: PathBuf,
// Add memory cache since TokenStorage getters can be called frequently
cache: Arc<Mutex<Option<HashMap<String, TokenInfo>>>>,
}
impl EncryptedTokenStorage {
pub fn new(path: PathBuf) -> Self {
Self {
file_path: path,
cache: Arc::new(Mutex::new(None)),
}
}
async fn load_from_disk(&self) -> HashMap<String, TokenInfo> {
let data = match tokio::fs::read(&self.file_path).await {
Ok(d) => d,
Err(_) => return HashMap::new(), // File doesn't exist yet — normal on first run
};
let decrypted = match crate::credential_store::decrypt(&data) {
Ok(d) => d,
Err(e) => {
eprintln!(
"warning: failed to decrypt token cache ({}): {e:#}",
self.file_path.display()
);
eprintln!("hint: you may need to re-authenticate with `gws auth login`");
return HashMap::new();
}
};
let json = match String::from_utf8(decrypted) {
Ok(j) => j,
Err(e) => {
eprintln!("warning: token cache contains invalid UTF-8: {e}");
return HashMap::new();
}
};
match serde_json::from_str(&json) {
Ok(map) => map,
Err(e) => {
eprintln!("warning: failed to parse token cache JSON: {e}");
HashMap::new()
}
}
}
async fn save_to_disk(&self, map: &HashMap<String, TokenInfo>) -> anyhow::Result<()> {
let json = serde_json::to_string(map)?;
let encrypted = crate::credential_store::encrypt(json.as_bytes())?;
if let Some(parent) = self.file_path.parent() {
let _ = tokio::fs::create_dir_all(parent).await;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700));
}
}
// Write atomically via a sibling .tmp file + rename.
crate::fs_util::atomic_write_async(&self.file_path, encrypted.as_slice()).await?;
Ok(())
}
// Helper to join scopes consistently for cache keys
fn cache_key(scopes: &[&str]) -> String {
let mut s: Vec<&str> = scopes.to_vec();
s.sort_unstable();
s.dedup();
s.join(" ")
}
}
#[async_trait::async_trait]
impl TokenStorage for EncryptedTokenStorage {
async fn set(&self, scopes: &[&str], token: TokenInfo) -> Result<(), TokenStorageError> {
let mut map_lock = self.cache.lock().await;
// Initialize cache if this is the first write
if map_lock.is_none() {
*map_lock = Some(self.load_from_disk().await);
}
if let Some(map) = map_lock.as_mut() {
map.insert(Self::cache_key(scopes), token);
self.save_to_disk(map)
.await
.map_err(|e| TokenStorageError::Other(std::borrow::Cow::Owned(e.to_string())))?;
}
Ok(())
}
async fn get(&self, scopes: &[&str]) -> Option<TokenInfo> {
let mut map_lock = self.cache.lock().await;
if map_lock.is_none() {
*map_lock = Some(self.load_from_disk().await);
}
if let Some(map) = map_lock.as_ref() {
let key = Self::cache_key(scopes);
if let Some(token) = map.get(&key) {
return Some(token.clone());
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[tokio::test]
async fn test_encrypted_token_storage_new() {
let path = PathBuf::from("/fake/path/to/token.json");
let storage = EncryptedTokenStorage::new(path.clone());
assert_eq!(storage.file_path, path);
let cache_lock = storage.cache.lock().await;
assert!(cache_lock.is_none());
}
}