mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-06 04:00:05 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da7579a2bb | ||
|
|
657e415c0c | ||
|
|
8fa908a201 | ||
|
|
a29b7f3b92 | ||
|
|
8360519408 | ||
|
|
a71d422528 |
@@ -548,6 +548,7 @@ export class SASViyaApiClient {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Exchanges the refresh token for an access token for the given client.
|
* Exchanges the refresh token for an access token for the given client.
|
||||||
|
* This method can only be used by Node.
|
||||||
* @param clientId - the client ID to authenticate with.
|
* @param clientId - the client ID to authenticate with.
|
||||||
* @param clientSecret - the client secret to authenticate with.
|
* @param clientSecret - the client secret to authenticate with.
|
||||||
* @param refreshToken - the refresh token received from the server.
|
* @param refreshToken - the refresh token received from the server.
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { refreshTokensForSasjs } from './refreshTokensForSasjs'
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the auth configuration, refreshing the tokens if necessary.
|
* Returns the auth configuration, refreshing the tokens if necessary.
|
||||||
|
* This function can only be used by Node, if a server type is SASVIYA.
|
||||||
* @param requestClient - the pre-configured HTTP request client
|
* @param requestClient - the pre-configured HTTP request client
|
||||||
* @param authConfig - an object containing a client ID, secret, access token and refresh token
|
* @param authConfig - an object containing a client ID, secret, access token and refresh token
|
||||||
* @param serverType - server type for which refreshing the tokens, defaults to SASVIYA
|
* @param serverType - server type for which refreshing the tokens, defaults to SASVIYA
|
||||||
@@ -29,9 +30,12 @@ export async function getTokens(
|
|||||||
const error =
|
const error =
|
||||||
'Unable to obtain new access token. Your refresh token has expired.'
|
'Unable to obtain new access token. Your refresh token has expired.'
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
|
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info('Refreshing access and refresh tokens.')
|
logger.info('Refreshing access and refresh tokens.')
|
||||||
|
|
||||||
const tokens =
|
const tokens =
|
||||||
serverType === ServerType.SasViya
|
serverType === ServerType.SasViya
|
||||||
? await refreshTokensForViya(
|
? await refreshTokensForViya(
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ import { SasAuthResponse } from '@sasjs/utils/types'
|
|||||||
import { prefixMessage } from '@sasjs/utils/error'
|
import { prefixMessage } from '@sasjs/utils/error'
|
||||||
import * as NodeFormData from 'form-data'
|
import * as NodeFormData from 'form-data'
|
||||||
import { RequestClient } from '../request/RequestClient'
|
import { RequestClient } from '../request/RequestClient'
|
||||||
|
import { isNode } from '../utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exchanges the refresh token for an access token for the given client.
|
* Exchanges the refresh token for an access token for the given client.
|
||||||
|
* This function can only be used by Node.
|
||||||
* @param requestClient - the pre-configured HTTP request client
|
* @param requestClient - the pre-configured HTTP request client
|
||||||
* @param clientId - the client ID to authenticate with.
|
* @param clientId - the client ID to authenticate with.
|
||||||
* @param clientSecret - the client secret to authenticate with.
|
* @param clientSecret - the client secret to authenticate with.
|
||||||
@@ -16,9 +18,12 @@ export async function refreshTokensForViya(
|
|||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
refreshToken: string
|
refreshToken: string
|
||||||
) {
|
) {
|
||||||
|
if (!isNode()) {
|
||||||
|
throw new Error(`Method 'refreshTokensForViya' can only be used by Node.`)
|
||||||
|
}
|
||||||
|
|
||||||
const url = '/SASLogon/oauth/token'
|
const url = '/SASLogon/oauth/token'
|
||||||
let token
|
const token =
|
||||||
token =
|
|
||||||
typeof Buffer === 'undefined'
|
typeof Buffer === 'undefined'
|
||||||
? btoa(clientId + ':' + clientSecret)
|
? btoa(clientId + ':' + clientSecret)
|
||||||
: Buffer.from(clientId + ':' + clientSecret).toString('base64')
|
: Buffer.from(clientId + ':' + clientSecret).toString('base64')
|
||||||
@@ -27,8 +32,7 @@ export async function refreshTokensForViya(
|
|||||||
Authorization: 'Basic ' + token
|
Authorization: 'Basic ' + token
|
||||||
}
|
}
|
||||||
|
|
||||||
const formData =
|
const formData = new NodeFormData()
|
||||||
typeof FormData === 'undefined' ? new NodeFormData() : new FormData()
|
|
||||||
formData.append('grant_type', 'refresh_token')
|
formData.append('grant_type', 'refresh_token')
|
||||||
formData.append('refresh_token', refreshToken)
|
formData.append('refresh_token', refreshToken)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import * as NodeFormData from 'form-data'
|
|||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
import { generateToken, mockAuthResponse } from './mockResponses'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { refreshTokensForViya } from '../refreshTokensForViya'
|
import { refreshTokensForViya } from '../refreshTokensForViya'
|
||||||
|
import * as IsNodeModule from '../../utils/isNode'
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
|
|
||||||
@@ -70,6 +71,18 @@ describe('refreshTokensForViya', () => {
|
|||||||
|
|
||||||
expect(error).toEqual(`Error while refreshing tokens: ${tokenError}`)
|
expect(error).toEqual(`Error while refreshing tokens: ${tokenError}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should throw an error if environment is not Node', async () => {
|
||||||
|
jest.spyOn(IsNodeModule, 'isNode').mockImplementation(() => false)
|
||||||
|
|
||||||
|
const expectedError = new Error(
|
||||||
|
`Method 'refreshTokensForViya' can only be used by Node.`
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
refreshTokensForViya(requestClient, 'client', 'secret', 'token')
|
||||||
|
).rejects.toEqual(expectedError)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const setupMocks = () => {
|
const setupMocks = () => {
|
||||||
|
|||||||
@@ -86,7 +86,8 @@ const nodeConfig = {
|
|||||||
entry: './node/index.ts',
|
entry: './node/index.ts',
|
||||||
output: {
|
output: {
|
||||||
...browserConfig.output,
|
...browserConfig.output,
|
||||||
path: path.resolve(__dirname, 'build', 'node')
|
path: path.resolve(__dirname, 'build', 'node'),
|
||||||
|
filename: 'index.js'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user