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();
loop {
match store.get(scope_key, scopes) {
Ok(Some(t)) => {
if !t.expired() {
return Ok(t);
}
// Implement refresh flow.
let rr = RefreshFlow::refresh_token(
client,
appsecret,
&t.refresh_token.as_ref().unwrap(),
)
.await?;
Ok(Some(t)) if !t.expired() => {
// unexpired token found
return Ok(t);
}
Ok(Some(Token {
refresh_token: Some(refresh_token),
..
})) => {
// token is expired but has a refresh token.
let rr = RefreshFlow::refresh_token(client, appsecret, &refresh_token).await?;
match rr {
RefreshResult::Error(ref e) => {
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?;
if let Err(e) = store.set(scope_key, scopes, Some(t.clone())) {
match delegate.token_storage_failure(true, &e) {

View File

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