1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

fix: split code to files + corrected usage of loginCallback

This commit is contained in:
Saad Jutt
2021-08-25 07:55:04 +05:00
parent 97918f301b
commit 1a59f95be7
5 changed files with 102 additions and 59 deletions

View File

@@ -1,11 +1,15 @@
import { ServerType } from '@sasjs/utils/types' import { ServerType } from '@sasjs/utils/types'
import { RequestClient } from '../request/RequestClient' import { RequestClient } from '../request/RequestClient'
import { delay, serialize } from '../utils' import { serialize } from '../utils'
import { openWebPage } from './openWebPage'
import { verifyingPopUpLoginSAS9 } from './verifyingPopUpLoginSAS9'
import { verifyingPopUpLoginSASVIYA } from './verifyingPopUpLoginSASVIYA'
export class AuthManager { export class AuthManager {
public userName = '' public userName = ''
private loginUrl: string private loginUrl: string
private logoutUrl: string private logoutUrl: string
private loginPreventRedirectUrl = `/SASLogon/home`
constructor( constructor(
private serverUrl: string, private serverUrl: string,
private serverType: ServerType, private serverType: ServerType,
@@ -20,69 +24,35 @@ export class AuthManager {
} }
public async redirectedLogIn() { public async redirectedLogIn() {
const width = 500 const loginPopup = openWebPage(this.loginPreventRedirectUrl, 'SASLogon', {
const height = 600 width: 500,
const left = screen.width / 2 - width / 2 height: 600
const top = screen.height / 2 - height / 2 })
const loginPopup = window.open( if (!loginPopup) {
this.loginUrl, alert('Unable to open popup for login. Please try with other browser.')
'_blank', return { isLoggedIn: false }
`toolbar=0,location=0,menubar=0,width=${width},height=${height},left=${left},top=${top}` }
)
const { isLoggedIn } = const { isLoggedIn } =
this.serverType === ServerType.SasViya this.serverType === ServerType.SasViya
? await this.verifyingPopUpLoginSASVIYA(loginPopup!) ? await verifyingPopUpLoginSASVIYA(loginPopup)
: await this.verifyingPopUpLoginSAS9(loginPopup!) : await verifyingPopUpLoginSAS9(loginPopup)
loginPopup?.close() loginPopup.close()
return { isLoggedIn, userName: 'test' } if (isLoggedIn) {
} if (this.serverType === ServerType.Sas9) {
const casAuthenticationUrl = `${this.serverUrl}/SASStoredProcess/j_spring_cas_security_check`
async verifyingPopUpLoginSASVIYA(loginPopup: Window) { await this.requestClient.get<string>(
let isLoggedIn = false `/SASLogon/login?service=${casAuthenticationUrl}`,
let startTime = new Date() undefined
let elapsedSeconds = 0
do {
await delay(1000)
if (loginPopup.closed) break
isLoggedIn =
document.cookie.includes('Current-User') &&
document.cookie.includes('userId')
elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000
} while (!isLoggedIn && elapsedSeconds < 5 * 60)
let isAuthorized = false
startTime = new Date()
do {
await delay(1000)
if (loginPopup.closed) break
isAuthorized =
!loginPopup.window.location.href.includes('SASLogon') ||
loginPopup.window.document.body.innerText.includes(
'You have signed in.'
) )
elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000 }
} while (!isAuthorized && elapsedSeconds < 5 * 60)
return { isLoggedIn: isLoggedIn && isAuthorized } await this.loginCallback()
} }
async verifyingPopUpLoginSAS9(loginPopup: Window) {
let isLoggedIn = false
let startTime = new Date()
let elapsedSeconds = 0
do {
await delay(1000)
if (loginPopup.closed) break
isLoggedIn = loginPopup.window.document.body.innerText.includes(
'You have signed in.'
)
elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000
} while (!isLoggedIn && elapsedSeconds < 5 * 60)
return { isLoggedIn } return { isLoggedIn }
} }

21
src/auth/openWebPage.ts Normal file
View File

@@ -0,0 +1,21 @@
interface windowFeatures {
width: number
height: number
}
export function openWebPage(
url: string,
windowName: string = '',
{ width, height }: windowFeatures
): Window | null {
const left = screen.width / 2 - width / 2
const top = screen.height / 2 - height / 2
const loginPopup = window.open(
url,
windowName,
`toolbar=0,location=0,menubar=0,width=${width},height=${height},left=${left},top=${top}`
)
return loginPopup
}

View File

@@ -0,0 +1,18 @@
import { delay } from '../utils'
export async function verifyingPopUpLoginSAS9(loginPopup: Window) {
let isLoggedIn = false
let startTime = new Date()
let elapsedSeconds = 0
do {
await delay(1000)
if (loginPopup.closed) break
isLoggedIn =
loginPopup.window.location.href.includes('SASLogon') &&
loginPopup.window.document.body.innerText.includes('You have signed in.')
elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000
} while (!isLoggedIn && elapsedSeconds < 5 * 60)
return { isLoggedIn }
}

View File

@@ -0,0 +1,29 @@
import { delay } from '../utils'
export async function verifyingPopUpLoginSASVIYA(loginPopup: Window) {
let isLoggedIn = false
let startTime = new Date()
let elapsedSeconds = 0
do {
await delay(1000)
if (loginPopup.closed) break
isLoggedIn = isLoggedInSASVIYA()
elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000
} while (!isLoggedIn && elapsedSeconds < 5 * 60)
let isAuthorized = false
startTime = new Date()
do {
await delay(1000)
if (loginPopup.closed) break
isAuthorized =
!loginPopup.window.location.href.includes('SASLogon') ||
loginPopup.window.document.body.innerText.includes('You have signed in.')
elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000
} while (!isAuthorized && elapsedSeconds < 5 * 60)
return { isLoggedIn: isLoggedIn && isAuthorized }
}
export const isLoggedInSASVIYA = () =>
document.cookie.includes('Current-User') && document.cookie.includes('userId')

View File

@@ -2,8 +2,7 @@ import { ServerType } from '@sasjs/utils/types'
import { import {
ErrorResponse, ErrorResponse,
JobExecutionError, JobExecutionError,
LoginRequiredError, LoginRequiredError
WeboutResponseError
} from '../types/errors' } from '../types/errors'
import { generateFileUploadForm } from '../file/generateFileUploadForm' import { generateFileUploadForm } from '../file/generateFileUploadForm'
import { generateTableUploadForm } from '../file/generateTableUploadForm' import { generateTableUploadForm } from '../file/generateTableUploadForm'
@@ -16,6 +15,7 @@ import {
} from '../utils' } from '../utils'
import { BaseJobExecutor } from './JobExecutor' import { BaseJobExecutor } from './JobExecutor'
import { parseWeboutResponse } from '../utils/parseWeboutResponse' import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { isLoggedInSASVIYA } from '../auth/verifyingPopUpLoginSASVIYA'
export interface WaitingRequstPromise { export interface WaitingRequstPromise {
promise: Promise<any> | null promise: Promise<any> | null
@@ -40,6 +40,11 @@ export class WebJobExecutor extends BaseJobExecutor {
loginRequiredCallback?: any loginRequiredCallback?: any
) { ) {
const loginCallback = loginRequiredCallback || (() => Promise.resolve()) const loginCallback = loginRequiredCallback || (() => Promise.resolve())
if (this.serverType === ServerType.SasViya && !isLoggedInSASVIYA()) {
await loginCallback()
}
const program = isRelativePath(sasJob) const program = isRelativePath(sasJob)
? config.appLoc ? config.appLoc
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '') ? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
@@ -143,8 +148,6 @@ export class WebJobExecutor extends BaseJobExecutor {
} }
if (e instanceof LoginRequiredError) { if (e instanceof LoginRequiredError) {
await loginCallback()
this.appendWaitingRequest(() => { this.appendWaitingRequest(() => {
return this.execute( return this.execute(
sasJob, sasJob,
@@ -160,6 +163,8 @@ export class WebJobExecutor extends BaseJobExecutor {
} }
) )
}) })
await loginCallback()
} else { } else {
reject(new ErrorResponse(e?.message, e)) reject(new ErrorResponse(e?.message, e))
} }