Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the latest token instead of panicking, even if it's expired #1

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/authentication_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl AuthenticationManager {
pub async fn get_token(&self, scopes: &[&str]) -> Result<Token, GCPAuthError> {
let mut sa = self.service_account.lock().await;
let mut token = sa.get_token(scopes);
if token.is_none() {

if token.is_none() || token.clone().unwrap().has_expired() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code is just refactor as it keeps the same functionality (returning library error after refresh returned invalid token)

sa.refresh_token(&self.client, scopes).await?;
token = sa.get_token(scopes);
}
Expand Down
11 changes: 2 additions & 9 deletions src/custom_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ impl CustomServiceAccount {
#[async_trait]
impl ServiceAccount for CustomServiceAccount {
fn get_token(&self, scopes: &[&str]) -> Option<Token> {
let key: Vec<_> = scopes.iter().map(|x| (*x).to_string()).collect();
let token = self
.tokens
.get(&key);

if token.is_none() || token.unwrap().has_expired() {
return None;
}
Some(token.unwrap().clone())
let key: Vec<_> = scopes.iter().map(|x| x.to_string()).collect();
self.tokens.get(&key).cloned()
}

async fn refresh_token(&mut self, client: &HyperClient, scopes: &[&str]) -> Result<(), GCPAuthError> {
Expand Down
3 changes: 0 additions & 3 deletions src/default_authorized_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ impl DefaultAuthorizedUser {
#[async_trait]
impl ServiceAccount for DefaultAuthorizedUser {
fn get_token(&self, _scopes: &[&str]) -> Option<Token> {
if self.token.has_expired() {
return None;
}
Some(self.token.clone())
}

Expand Down
3 changes: 0 additions & 3 deletions src/default_service_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ impl DefaultServiceAccount {
#[async_trait]
impl ServiceAccount for DefaultServiceAccount {
fn get_token(&self, _scopes: &[&str]) -> Option<Token> {
if self.token.has_expired() {
return None;
}
Some(self.token.clone())
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{DateTime, Utc};
use serde::{Deserializer};
use serde::Deserializer;
use serde::{Deserialize, Serialize};

/// Represents an access token. All access tokens are Bearer tokens.
Expand All @@ -17,7 +17,7 @@ pub struct Token {
impl Token {
pub(crate) fn has_expired(&self) -> bool {
self.expires_at
.map(|expiration_time| expiration_time - chrono::Duration::minutes(1) <= Utc::now())
.map(|expiration_time| expiration_time - chrono::Duration::seconds(30) <= Utc::now())
.unwrap_or(false)
}

Expand Down