From b66f89a76dd4fd6467e028c9650c76889828ed43 Mon Sep 17 00:00:00 2001 From: Dan Jones <dan.jones@noc.ac.uk> Date: Mon, 20 Feb 2023 09:45:06 +0000 Subject: [PATCH] feat: add retry method to refresh token on 403 At the moment if the backbone token secret changes the adapter can have a token it thinks is valid but which can no longer be decrypted by the backbone. On a 403 error this will trigger a new token grant and resend the request with the new token. --- dist/adapter.esm.js | 34 +++++++++++++++++++++++++++++----- dist/adapter.js | 34 +++++++++++++++++++++++++++++----- src/adapter/index.js | 34 +++++++++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 15 deletions(-) diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js index 1acacf6..97b4e82 100644 --- a/dist/adapter.esm.js +++ b/dist/adapter.esm.js @@ -115,9 +115,10 @@ class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body + * @param {boolean} is_retry * @returns */ - publish(topic, body) { + publish(topic, body, is_retry=false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -136,7 +137,18 @@ class Adapter { return response; }) .catch((error) => { - return Promise.reject(error); + let retry = false; + switch(error.response.status_code) { + case 403: { + this.credentials = null; + retry = true; + } break; + case 503: { + retry = true; + } + } + if (retry && !is_retry) return this.publish(topic, body, true); + else return Promise.reject(error); }); } @@ -148,10 +160,11 @@ class Adapter { * quickly in an emergency scenario. * * Messages should be passed through encode before sending - * @param {*} body + * @param {string} body + * @param {boolean} is_retry * @returns */ - broadcast(body) { + broadcast(body, is_retry=false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -169,7 +182,18 @@ class Adapter { return response; }) .catch((error) => { - return Promise.reject(error); + let retry = false; + switch(error.response.status_code) { + case 403: { + this.credentials = null; + retry = true; + } break; + case 503: { + retry = true; + } + } + if (retry && !is_retry) return this.broadcast(body, true); + else return Promise.reject(error); }); } } diff --git a/dist/adapter.js b/dist/adapter.js index c868d51..57c294f 100644 --- a/dist/adapter.js +++ b/dist/adapter.js @@ -117,9 +117,10 @@ class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body + * @param {boolean} is_retry * @returns */ - publish(topic, body) { + publish(topic, body, is_retry=false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -138,7 +139,18 @@ class Adapter { return response; }) .catch((error) => { - return Promise.reject(error); + let retry = false; + switch(error.response.status_code) { + case 403: { + this.credentials = null; + retry = true; + } break; + case 503: { + retry = true; + } + } + if (retry && !is_retry) return this.publish(topic, body, true); + else return Promise.reject(error); }); } @@ -150,10 +162,11 @@ class Adapter { * quickly in an emergency scenario. * * Messages should be passed through encode before sending - * @param {*} body + * @param {string} body + * @param {boolean} is_retry * @returns */ - broadcast(body) { + broadcast(body, is_retry=false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -171,7 +184,18 @@ class Adapter { return response; }) .catch((error) => { - return Promise.reject(error); + let retry = false; + switch(error.response.status_code) { + case 403: { + this.credentials = null; + retry = true; + } break; + case 503: { + retry = true; + } + } + if (retry && !is_retry) return this.broadcast(body, true); + else return Promise.reject(error); }); } } diff --git a/src/adapter/index.js b/src/adapter/index.js index d52101f..51ecde7 100644 --- a/src/adapter/index.js +++ b/src/adapter/index.js @@ -115,9 +115,10 @@ export class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body + * @param {boolean} is_retry * @returns */ - publish(topic, body) { + publish(topic, body, is_retry=false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -136,7 +137,18 @@ export class Adapter { return response; }) .catch((error) => { - return Promise.reject(error); + let retry = false; + switch(error.response.status_code) { + case 403: { + this.credentials = null; + retry = true; + } break; + case 503: { + retry = true; + } + } + if (retry && !is_retry) return this.publish(topic, body, true); + else return Promise.reject(error); }); } @@ -148,10 +160,11 @@ export class Adapter { * quickly in an emergency scenario. * * Messages should be passed through encode before sending - * @param {*} body + * @param {string} body + * @param {boolean} is_retry * @returns */ - broadcast(body) { + broadcast(body, is_retry=false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -169,7 +182,18 @@ export class Adapter { return response; }) .catch((error) => { - return Promise.reject(error); + let retry = false; + switch(error.response.status_code) { + case 403: { + this.credentials = null; + retry = true; + } break; + case 503: { + retry = true; + } + } + if (retry && !is_retry) return this.broadcast(body, true); + else return Promise.reject(error); }); } } -- GitLab