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:
@@ -1,11 +1,15 @@
|
||||
import { ServerType } from '@sasjs/utils/types'
|
||||
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 {
|
||||
public userName = ''
|
||||
private loginUrl: string
|
||||
private logoutUrl: string
|
||||
private loginPreventRedirectUrl = `/SASLogon/home`
|
||||
constructor(
|
||||
private serverUrl: string,
|
||||
private serverType: ServerType,
|
||||
@@ -20,70 +24,36 @@ export class AuthManager {
|
||||
}
|
||||
|
||||
public async redirectedLogIn() {
|
||||
const width = 500
|
||||
const height = 600
|
||||
const left = screen.width / 2 - width / 2
|
||||
const top = screen.height / 2 - height / 2
|
||||
const loginPopup = openWebPage(this.loginPreventRedirectUrl, 'SASLogon', {
|
||||
width: 500,
|
||||
height: 600
|
||||
})
|
||||
|
||||
const loginPopup = window.open(
|
||||
this.loginUrl,
|
||||
'_blank',
|
||||
`toolbar=0,location=0,menubar=0,width=${width},height=${height},left=${left},top=${top}`
|
||||
)
|
||||
if (!loginPopup) {
|
||||
alert('Unable to open popup for login. Please try with other browser.')
|
||||
return { isLoggedIn: false }
|
||||
}
|
||||
|
||||
const { isLoggedIn } =
|
||||
this.serverType === ServerType.SasViya
|
||||
? await this.verifyingPopUpLoginSASVIYA(loginPopup!)
|
||||
: await this.verifyingPopUpLoginSAS9(loginPopup!)
|
||||
? await verifyingPopUpLoginSASVIYA(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`
|
||||
|
||||
await this.requestClient.get<string>(
|
||||
`/SASLogon/login?service=${casAuthenticationUrl}`,
|
||||
undefined
|
||||
)
|
||||
}
|
||||
|
||||
async verifyingPopUpLoginSASVIYA(loginPopup: Window) {
|
||||
let isLoggedIn = false
|
||||
let startTime = new Date()
|
||||
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 }
|
||||
}
|
||||
|
||||
|
||||
21
src/auth/openWebPage.ts
Normal file
21
src/auth/openWebPage.ts
Normal 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
|
||||
}
|
||||
18
src/auth/verifyingPopUpLoginSAS9.ts
Normal file
18
src/auth/verifyingPopUpLoginSAS9.ts
Normal 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 }
|
||||
}
|
||||
29
src/auth/verifyingPopUpLoginSASVIYA.ts
Normal file
29
src/auth/verifyingPopUpLoginSASVIYA.ts
Normal 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')
|
||||
@@ -2,8 +2,7 @@ import { ServerType } from '@sasjs/utils/types'
|
||||
import {
|
||||
ErrorResponse,
|
||||
JobExecutionError,
|
||||
LoginRequiredError,
|
||||
WeboutResponseError
|
||||
LoginRequiredError
|
||||
} from '../types/errors'
|
||||
import { generateFileUploadForm } from '../file/generateFileUploadForm'
|
||||
import { generateTableUploadForm } from '../file/generateTableUploadForm'
|
||||
@@ -16,6 +15,7 @@ import {
|
||||
} from '../utils'
|
||||
import { BaseJobExecutor } from './JobExecutor'
|
||||
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||
import { isLoggedInSASVIYA } from '../auth/verifyingPopUpLoginSASVIYA'
|
||||
|
||||
export interface WaitingRequstPromise {
|
||||
promise: Promise<any> | null
|
||||
@@ -40,6 +40,11 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
loginRequiredCallback?: any
|
||||
) {
|
||||
const loginCallback = loginRequiredCallback || (() => Promise.resolve())
|
||||
|
||||
if (this.serverType === ServerType.SasViya && !isLoggedInSASVIYA()) {
|
||||
await loginCallback()
|
||||
}
|
||||
|
||||
const program = isRelativePath(sasJob)
|
||||
? config.appLoc
|
||||
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
@@ -143,8 +148,6 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
}
|
||||
|
||||
if (e instanceof LoginRequiredError) {
|
||||
await loginCallback()
|
||||
|
||||
this.appendWaitingRequest(() => {
|
||||
return this.execute(
|
||||
sasJob,
|
||||
@@ -160,6 +163,8 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
await loginCallback()
|
||||
} else {
|
||||
reject(new ErrorResponse(e?.message, e))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user