@@ -52,6 +52,8 @@ const authTypes = {
5252
5353const CREDS_HEADER_TYPE = 'CREDS_HEADER_TYPE' ;
5454
55+ let secret ;
56+
5557/**
5658 * Executes the action's/trigger's logic by sending a request to the assigned URL and emitting response to the platform.
5759 * The function returns a Promise sending a request and resolving the response as platform message.
@@ -109,7 +111,9 @@ module.exports.processMethod = async function (msg, cfg) {
109111
110112 const existingAuthHeader = ( headers || [ ] ) . find ( ( header ) => header . _type === CREDS_HEADER_TYPE ) ;
111113
112- const secret = await getSecret ( this , cfg . secretId ) ;
114+ if ( ! secret ) {
115+ secret = await getSecret ( this , cfg . secretId ) ;
116+ }
113117 switch ( secret . type ) {
114118 case authTypes . BASIC :
115119 if ( existingAuthHeader ) {
@@ -169,6 +173,7 @@ module.exports.processMethod = async function (msg, cfg) {
169173 doImmediateRetry = false ;
170174
171175 return await buildRequestBody ( )
176+ // eslint-disable-next-line no-loop-func
172177 . then ( async ( ) => {
173178 emitter . logger . trace ( 'Got request body' ) ;
174179 let result ;
@@ -177,17 +182,32 @@ module.exports.processMethod = async function (msg, cfg) {
177182 iteration -= 1 ;
178183 try {
179184 result = await request ( requestOptions ) ; // eslint-disable-line
185+ // 'requestOptions.simple' means that in case of 401 or 403 an exception won't be thrown.
186+ // For this case we throw an error manually.
187+ if ( ( result . statusCode === 403 || result . statusCode === 401 ) && secret . type === authTypes . OAUTH2 ) {
188+ const err = new Error ( 'Request unauthorized. Failing...' ) ;
189+ err . statusCode = result . statusCode ;
190+ throw err ;
191+ }
180192 break ;
181193 } catch ( e ) {
182194 this . logger . error ( 'Got request error' ) ;
183195 if ( ( e . statusCode === 403 || e . statusCode === 401 ) && secret . type === authTypes . OAUTH2 ) {
184- try {
185- this . logger . info ( 'Going to refresh token' ) ;
186- const token = await refreshToken ( this , cfg . secretId ) ; // eslint-disable-line
187- this . logger . info ( 'Token successfully refreshed' ) ;
188- requestOptions . headers . authorization = `Bearer ${ token } ` ;
189- } catch ( e ) {
190- this . logger . error ( 'Failed to refresh token' ) ;
196+ const newSecret = await getSecret ( this , cfg . secretId ) ;
197+ if ( secret . credentials . access_token !== newSecret . credentials . access_token ) {
198+ this . logger . info ( 'Secret has been changed, trying to use new one...' ) ;
199+ secret = newSecret ;
200+ requestOptions . headers . authorization = `Bearer ${ secret . credentials . access_token } ` ;
201+ } else {
202+ try {
203+ this . logger . info ( 'Going to refresh token' ) ;
204+ await refreshToken ( this , cfg . secretId ) ;
205+ secret = await getSecret ( this , cfg . secretId ) ;
206+ this . logger . info ( 'Token successfully refreshed' ) ;
207+ requestOptions . headers . authorization = `Bearer ${ secret . credentials . access_token } ` ;
208+ } catch ( e ) {
209+ this . logger . error ( 'Failed to refresh token' ) ;
210+ }
191211 }
192212 } else {
193213 throw e ;
0 commit comments