1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-07 12:30:06 +00:00

chore: fetch username for Redirected-Login and return

This commit is contained in:
Saad Jutt
2021-09-07 05:26:42 +05:00
parent 0972c0deaa
commit a1f5355d6a
4 changed files with 48 additions and 37 deletions

View File

@@ -25,7 +25,7 @@ import {
Sas9JobExecutor Sas9JobExecutor
} from './job-execution' } from './job-execution'
import { ErrorResponse } from './types/errors' import { ErrorResponse } from './types/errors'
import { LoginOptions } from './types/LoginOptions' import { LoginOptions, LoginReturn } from './types/Login'
const defaultConfig: SASjsConfig = { const defaultConfig: SASjsConfig = {
serverUrl: '', serverUrl: '',
@@ -538,7 +538,7 @@ export default class SASjs {
username?: string, username?: string,
password?: string, password?: string,
options: LoginOptions = {} options: LoginOptions = {}
) { ): Promise<LoginReturn> {
if (this.sasjsConfig.loginMechanism === LoginMechanism.Default) { if (this.sasjsConfig.loginMechanism === LoginMechanism.Default) {
if (!username || !password) { if (!username || !password) {
throw new Error( throw new Error(

View File

@@ -1,6 +1,6 @@
import { ServerType } from '@sasjs/utils/types' import { ServerType } from '@sasjs/utils/types'
import { RequestClient } from '../request/RequestClient' import { RequestClient } from '../request/RequestClient'
import { LoginOptions } from '../types/LoginOptions' import { LoginOptions, LoginReturn } from '../types/Login'
import { serialize } from '../utils' import { serialize } from '../utils'
import { openWebPage } from './openWebPage' import { openWebPage } from './openWebPage'
import { verifyingPopUpLoginSAS9 } from './verifyingPopUpLoginSAS9' import { verifyingPopUpLoginSAS9 } from './verifyingPopUpLoginSAS9'
@@ -28,7 +28,9 @@ export class AuthManager {
* Opens Pop up window to SAS Login screen. * Opens Pop up window to SAS Login screen.
* And checks if user has finished login process. * And checks if user has finished login process.
*/ */
public async redirectedLogIn({ onLoggedOut }: LoginOptions) { public async redirectedLogIn({
onLoggedOut
}: LoginOptions): Promise<LoginReturn> {
const loginPopup = await openWebPage( const loginPopup = await openWebPage(
this.loginPreventRedirectUrl, this.loginPreventRedirectUrl,
'SASLogon', 'SASLogon',
@@ -40,7 +42,7 @@ export class AuthManager {
) )
if (!loginPopup) { if (!loginPopup) {
return { isLoggedIn: false } return { isLoggedIn: false, userName: '' }
} }
const { isLoggedIn } = const { isLoggedIn } =
@@ -60,10 +62,14 @@ export class AuthManager {
) )
} }
const { userName } = await this.fetchUserName()
await this.loginCallback() await this.loginCallback()
return { isLoggedIn: true, userName }
} }
return { isLoggedIn } return { isLoggedIn: false, userName: '' }
} }
/** /**
@@ -72,13 +78,7 @@ export class AuthManager {
* @param password - a string representing the password. * @param password - a string representing the password.
* @returns - a boolean `isLoggedin` and a string `username` * @returns - a boolean `isLoggedin` and a string `username`
*/ */
public async logIn( public async logIn(username: string, password: string): Promise<LoginReturn> {
username: string,
password: string
): Promise<{
isLoggedIn: boolean
userName: string
}> {
const loginParams = { const loginParams = {
_service: 'default', _service: 'default',
username, username,
@@ -119,7 +119,7 @@ export class AuthManager {
const res = await this.checkSession() const res = await this.checkSession()
isLoggedIn = res.isLoggedIn isLoggedIn = res.isLoggedIn
if (isLoggedIn) this.userName = res.userName! if (isLoggedIn) this.userName = res.userName
} else { } else {
this.userName = loginParams.username this.userName = loginParams.username
} }
@@ -175,27 +175,10 @@ export class AuthManager {
*/ */
public async checkSession(): Promise<{ public async checkSession(): Promise<{
isLoggedIn: boolean isLoggedIn: boolean
userName?: string userName: string
loginForm?: any loginForm?: any
}> { }> {
//For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution. const { isLoggedIn, userName } = await this.fetchUserName()
//For SAS9 we will send request on SASStoredProcess
const url =
this.serverType === 'SASVIYA'
? `${this.serverUrl}/identities/users/@currentUser`
: `${this.serverUrl}/SASStoredProcess`
const { result: loginResponse } = await this.requestClient
.get<string>(url, undefined, 'text/plain')
.catch((err: any) => {
return { result: 'authErr' }
})
const isLoggedIn = loginResponse !== 'authErr'
const userName = isLoggedIn
? this.extractUserName(loginResponse)
: undefined
let loginForm = null let loginForm = null
if (!isLoggedIn) { if (!isLoggedIn) {
@@ -214,11 +197,34 @@ export class AuthManager {
return Promise.resolve({ return Promise.resolve({
isLoggedIn, isLoggedIn,
userName: userName?.toLowerCase(), userName: userName.toLowerCase(),
loginForm loginForm
}) })
} }
private async fetchUserName(): Promise<{
isLoggedIn: boolean
userName: string
}> {
//For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution.
//For SAS9 we will send request on SASStoredProcess
const url =
this.serverType === 'SASVIYA'
? `${this.serverUrl}/identities/users/@currentUser`
: `${this.serverUrl}/SASStoredProcess`
const { result: loginResponse } = await this.requestClient
.get<string>(url, undefined, 'text/plain')
.catch((err: any) => {
return { result: 'authErr' }
})
const isLoggedIn = loginResponse !== 'authErr'
const userName = isLoggedIn ? this.extractUserName(loginResponse) : ''
return { isLoggedIn, userName }
}
private extractUserName = (response: any): string => { private extractUserName = (response: any): string => {
switch (this.serverType) { switch (this.serverType) {
case ServerType.SasViya: case ServerType.SasViya:

8
src/types/Login.ts Normal file
View File

@@ -0,0 +1,8 @@
export interface LoginOptions {
onLoggedOut?: () => Promise<boolean>
}
export interface LoginReturn {
isLoggedIn: boolean
userName: string
}

View File

@@ -1,3 +0,0 @@
export interface LoginOptions {
onLoggedOut?: () => Promise<boolean>
}