diff --git a/src/SASjs.ts b/src/SASjs.ts index f554947..01c00b5 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -592,15 +592,6 @@ export default class SASjs { 'A username and password are required when using the default login mechanism.' ) - if (this.sasjsConfig.serverType === ServerType.Sasjs) { - if (!clientId) - throw new Error( - 'A username, password and clientId are required when using the default login mechanism with server type SASJS.' - ) - - return this.authManager!.logInSasjs(username, password, clientId) - } - return this.authManager!.logIn(username, password) } diff --git a/src/SASjsApiClient.ts b/src/SASjsApiClient.ts index af17911..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' @@ -114,20 +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, - clientId: string - ) { - return getAuthCodeForSasjs(this.requestClient, username, password, clientId) - } } // todo move to sasjs/utils diff --git a/src/auth/AuthManager.ts b/src/auth/AuthManager.ts index 5d69c94..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' @@ -83,39 +81,6 @@ export class AuthManager { return { isLoggedIn: false, userName: '' } } - /** - * Logs into the SAS server with the supplied credentials. - * @param userName - a string representing the username. - * @param password - a string representing the password. - * @param clientId - a string representing the client ID. - * @returns - a boolean `isLoggedin` and a string `username` - */ - public async logInSasjs( - username: string, - password: string, - clientId: string - ): Promise { - const isLoggedIn = await this.sendLoginRequestSasjs( - username, - password, - clientId - ) - .then((res) => { - this.userName = username - this.requestClient.saveLocalStorageToken( - res.access_token, - res.refresh_token - ) - return true - }) - .catch(() => false) - - return { - isLoggedIn, - userName: this.userName - } - } - /** * Logs into the SAS server with the supplied credentials. * @param username - a string representing the username. @@ -152,7 +117,7 @@ export class AuthManager { let loginResponse = await this.sendLoginRequest(loginForm, loginParams) - let isLoggedIn = isLogInSuccess(loginResponse) + let isLoggedIn = isLogInSuccess(this.serverType, loginResponse) if (!isLoggedIn) { if (isCredentialsVerifyError(loginResponse)) { @@ -196,6 +161,17 @@ export class AuthManager { loginForm: { [key: string]: any }, loginParams: { [key: string]: any } ) { + if (this.serverType === ServerType.Sasjs) { + const { username, password } = loginParams + const { result: loginResponse } = await this.requestClient.post( + this.loginUrl, + { username, password }, + undefined + ) + + return loginResponse + } + for (const key in loginForm) { loginParams[key] = loginForm[key] } @@ -215,19 +191,6 @@ export class AuthManager { return loginResponse } - private async sendLoginRequestSasjs( - username: string, - password: string, - clientId: string - ) { - const authCode = await getAuthCodeForSasjs( - this.requestClient, - username, - password, - clientId - ) - return getAccessTokenForSasjs(this.requestClient, clientId, authCode) - } /** * Checks whether a session is active, or login is required. * @returns - a promise which resolves with an object containing three values @@ -248,8 +211,7 @@ export class AuthManager { //Residue can happen in case of session expiration await this.logOut() - if (this.serverType !== ServerType.Sasjs) - loginForm = await this.getNewLoginForm() + loginForm = await this.getNewLoginForm() } return Promise.resolve({ @@ -260,6 +222,12 @@ export class AuthManager { } private async getNewLoginForm() { + if (this.serverType === ServerType.Sasjs) { + // server will be sending CSRF cookie, + // http client will use it automatically + return this.requestClient.get('/', undefined) + } + const { result: formResponse } = await this.requestClient.get( this.loginUrl.replace('.do', ''), undefined, @@ -384,5 +352,8 @@ const isCredentialsVerifyError = (response: string): boolean => response ) -const isLogInSuccess = (response: string): boolean => - /You have signed in/gm.test(response) +const isLogInSuccess = (serverType: ServerType, response: any): boolean => { + if (serverType === ServerType.Sasjs) return response?.loggedin + + return /You have signed in/gm.test(response) +} diff --git a/src/auth/getAuthCodeForSasjs.ts b/src/auth/getAuthCodeForSasjs.ts deleted file mode 100644 index aa6d2b2..0000000 --- a/src/auth/getAuthCodeForSasjs.ts +++ /dev/null @@ -1,31 +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, - clientId: string -) => { - const url = '/SASjsApi/auth/authorize' - const data = { username, password, clientId } - - 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/utils/convertToCsv.ts b/src/utils/convertToCsv.ts index 2211cd8..62cf613 100644 --- a/src/utils/convertToCsv.ts +++ b/src/utils/convertToCsv.ts @@ -1,4 +1,4 @@ -import { isSpecialMissing } from '@sasjs/utils' +import { isSpecialMissing } from '@sasjs/utils/input/validators' /** * Converts the given JSON object array to a CSV string.