mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-02 18:20:06 +00:00
fix(execution): refresh tokens before fetching results
This commit is contained in:
@@ -290,16 +290,14 @@ export class SASViyaApiClient {
|
|||||||
printPid = false,
|
printPid = false,
|
||||||
variables?: MacroVar
|
variables?: MacroVar
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const { access_token } = authConfig || {}
|
let access_token = (authConfig || {}).access_token
|
||||||
|
if (authConfig) {
|
||||||
|
;({ access_token } = await this.getTokens(authConfig))
|
||||||
|
}
|
||||||
|
|
||||||
const logger = process.logger || console
|
const logger = process.logger || console
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const headers: any = {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
|
|
||||||
if (access_token) headers.Authorization = `Bearer ${access_token}`
|
|
||||||
|
|
||||||
let executionSessionId: string
|
let executionSessionId: string
|
||||||
|
|
||||||
const session = await this.sessionManager
|
const session = await this.sessionManager
|
||||||
@@ -443,6 +441,10 @@ export class SASViyaApiClient {
|
|||||||
throw prefixMessage(err, 'Error while polling job status. ')
|
throw prefixMessage(err, 'Error while polling job status. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (authConfig) {
|
||||||
|
;({ access_token } = await this.getTokens(authConfig))
|
||||||
|
}
|
||||||
|
|
||||||
const { result: currentJob } = await this.requestClient
|
const { result: currentJob } = await this.requestClient
|
||||||
.get<Job>(
|
.get<Job>(
|
||||||
`/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
|
`/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
|
||||||
@@ -886,7 +888,10 @@ export class SASViyaApiClient {
|
|||||||
printPid = false,
|
printPid = false,
|
||||||
variables?: MacroVar
|
variables?: MacroVar
|
||||||
) {
|
) {
|
||||||
let { access_token } = authConfig || {}
|
let access_token = (authConfig || {}).access_token
|
||||||
|
if (authConfig) {
|
||||||
|
;({ access_token } = await this.getTokens(authConfig))
|
||||||
|
}
|
||||||
|
|
||||||
if (isRelativePath(sasJob) && !this.rootFolderName) {
|
if (isRelativePath(sasJob) && !this.rootFolderName) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -913,12 +918,6 @@ export class SASViyaApiClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const headers: any = { 'Content-Type': 'application/json' }
|
|
||||||
|
|
||||||
if (!!access_token) {
|
|
||||||
headers.Authorization = `Bearer ${access_token}`
|
|
||||||
}
|
|
||||||
|
|
||||||
const jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
const jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||||
|
|
||||||
if (!jobToExecute) {
|
if (!jobToExecute) {
|
||||||
@@ -985,7 +984,10 @@ export class SASViyaApiClient {
|
|||||||
data?: any,
|
data?: any,
|
||||||
authConfig?: AuthConfig
|
authConfig?: AuthConfig
|
||||||
) {
|
) {
|
||||||
let { access_token } = authConfig || {}
|
let access_token = (authConfig || {}).access_token
|
||||||
|
if (authConfig) {
|
||||||
|
;({ access_token } = await this.getTokens(authConfig))
|
||||||
|
}
|
||||||
if (isRelativePath(sasJob) && !this.rootFolderName) {
|
if (isRelativePath(sasJob) && !this.rootFolderName) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Relative paths cannot be used without specifying a root folder name.'
|
'Relative paths cannot be used without specifying a root folder name.'
|
||||||
@@ -1145,21 +1147,9 @@ export class SASViyaApiClient {
|
|||||||
let POLL_INTERVAL = 300
|
let POLL_INTERVAL = 300
|
||||||
let MAX_POLL_COUNT = 1000
|
let MAX_POLL_COUNT = 1000
|
||||||
let MAX_ERROR_COUNT = 5
|
let MAX_ERROR_COUNT = 5
|
||||||
let { access_token, refresh_token, client, secret } = authConfig || {}
|
let access_token = (authConfig || {}).access_token
|
||||||
if (access_token && refresh_token) {
|
if (authConfig) {
|
||||||
if (
|
;({ access_token } = await this.getTokens(authConfig))
|
||||||
client &&
|
|
||||||
secret &&
|
|
||||||
refresh_token &&
|
|
||||||
(isAccessTokenExpiring(access_token) ||
|
|
||||||
isRefreshTokenExpiring(refresh_token))
|
|
||||||
) {
|
|
||||||
;({ access_token, refresh_token } = await this.refreshTokens(
|
|
||||||
client,
|
|
||||||
secret,
|
|
||||||
refresh_token
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pollOptions) {
|
if (pollOptions) {
|
||||||
@@ -1213,20 +1203,8 @@ export class SASViyaApiClient {
|
|||||||
postedJobState === 'pending' ||
|
postedJobState === 'pending' ||
|
||||||
postedJobState === 'unavailable'
|
postedJobState === 'unavailable'
|
||||||
) {
|
) {
|
||||||
if (access_token && refresh_token) {
|
if (authConfig) {
|
||||||
if (
|
;({ access_token } = await this.getTokens(authConfig))
|
||||||
client &&
|
|
||||||
secret &&
|
|
||||||
refresh_token &&
|
|
||||||
(isAccessTokenExpiring(access_token) ||
|
|
||||||
isRefreshTokenExpiring(refresh_token))
|
|
||||||
) {
|
|
||||||
;({ access_token, refresh_token } = await this.refreshTokens(
|
|
||||||
client,
|
|
||||||
secret,
|
|
||||||
refresh_token
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stateLink) {
|
if (stateLink) {
|
||||||
@@ -1510,4 +1488,21 @@ export class SASViyaApiClient {
|
|||||||
|
|
||||||
return movedFolder
|
return movedFolder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getTokens(authConfig: AuthConfig): Promise<AuthConfig> {
|
||||||
|
const logger = process.logger || console
|
||||||
|
let { access_token, refresh_token, client, secret } = authConfig
|
||||||
|
if (
|
||||||
|
isAccessTokenExpiring(access_token) ||
|
||||||
|
isRefreshTokenExpiring(refresh_token)
|
||||||
|
) {
|
||||||
|
logger.info('Refreshing access and refresh tokens.')
|
||||||
|
;({ access_token, refresh_token } = await this.refreshTokens(
|
||||||
|
client,
|
||||||
|
secret,
|
||||||
|
refresh_token
|
||||||
|
))
|
||||||
|
}
|
||||||
|
return { access_token, refresh_token, client, secret }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user