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

fix(auth): login and check session returns username + user full name

This commit is contained in:
Saad Jutt
2022-09-25 01:11:22 +05:00
parent 2cbba38af5
commit a1c09ec802
4 changed files with 79 additions and 58 deletions

View File

@@ -1,14 +1,15 @@
import { ServerType } from '@sasjs/utils/types'
import { RequestClient } from '../request/RequestClient'
import { LoginOptions, LoginResult } from '../types/Login'
import { LoginOptions, LoginResult, LoginResultInternal } from '../types/Login'
import { serialize } from '../utils'
import { extractUserNameSas9 } from '../utils/sas9/extractUserNameSas9'
import { extractUserLongNameSas9 } from '../utils/sas9/extractUserLongNameSas9'
import { openWebPage } from './openWebPage'
import { verifySas9Login } from './verifySas9Login'
import { verifySasViyaLogin } from './verifySasViyaLogin'
export class AuthManager {
public userName = ''
public userLongName = ''
private loginUrl: string
private logoutUrl: string
private redirectedLoginUrl = `/SASLogon/home`
@@ -34,15 +35,19 @@ export class AuthManager {
public async redirectedLogIn({
onLoggedOut
}: LoginOptions): Promise<LoginResult> {
const { isLoggedIn: isLoggedInAlready, userName: currentSessionUsername } =
await this.fetchUserName()
const {
isLoggedIn: isLoggedInAlready,
userName: currentSessionUserName,
userLongName: currentSessionUserLongName
} = await this.fetchUserName()
if (isLoggedInAlready) {
await this.loginCallback()
return {
isLoggedIn: true,
userName: currentSessionUsername
userName: currentSessionUserName,
userLongName: currentSessionUserLongName
}
}
@@ -57,7 +62,7 @@ export class AuthManager {
)
if (!loginPopup) {
return { isLoggedIn: false, userName: '' }
return { isLoggedIn: false, userName: '', userLongName: '' }
}
const { isLoggedIn } =
@@ -72,14 +77,14 @@ export class AuthManager {
await this.performCASSecurityCheck()
}
const { userName } = await this.fetchUserName()
const { userName, userLongName } = await this.fetchUserName()
await this.loginCallback()
return { isLoggedIn: true, userName }
return { isLoggedIn: true, userName, userLongName }
}
return { isLoggedIn: false, userName: '' }
return { isLoggedIn: false, userName: '', userLongName: '' }
}
/**
@@ -94,27 +99,26 @@ export class AuthManager {
username,
password
}
this.userName = ''
this.userLongName = ''
let {
isLoggedIn: isLoggedInAlready,
loginForm,
userName: currentSessionUsername
userLongName: currentSessionUserLongName
} = await this.checkSession()
if (isLoggedInAlready) {
if (currentSessionUsername === loginParams.username) {
await this.loginCallback()
await this.loginCallback()
this.userName = currentSessionUsername!
return {
isLoggedIn: true,
userName: this.userName
}
} else {
await this.logOut()
loginForm = await this.getNewLoginForm()
this.userName = loginParams.username
this.userLongName = currentSessionUserLongName
return {
isLoggedIn: true,
userName: this.userName,
userLongName: this.userLongName
}
} else this.userName = ''
}
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
@@ -129,10 +133,7 @@ export class AuthManager {
const res = await this.checkSession()
isLoggedIn = res.isLoggedIn
if (isLoggedIn) this.userName = res.userName
} else {
this.userName = loginParams.username
this.userLongName = res.userLongName
}
if (isLoggedIn) {
@@ -141,11 +142,13 @@ export class AuthManager {
}
this.loginCallback()
} else this.userName = ''
this.userName = loginParams.username
}
return {
isLoggedIn,
userName: this.userName
userName: this.userName,
userLongName: this.userLongName
}
}
@@ -196,15 +199,12 @@ export class AuthManager {
* Checks whether a session is active, or login is required.
* @returns - a promise which resolves with an object containing three values
* - a boolean `isLoggedIn`
* - a string `userName` and
* - a string `userName`,
* - a string `userFullName` and
* - a form `loginForm` if not loggedin.
*/
public async checkSession(): Promise<{
isLoggedIn: boolean
userName: string
loginForm?: any
}> {
const { isLoggedIn, userName } = await this.fetchUserName()
public async checkSession(): Promise<LoginResultInternal> {
const { isLoggedIn, userName, userLongName } = await this.fetchUserName()
let loginForm = null
if (!isLoggedIn) {
@@ -217,7 +217,8 @@ export class AuthManager {
return Promise.resolve({
isLoggedIn,
userName: userName.toLowerCase(),
userName,
userLongName,
loginForm
})
}
@@ -246,10 +247,7 @@ export class AuthManager {
return await this.getLoginForm(formResponse)
}
private async fetchUserName(): Promise<{
isLoggedIn: boolean
userName: string
}> {
private async fetchUserName(): Promise<LoginResult> {
const url =
this.serverType === ServerType.SasViya
? `${this.serverUrl}/identities/users/@currentUser`
@@ -264,15 +262,19 @@ export class AuthManager {
})
const isLoggedIn = loginResponse !== 'authErr'
const userName = isLoggedIn ? this.extractUserName(loginResponse) : ''
if (!isLoggedIn) {
//We will logout to make sure cookies are removed and login form is presented
//Residue can happen in case of session expiration
await this.logOut()
return { isLoggedIn, userName: '', userLongName: '' }
}
return { isLoggedIn, userName }
return {
isLoggedIn,
userName: this.extractUserName(loginResponse),
userLongName: this.extractUserLongName(loginResponse)
}
}
private extractUserName = (response: any): string => {
@@ -281,7 +283,7 @@ export class AuthManager {
return response?.id
case ServerType.Sas9:
return extractUserNameSas9(response)
return ''
case ServerType.Sasjs:
return response?.username
@@ -292,6 +294,23 @@ export class AuthManager {
}
}
private extractUserLongName = (response: any): string => {
switch (this.serverType) {
case ServerType.SasViya:
return response?.name
case ServerType.Sas9:
return extractUserLongNameSas9(response)
case ServerType.Sasjs:
return response?.displayName
default:
console.error('Server Type not found in extractUserName function')
return ''
}
}
private getLoginForm(response: any) {
const pattern: RegExp = /<form.+action="(.*Logon[^"]*).*>/
const matches = pattern.exec(response)