mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 20:40:05 +00:00
feat(get-token): improved error prefix
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { SasAuthResponse } from '@sasjs/utils/types'
|
||||
import { SasAuthResponse, ServerType } from '@sasjs/utils/types'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
import { CertificateError } from '../types/errors'
|
||||
import { getTokenRequestErrorPrefix } from './getTokenRequestErrorPrefix'
|
||||
|
||||
/**
|
||||
* Exchanges the auth code for an access token for the given client.
|
||||
* @param requestClient - the pre-configured HTTP request client
|
||||
* Exchange the auth code for access / refresh tokens for the given client / secret pair.
|
||||
* @param requestClient - the pre-configured HTTP request client.
|
||||
* @param clientId - the client ID to authenticate with.
|
||||
* @param clientSecret - the client secret to authenticate with.
|
||||
* @param authCode - the auth code received from the server.
|
||||
@@ -16,29 +17,44 @@ export async function getAccessTokenForViya(
|
||||
clientSecret: string,
|
||||
authCode: string
|
||||
): Promise<SasAuthResponse> {
|
||||
const url = '/SASLogon/oauth/token'
|
||||
let token
|
||||
|
||||
if (typeof Buffer === 'undefined') {
|
||||
token = btoa(clientId + ':' + clientSecret)
|
||||
} else {
|
||||
token = Buffer.from(clientId + ':' + clientSecret).toString('base64')
|
||||
}
|
||||
|
||||
const url = '/SASLogon/oauth/token'
|
||||
const headers = {
|
||||
Authorization: 'Basic ' + token,
|
||||
Accept: 'application/json'
|
||||
}
|
||||
|
||||
const data = new URLSearchParams({
|
||||
const dataJson = new URLSearchParams({
|
||||
grant_type: 'authorization_code',
|
||||
code: authCode
|
||||
})
|
||||
const data = new URLSearchParams(dataJson)
|
||||
|
||||
const authResponse = await requestClient
|
||||
.post(url, data, undefined, 'application/x-www-form-urlencoded', headers)
|
||||
.then((res) => res.result as SasAuthResponse)
|
||||
.catch((err) => {
|
||||
if (err instanceof CertificateError) throw err
|
||||
throw prefixMessage(err, 'Error while getting access token. ')
|
||||
throw prefixMessage(
|
||||
err,
|
||||
getTokenRequestErrorPrefix(
|
||||
'fetching access token',
|
||||
'getAccessTokenForViya',
|
||||
ServerType.SasViya,
|
||||
url,
|
||||
dataJson,
|
||||
headers,
|
||||
clientId,
|
||||
clientSecret
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
return authResponse
|
||||
|
||||
Reference in New Issue
Block a user