Authenticator should handle the server not returning a refresh_token.

Currently the authenticator will panic when trying to refresh an expired
token that does not have a refresh token. This change handles it so that
the authenticator will only attempt a refresh when a refresh_token
exists, and otherwise will attempt to retrieve a fresh token.
This commit is contained in:
Glenn Griffin
2019-11-12 10:12:08 -08:00
parent 060eb92bf7
commit e1f0819156
2 changed files with 18 additions and 14 deletions

View File

@@ -196,17 +196,16 @@ where
let appsecret = gettoken.application_secret(); let appsecret = gettoken.application_secret();
loop { loop {
match store.get(scope_key, scopes) { match store.get(scope_key, scopes) {
Ok(Some(t)) => { Ok(Some(t)) if !t.expired() => {
if !t.expired() { // unexpired token found
return Ok(t); return Ok(t);
} }
// Implement refresh flow. Ok(Some(Token {
let rr = RefreshFlow::refresh_token( refresh_token: Some(refresh_token),
client, ..
appsecret, })) => {
&t.refresh_token.as_ref().unwrap(), // token is expired but has a refresh token.
) let rr = RefreshFlow::refresh_token(client, appsecret, &refresh_token).await?;
.await?;
match rr { match rr {
RefreshResult::Error(ref e) => { RefreshResult::Error(ref e) => {
delegate.token_refresh_failed( delegate.token_refresh_failed(
@@ -236,7 +235,12 @@ where
} }
} }
} }
Ok(None) => { Ok(None)
| Ok(Some(Token {
refresh_token: None,
..
})) => {
// no token in the cache or the token returned does not contain a refresh token.
let t = gettoken.token(scopes).await?; let t = gettoken.token(scopes).await?;
if let Err(e) = store.set(scope_key, scopes, Some(t.clone())) { if let Err(e) = store.set(scope_key, scopes, Some(t.clone())) {
match delegate.token_storage_failure(true, &e) { match delegate.token_storage_failure(true, &e) {

View File

@@ -255,7 +255,7 @@ where
#[derive(Deserialize)] #[derive(Deserialize)]
struct JSONTokenResponse { struct JSONTokenResponse {
access_token: String, access_token: String,
refresh_token: String, refresh_token: Option<String>,
token_type: String, token_type: String,
expires_in: Option<i64>, expires_in: Option<i64>,
} }
@@ -270,7 +270,7 @@ where
}) => { }) => {
let mut token = Token { let mut token = Token {
access_token, access_token,
refresh_token: Some(refresh_token), refresh_token,
token_type, token_type,
expires_in, expires_in,
expires_in_timestamp: None, expires_in_timestamp: None,