mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-05 19:50:06 +00:00
Compare commits
2 Commits
v3.9.4
...
snyk-upgra
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59195e6379 | ||
|
|
3a820c56a9 |
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# Using `--silent` helps for showing any errs in the first line of the response
|
|
||||||
# The first line is picked up by the VS Code GIT UI popup when rc is not 0
|
|
||||||
|
|
||||||
if npm run --silent lint:silent ; then
|
|
||||||
exit 0
|
|
||||||
else
|
|
||||||
npm run --silent lint:fix
|
|
||||||
echo "❌ Prettier check failed! We ran lint:fix for you. Please add & commit again."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
@@ -8,9 +8,8 @@
|
|||||||
"build": "rimraf build && rimraf node && mkdir node && copyfiles -u 1 \"./src/**/*\" ./node && webpack && rimraf build/src && rimraf node",
|
"build": "rimraf build && rimraf node && mkdir node && copyfiles -u 1 \"./src/**/*\" ./node && webpack && rimraf build/src && rimraf node",
|
||||||
"package:lib": "npm run build && copyfiles ./package.json ./checkNodeVersion.js build && cd build && npm version \"5.0.0\" && npm pack",
|
"package:lib": "npm run build && copyfiles ./package.json ./checkNodeVersion.js build && cd build && npm version \"5.0.0\" && npm pack",
|
||||||
"publish:lib": "npm run build && cd build && npm publish",
|
"publish:lib": "npm run build && cd build && npm publish",
|
||||||
"lint:fix": "npx prettier --loglevel silent --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\" && npx prettier --loglevel silent --write \"sasjs-tests/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\"",
|
"lint:fix": "npx prettier --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\" && npx prettier --write \"sasjs-tests/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\"",
|
||||||
"lint": "npx prettier --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\" && npx prettier --check \"sasjs-tests/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\"",
|
"lint": "npx prettier --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\" && npx prettier --check \"sasjs-tests/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\"",
|
||||||
"lint:silent": "npx prettier --loglevel silent --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\" && npx prettier --loglevel silent --check \"sasjs-tests/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}\"",
|
|
||||||
"test": "jest --silent --coverage",
|
"test": "jest --silent --coverage",
|
||||||
"prepublishOnly": "cp -r ./build/* . && rm -rf ./build",
|
"prepublishOnly": "cp -r ./build/* . && rm -rf ./build",
|
||||||
"postpublish": "git clean -fd",
|
"postpublish": "git clean -fd",
|
||||||
|
|||||||
@@ -592,6 +592,15 @@ export default class SASjs {
|
|||||||
'A username and password are required when using the default login mechanism.'
|
'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)
|
return this.authManager!.logIn(username, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { ExecutionQuery } from './types'
|
|||||||
import { RequestClient } from './request/RequestClient'
|
import { RequestClient } from './request/RequestClient'
|
||||||
import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs'
|
import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs'
|
||||||
import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs'
|
import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs'
|
||||||
|
import { getAuthCodeForSasjs } from './auth/getAuthCodeForSasjs'
|
||||||
import { parseWeboutResponse } from './utils'
|
import { parseWeboutResponse } from './utils'
|
||||||
import { getTokens } from './auth/getTokens'
|
import { getTokens } from './auth/getTokens'
|
||||||
|
|
||||||
@@ -113,6 +114,20 @@ export class SASjsApiClient {
|
|||||||
public async refreshTokens(refreshToken: string): Promise<SASjsAuthResponse> {
|
public async refreshTokens(refreshToken: string): Promise<SASjsAuthResponse> {
|
||||||
return refreshTokensForSasjs(this.requestClient, refreshToken)
|
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
|
// todo move to sasjs/utils
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import { ServerType } from '@sasjs/utils/types'
|
|||||||
import { RequestClient } from '../request/RequestClient'
|
import { RequestClient } from '../request/RequestClient'
|
||||||
import { LoginOptions, LoginResult } from '../types/Login'
|
import { LoginOptions, LoginResult } from '../types/Login'
|
||||||
import { serialize } from '../utils'
|
import { serialize } from '../utils'
|
||||||
|
import { getAccessTokenForSasjs } from './getAccessTokenForSasjs'
|
||||||
|
import { getAuthCodeForSasjs } from './getAuthCodeForSasjs'
|
||||||
import { openWebPage } from './openWebPage'
|
import { openWebPage } from './openWebPage'
|
||||||
import { verifySas9Login } from './verifySas9Login'
|
import { verifySas9Login } from './verifySas9Login'
|
||||||
import { verifySasViyaLogin } from './verifySasViyaLogin'
|
import { verifySasViyaLogin } from './verifySasViyaLogin'
|
||||||
@@ -81,6 +83,39 @@ export class AuthManager {
|
|||||||
return { isLoggedIn: false, userName: '' }
|
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<LoginResult> {
|
||||||
|
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.
|
* Logs into the SAS server with the supplied credentials.
|
||||||
* @param username - a string representing the username.
|
* @param username - a string representing the username.
|
||||||
@@ -117,7 +152,7 @@ export class AuthManager {
|
|||||||
|
|
||||||
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
|
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
|
||||||
|
|
||||||
let isLoggedIn = isLogInSuccess(this.serverType, loginResponse)
|
let isLoggedIn = isLogInSuccess(loginResponse)
|
||||||
|
|
||||||
if (!isLoggedIn) {
|
if (!isLoggedIn) {
|
||||||
if (isCredentialsVerifyError(loginResponse)) {
|
if (isCredentialsVerifyError(loginResponse)) {
|
||||||
@@ -161,17 +196,6 @@ export class AuthManager {
|
|||||||
loginForm: { [key: string]: any },
|
loginForm: { [key: string]: any },
|
||||||
loginParams: { [key: string]: any }
|
loginParams: { [key: string]: any }
|
||||||
) {
|
) {
|
||||||
if (this.serverType === ServerType.Sasjs) {
|
|
||||||
const { username, password } = loginParams
|
|
||||||
const { result: loginResponse } = await this.requestClient.post<string>(
|
|
||||||
this.loginUrl,
|
|
||||||
{ username, password },
|
|
||||||
undefined
|
|
||||||
)
|
|
||||||
|
|
||||||
return loginResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in loginForm) {
|
for (const key in loginForm) {
|
||||||
loginParams[key] = loginForm[key]
|
loginParams[key] = loginForm[key]
|
||||||
}
|
}
|
||||||
@@ -191,6 +215,19 @@ export class AuthManager {
|
|||||||
return loginResponse
|
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.
|
* Checks whether a session is active, or login is required.
|
||||||
* @returns - a promise which resolves with an object containing three values
|
* @returns - a promise which resolves with an object containing three values
|
||||||
@@ -211,6 +248,7 @@ export class AuthManager {
|
|||||||
//Residue can happen in case of session expiration
|
//Residue can happen in case of session expiration
|
||||||
await this.logOut()
|
await this.logOut()
|
||||||
|
|
||||||
|
if (this.serverType !== ServerType.Sasjs)
|
||||||
loginForm = await this.getNewLoginForm()
|
loginForm = await this.getNewLoginForm()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,12 +260,6 @@ export class AuthManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async getNewLoginForm() {
|
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<string>(
|
const { result: formResponse } = await this.requestClient.get<string>(
|
||||||
this.loginUrl.replace('.do', ''),
|
this.loginUrl.replace('.do', ''),
|
||||||
undefined,
|
undefined,
|
||||||
@@ -352,8 +384,5 @@ const isCredentialsVerifyError = (response: string): boolean =>
|
|||||||
response
|
response
|
||||||
)
|
)
|
||||||
|
|
||||||
const isLogInSuccess = (serverType: ServerType, response: any): boolean => {
|
const isLogInSuccess = (response: string): boolean =>
|
||||||
if (serverType === ServerType.Sasjs) return response?.loggedin
|
/You have signed in/gm.test(response)
|
||||||
|
|
||||||
return /You have signed in/gm.test(response)
|
|
||||||
}
|
|
||||||
|
|||||||
31
src/auth/getAuthCodeForSasjs.ts
Normal file
31
src/auth/getAuthCodeForSasjs.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { isSpecialMissing } from '@sasjs/utils/input/validators'
|
import { isSpecialMissing } from '@sasjs/utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the given JSON object array to a CSV string.
|
* Converts the given JSON object array to a CSV string.
|
||||||
|
|||||||
Reference in New Issue
Block a user