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

Implement stubs for cache handling of user tokens

parent f6189ced
No related branches found
No related tags found
No related merge requests found
...@@ -13,20 +13,75 @@ ...@@ -13,20 +13,75 @@
* limitations under the License. * limitations under the License.
*/ */
use lazy_static::lazy_static;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::From;
use std::time::SystemTime;
use oauth2::basic::BasicTokenResponse;
const TOKEN_DEFAULT_EXPIRES: u64 = 24 * 60 * 60;
struct UserToken { struct UserToken {
access_token: String, access_token: String,
expires_in: u32, expires_at: u64,
refresh_token: String, refresh_token: Option<String>,
scope: String, }
token_type: String,
impl UserToken {
fn is_expired(&self) -> bool {
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(d) => d.as_secs() >= self.expires_at,
Err(_) => true
}
}
}
impl From<BasicTokenResponse> for UserToken {
fn from(response: BasicTokenResponse) -> Self {
UserToken {
access_token: response.access_token.secret().to_string(),
expires_at: match response.expires_in {
Some(duration) => duration,
None => TOKEN_DEFAULT_EXPIRES
} + SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).ok().unwrap().as_secs(),
refresh_token: match response.refresh_token {
Some(t) => Some(t.secret().to_string()),
None => None
}
}
}
} }
static mut USER_TOKENS: Option<HashMap<String, UserToken>> = None; struct Cache {
user_tokens: HashMap<String, UserToken>
}
impl Cache {
fn new() -> Cache {
Cache {
user_tokens: HashMap::new()
}
}
fn load_user_token(&self, owner: String) -> Option<&UserToken> {
return self.user_tokens.get(&owner);
}
fn setup_cache() { fn save_user_token(&self, owner: String, token: UserToken) {
unsafe { self.user_tokens.insert(owner, token);
USER_TOKENS = Some(HashMap::new());
} }
fn cleanup_tokens(&self) {
for (owner, token) in self.user_tokens {
if token.is_expired() {
self.user_tokens.remove(&owner);
}
}
}
}
lazy_static! {
static ref CACHE: Cache = Cache::new();
} }
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