From 8112f5d62558fa12f1137ecb6ccbc89b8dae67e2 Mon Sep 17 00:00:00 2001 From: Rafael Renan Pacheco Date: Mon, 21 Jan 2019 14:38:44 -0200 Subject: [PATCH] Fetch new access and refresh tokens when refresh fails (#1338) * Fetch new access and refresh tokens when refresh fails * Fix typo --- packages/insomnia-app/app/network/o-auth-2/get-token.js | 6 ++++++ .../insomnia-app/app/network/o-auth-2/refresh-token.js | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/insomnia-app/app/network/o-auth-2/get-token.js b/packages/insomnia-app/app/network/o-auth-2/get-token.js index 6a0d039cf8a..a6824780d2a 100644 --- a/packages/insomnia-app/app/network/o-auth-2/get-token.js +++ b/packages/insomnia-app/app/network/o-auth-2/get-token.js @@ -190,6 +190,12 @@ async function _getAccessToken( authentication.scope, ); + // If we didn't receive an access token it means the refresh token didn't succeed, + // so we tell caller to fetch brand new access and refresh tokens. + if (!refreshResults.access_token) { + return null; + } + // ~~~~~~~~~~~~~ // // Update the DB // // ~~~~~~~~~~~~~ // diff --git a/packages/insomnia-app/app/network/o-auth-2/refresh-token.js b/packages/insomnia-app/app/network/o-auth-2/refresh-token.js index 4c6bdfd4152..465ae46de2b 100644 --- a/packages/insomnia-app/app/network/o-auth-2/refresh-token.js +++ b/packages/insomnia-app/app/network/o-auth-2/refresh-token.js @@ -48,7 +48,14 @@ export default async function( }); const statusCode = response.statusCode || 0; - if (statusCode < 200 || statusCode >= 300) { + + if (statusCode === 401) { + // If the refresh token was rejected due an unauthorized request, we will + // return a null access_token to trigger an authentication request to fetch + // brand new refresh and access tokens. + + return responseToObject(null, [c.P_ACCESS_TOKEN]); + } else if (statusCode < 200 || statusCode >= 300) { throw new Error(`[oauth2] Failed to refresh token url=${url} status=${statusCode}`); }