From c7be71c78189a025cf22697bf269edc5e9a84957 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Fri, 5 Feb 2021 08:56:40 +0000 Subject: [PATCH] fix(*): add the ability to ignore SSL errors --- src/SASViyaApiClient.ts | 39 +++++--------------- src/job-execution/JobExecutor.ts | 23 ++---------- src/job-execution/index.ts | 1 - src/job-execution/parseSasWork.ts | 61 ------------------------------- src/request/RequestClient.ts | 15 +++++--- 5 files changed, 23 insertions(+), 116 deletions(-) delete mode 100644 src/job-execution/parseSasWork.ts diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 7a33c8d..6c99376 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -623,11 +623,9 @@ export class SASViyaApiClient { public async getAuthCode(clientId: string) { const authUrl = `${this.serverUrl}/SASLogon/oauth/authorize?client_id=${clientId}&response_type=code` - const authCode = await fetch(authUrl, { - referrerPolicy: 'same-origin', - credentials: 'include' - }) - .then((response) => response.text()) + const authCode = await this.requestClient + .get(authUrl, undefined, 'text/plain') + .then((response) => response.result) .then(async (response) => { let code = '' if (isAuthorizeFormRequired(response)) { @@ -694,24 +692,9 @@ export class SASViyaApiClient { formData.append('code', authCode) } - let moreOptions = {} - if (insecure) { - const https = require('https') - moreOptions = { - agent: new https.Agent({ - rejectUnauthorized: false - }) - } - } - - const authResponse = await fetch(url, { - method: 'POST', - credentials: 'include', - headers, - body: formData as any, - referrerPolicy: 'same-origin', - ...moreOptions - }).then((res) => res.json()) + const authResponse = await this.requestClient + .post(url, formData, undefined, 'application/json', headers) + .then((res) => res.result) return authResponse } @@ -749,13 +732,9 @@ export class SASViyaApiClient { formData.append('refresh_token', refreshToken) } - const authResponse = await fetch(url, { - method: 'POST', - credentials: 'include', - headers, - body: formData as any, - referrerPolicy: 'same-origin' - }).then((res) => res.json()) + const authResponse = await this.requestClient + .post(url, formData, undefined, 'application/json', headers) + .then((res) => res.result) return authResponse } diff --git a/src/job-execution/JobExecutor.ts b/src/job-execution/JobExecutor.ts index e8a77cf..df75cb6 100644 --- a/src/job-execution/JobExecutor.ts +++ b/src/job-execution/JobExecutor.ts @@ -1,12 +1,6 @@ import { ServerType } from '@sasjs/utils/types' -import { parseSasWork } from '.' import { SASjsRequest } from '../types' -import { - asyncForEach, - parseGeneratedCode, - parseSourceCode, - parseWeboutResponse -} from '../utils' +import { asyncForEach, parseGeneratedCode, parseSourceCode } from '../utils' export type ExecuteFunction = () => Promise @@ -59,11 +53,7 @@ export abstract class BaseJobExecutor implements JobExecutor { this.waitingRequests.push(request) } - protected async appendRequest( - response: any, - program: string, - debug: boolean - ) { + protected appendRequest(response: any, program: string, debug: boolean) { let sourceCode = '' let generatedCode = '' let sasWork = null @@ -76,17 +66,12 @@ export abstract class BaseJobExecutor implements JobExecutor { if (response.log) { sasWork = response.log } else { - sasWork = JSON.parse(parseWeboutResponse(response.result)).WORK + sasWork = response.result.WORK } } else if (response?.result) { sourceCode = parseSourceCode(response.result) generatedCode = parseGeneratedCode(response.result) - sasWork = await parseSasWork( - response.result, - debug, - this.serverUrl, - this.serverType - ) + sasWork = response.result.WORK } } diff --git a/src/job-execution/index.ts b/src/job-execution/index.ts index f053824..d71b564 100644 --- a/src/job-execution/index.ts +++ b/src/job-execution/index.ts @@ -1,5 +1,4 @@ export * from './ComputeJobExecutor' export * from './JesJobExecutor' export * from './JobExecutor' -export * from './parseSasWork' export * from './WebJobExecutor' diff --git a/src/job-execution/parseSasWork.ts b/src/job-execution/parseSasWork.ts deleted file mode 100644 index 9057073..0000000 --- a/src/job-execution/parseSasWork.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { ServerType } from '@sasjs/utils/types' -import { parseWeboutResponse } from '../utils' - -export const parseSasWork = async ( - response: any, - debug: boolean, - serverUrl: string, - serverType: ServerType -) => { - if (debug) { - let jsonResponse - - if (serverType === ServerType.Sas9) { - try { - jsonResponse = JSON.parse(parseWeboutResponse(response)) - } catch (e) { - console.error(e) - } - } else { - await parseSASVIYADebugResponse(response, serverUrl).then( - (resText: any) => { - try { - jsonResponse = JSON.parse(resText) - } catch (e) { - console.error(e) - } - }, - (err: any) => { - console.error(err) - } - ) - } - - if (jsonResponse) { - return jsonResponse.WORK - } - } - return null -} - -const parseSASVIYADebugResponse = async ( - response: string, - serverUrl: string -) => { - return new Promise((resolve, reject) => { - const iframeStart = response.split( - '')[0] : null - - if (jsonUrl) { - fetch(serverUrl + jsonUrl) - .then((res) => res.text()) - .then((resText) => { - resolve(resText) - }) - } else { - reject('No debug info found in response.') - } - }) -} diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index acbc910..39efb4c 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -5,6 +5,7 @@ import { LoginRequiredError } from '../types' import { AuthorizeError } from '../types/AuthorizeError' import { NotFoundError } from '../types/NotFoundError' import { parseWeboutResponse } from '../utils/parseWeboutResponse' +import * as https from 'https' export interface HttpClient { get( @@ -43,8 +44,13 @@ export class RequestClient implements HttpClient { private fileUploadCsrfToken: CsrfToken | undefined private httpClient: AxiosInstance - constructor(private baseUrl: string) { - this.httpClient = axios.create({ baseURL: baseUrl }) + constructor(private baseUrl: string, allowInsecure = false) { + this.httpClient = axios.create({ + baseURL: baseUrl, + httpsAgent: new https.Agent({ + rejectUnauthorized: !allowInsecure + }) + }) } public getCsrfToken(type: 'general' | 'file' = 'general') { @@ -97,7 +103,7 @@ export class RequestClient implements HttpClient { overrideHeaders: { [key: string]: string | number } = {} ): Promise<{ result: T; etag: string }> { const headers = { - ...this.getHeaders(accessToken, contentType, url.endsWith('login.do')), + ...this.getHeaders(accessToken, contentType), ...overrideHeaders } @@ -262,8 +268,7 @@ export class RequestClient implements HttpClient { private getHeaders = ( accessToken: string | undefined, - contentType: string, - appendCsrfToken = true + contentType: string ) => { const headers: any = { 'Content-Type': contentType