1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-06 04:00:05 +00:00

Compare commits

...

6 Commits

Author SHA1 Message Date
Yury Shkoda
da7579a2bb Merge pull request #805 from sasjs/webpack-fix
fix(packaging): fixed output file for Node
2023-04-21 17:28:52 +03:00
Yury Shkoda
657e415c0c fix(packaging): fixed output file for Node 2023-04-21 16:02:47 +03:00
Allan Bowe
8fa908a201 Merge pull request #804 from sasjs/refrerefreshTokensForViya-fix
fix(refreshTokensForViya): fixed FormData logic
2023-04-21 09:53:55 +01:00
Yury Shkoda
a29b7f3b92 docs(refreshTokensForViya): added notes about limitation 2023-04-21 11:06:48 +03:00
Yury Shkoda
8360519408 fix(refreshTokensForViya): added throw error if not Node 2023-04-21 11:03:41 +03:00
Yury Shkoda
a71d422528 fix(refreshTokensForViya): fixed FormData logic 2023-04-20 16:13:22 +03:00
5 changed files with 28 additions and 5 deletions

View File

@@ -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.

View File

@@ -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(

View File

@@ -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)

View File

@@ -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 = () => {

View File

@@ -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'
} }
} }