diff --git a/src/SASjs.ts b/src/SASjs.ts index 59fe1a4..01c00b5 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -486,7 +486,7 @@ export default class SASjs { ]) if (this.sasjsConfig.serverType === ServerType.Sasjs) - return await this.sasJSApiClient!.getAccessToken(authCode) + return await this.sasJSApiClient!.getAccessToken(clientId, authCode) return await this.sasViyaApiClient!.getAccessToken( clientId, diff --git a/src/SASjsApiClient.ts b/src/SASjsApiClient.ts index 51ced0f..e0d8955 100644 --- a/src/SASjsApiClient.ts +++ b/src/SASjsApiClient.ts @@ -3,7 +3,6 @@ import { ExecutionQuery } from './types' import { RequestClient } from './request/RequestClient' import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs' import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs' -import { getAuthCodeForSasjs } from './auth/getAuthCodeForSasjs' import { parseWeboutResponse } from './utils' import { getTokens } from './auth/getTokens' @@ -100,8 +99,11 @@ export class SASjsApiClient { * @param clientId - the client ID to authenticate with. * @param authCode - the auth code received from the server. */ - public async getAccessToken(authCode: string): Promise { - return getAccessTokenForSasjs(this.requestClient, authCode) + public async getAccessToken( + clientId: string, + authCode: string + ): Promise { + return getAccessTokenForSasjs(this.requestClient, clientId, authCode) } /** @@ -111,16 +113,6 @@ export class SASjsApiClient { public async refreshTokens(refreshToken: string): Promise { return refreshTokensForSasjs(this.requestClient, refreshToken) } - - /** - * Performs a login authenticate and returns an auth code for the given client. - * @param username - a string representing the username. - * @param password - a string representing the password. - * @param clientId - the client ID to authenticate with. - */ - public async getAuthCode(username: string, password: string) { - return getAuthCodeForSasjs(this.requestClient, username, password) - } } // todo move to sasjs/utils diff --git a/src/auth/AuthManager.ts b/src/auth/AuthManager.ts index b78ecae..b541c26 100644 --- a/src/auth/AuthManager.ts +++ b/src/auth/AuthManager.ts @@ -2,8 +2,6 @@ import { ServerType } from '@sasjs/utils/types' import { RequestClient } from '../request/RequestClient' import { LoginOptions, LoginResult } from '../types/Login' import { serialize } from '../utils' -import { getAccessTokenForSasjs } from './getAccessTokenForSasjs' -import { getAuthCodeForSasjs } from './getAuthCodeForSasjs' import { openWebPage } from './openWebPage' import { verifySas9Login } from './verifySas9Login' import { verifySasViyaLogin } from './verifySasViyaLogin' diff --git a/src/auth/getAccessTokenForSasjs.ts b/src/auth/getAccessTokenForSasjs.ts index 57cc4bd..7b080bf 100644 --- a/src/auth/getAccessTokenForSasjs.ts +++ b/src/auth/getAccessTokenForSasjs.ts @@ -9,10 +9,12 @@ import { RequestClient } from '../request/RequestClient' */ export async function getAccessTokenForSasjs( requestClient: RequestClient, + clientId: string, authCode: string ) { const url = '/SASjsApi/auth/token' const data = { + clientId, code: authCode } diff --git a/src/auth/getAuthCodeForSasjs.ts b/src/auth/getAuthCodeForSasjs.ts deleted file mode 100644 index 4b99414..0000000 --- a/src/auth/getAuthCodeForSasjs.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { prefixMessage } from '@sasjs/utils/error' -import { RequestClient } from '../request/RequestClient' - -/** - * Performs a login authenticate and returns an auth code for the given client. - * @param requestClient - the pre-configured HTTP request client - * @param username - a string representing the username. - * @param password - a string representing the password. - * @param clientId - the client ID to authenticate with. - */ -export const getAuthCodeForSasjs = async ( - requestClient: RequestClient, - username: string, - password: string -) => { - const url = '/SASjsApi/auth/authorize' - const data = { username, password } - - const { code: authCode } = await requestClient - .post<{ code: string }>(url, data, undefined) - .then((res) => res.result) - .catch((err) => { - throw prefixMessage( - err, - 'Error while authenticating with provided username, password and clientId. ' - ) - }) - - return authCode -} diff --git a/src/auth/spec/getAccessTokenForSasjs.spec.ts b/src/auth/spec/getAccessTokenForSasjs.spec.ts index 8792a25..075d9b9 100644 --- a/src/auth/spec/getAccessTokenForSasjs.spec.ts +++ b/src/auth/spec/getAccessTokenForSasjs.spec.ts @@ -22,11 +22,15 @@ describe('getAccessTokenForSasjs', () => { Promise.resolve({ result: mockSasjsAuthResponse, etag: '' }) ) - await getAccessTokenForSasjs(requestClient, authConfig.refresh_token) + await getAccessTokenForSasjs( + requestClient, + authConfig.client, + authConfig.refresh_token + ) expect(requestClient.post).toHaveBeenCalledWith( '/SASjsApi/auth/token', - { code: authConfig.refresh_token }, + { clientId: authConfig.client, code: authConfig.refresh_token }, undefined ) }) @@ -47,6 +51,7 @@ describe('getAccessTokenForSasjs', () => { const error = await getAccessTokenForSasjs( requestClient, + authConfig.client, authConfig.refresh_token ).catch((e) => e)