mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 08:30:07 +00:00
fix(login): code refactor + sasjs-tests updated
This commit is contained in:
22129
sasjs-tests/package-lock.json
generated
22129
sasjs-tests/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -41,6 +41,19 @@ export const basicTests = (
|
|||||||
assertion: (response: any) =>
|
assertion: (response: any) =>
|
||||||
response && response.isLoggedIn && response.userName === userName
|
response && response.isLoggedIn && response.userName === userName
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: 'Fetch username for already logged in user',
|
||||||
|
description: 'Should log the user in',
|
||||||
|
test: async () => {
|
||||||
|
await adapter.logIn(userName, password)
|
||||||
|
|
||||||
|
const newAdapterIns = new SASjs(adapter.getSasjsConfig())
|
||||||
|
|
||||||
|
return newAdapterIns.checkSession()
|
||||||
|
},
|
||||||
|
assertion: (response: any) =>
|
||||||
|
response?.isLoggedIn && response?.userName === userName
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: 'Multiple Log in attempts',
|
title: 'Multiple Log in attempts',
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -23,43 +23,53 @@ export class AuthManager {
|
|||||||
* Logs into the SAS server with the supplied credentials.
|
* Logs into the SAS server with the supplied credentials.
|
||||||
* @param username - a string representing the username.
|
* @param username - a string representing the username.
|
||||||
* @param password - a string representing the password.
|
* @param password - a string representing the password.
|
||||||
|
* @returns - a boolean `isLoggedin`
|
||||||
*/
|
*/
|
||||||
public async logIn(username: string, password: string) {
|
public async logIn(
|
||||||
const loginParams: any = {
|
username: string,
|
||||||
|
password: string
|
||||||
|
): Promise<{
|
||||||
|
isLoggedIn: boolean
|
||||||
|
}> {
|
||||||
|
const loginParams = {
|
||||||
_service: 'default',
|
_service: 'default',
|
||||||
username,
|
username,
|
||||||
password
|
password
|
||||||
}
|
}
|
||||||
|
|
||||||
this.userName = loginParams.username
|
let { isLoggedIn, loginForm, userName } = await this.checkSession()
|
||||||
|
|
||||||
const { isLoggedIn, loginForm } = await this.checkSession()
|
|
||||||
|
|
||||||
if (isLoggedIn) {
|
if (isLoggedIn) {
|
||||||
await this.loginCallback()
|
if (userName === loginParams.username) {
|
||||||
|
await this.loginCallback()
|
||||||
|
|
||||||
return {
|
this.userName = userName!
|
||||||
isLoggedIn,
|
return {
|
||||||
userName: this.userName
|
isLoggedIn
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.logOut()
|
||||||
}
|
}
|
||||||
}
|
} else this.userName = ''
|
||||||
|
|
||||||
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
|
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
|
||||||
|
|
||||||
let loggedIn = isLogInSuccess(loginResponse)
|
isLoggedIn = isLogInSuccess(loginResponse)
|
||||||
|
|
||||||
if (!loggedIn) {
|
if (!isLoggedIn) {
|
||||||
if (isCredentialsVerifyError(loginResponse)) {
|
if (isCredentialsVerifyError(loginResponse)) {
|
||||||
const newLoginForm = await this.getLoginForm(loginResponse)
|
const newLoginForm = await this.getLoginForm(loginResponse)
|
||||||
|
|
||||||
loginResponse = await this.sendLoginRequest(newLoginForm, loginParams)
|
loginResponse = await this.sendLoginRequest(newLoginForm, loginParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentSession = await this.checkSession()
|
;({ isLoggedIn, userName } = await this.checkSession())
|
||||||
loggedIn = currentSession.isLoggedIn
|
} else {
|
||||||
|
this.userName = loginParams.username
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loggedIn) {
|
if (isLoggedIn) {
|
||||||
|
this.userName = userName!
|
||||||
if (this.serverType === ServerType.Sas9) {
|
if (this.serverType === ServerType.Sas9) {
|
||||||
const casAuthenticationUrl = `${this.serverUrl}/SASStoredProcess/j_spring_cas_security_check`
|
const casAuthenticationUrl = `${this.serverUrl}/SASStoredProcess/j_spring_cas_security_check`
|
||||||
|
|
||||||
@@ -70,11 +80,10 @@ export class AuthManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.loginCallback()
|
this.loginCallback()
|
||||||
}
|
} else this.userName = ''
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isLoggedIn: !!loggedIn,
|
isLoggedIn
|
||||||
userName: this.userName
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,9 +112,16 @@ export class AuthManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a session is active, or login is required.
|
* Checks whether a session is active, or login is required.
|
||||||
* @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`.
|
* @returns - a promise which resolves with an object containing three values
|
||||||
|
* - a boolean `isLoggedIn`
|
||||||
|
* - a string `userName` and
|
||||||
|
* - a form `loginForm` if not loggedin.
|
||||||
*/
|
*/
|
||||||
public async checkSession() {
|
public async checkSession(): Promise<{
|
||||||
|
isLoggedIn: boolean
|
||||||
|
userName?: string
|
||||||
|
loginForm?: any
|
||||||
|
}> {
|
||||||
//For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution.
|
//For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution.
|
||||||
//For SAS9 we will send request on SASStoredProcess
|
//For SAS9 we will send request on SASStoredProcess
|
||||||
const url =
|
const url =
|
||||||
@@ -122,7 +138,7 @@ export class AuthManager {
|
|||||||
const isLoggedIn = loginResponse !== 'authErr'
|
const isLoggedIn = loginResponse !== 'authErr'
|
||||||
const userName = isLoggedIn
|
const userName = isLoggedIn
|
||||||
? this.extractUserName(loginResponse)
|
? this.extractUserName(loginResponse)
|
||||||
: this.userName
|
: undefined
|
||||||
|
|
||||||
let loginForm = null
|
let loginForm = null
|
||||||
|
|
||||||
@@ -142,14 +158,14 @@ export class AuthManager {
|
|||||||
|
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
isLoggedIn,
|
isLoggedIn,
|
||||||
userName: userName ?? this.userName,
|
userName,
|
||||||
loginForm
|
loginForm
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private extractUserName = (response: string) =>
|
private extractUserName = (response: any) =>
|
||||||
this.serverType === 'SASVIYA'
|
this.serverType === 'SASVIYA'
|
||||||
? response?.match(/<id>[0-1a-zA-Z]*<\/id>/)?.[0].slice(4, -5)
|
? response?.id
|
||||||
: response?.match(/"title":"Log Off [0-1a-zA-Z]*"/)?.[0].slice(17, -1)
|
: response?.match(/"title":"Log Off [0-1a-zA-Z]*"/)?.[0].slice(17, -1)
|
||||||
|
|
||||||
private getLoginForm(response: any) {
|
private getLoginForm(response: any) {
|
||||||
|
|||||||
Reference in New Issue
Block a user