@sasjs/adapter 3.10.16 → 3.10.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,4 +4,11 @@ export interface LoginOptions {
4
4
  export interface LoginResult {
5
5
  isLoggedIn: boolean;
6
6
  userName: string;
7
+ userLongName: string;
8
+ }
9
+ export interface LoginResultInternal {
10
+ isLoggedIn: boolean;
11
+ userName: string;
12
+ userLongName: string;
13
+ loginForm?: any;
7
14
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Extracts user full name assuming the first word after "title" means log off if not found otherwise in the dictionary
3
+ * @param response SAS response content
4
+ * @returns user full name
5
+ */
6
+ export declare const extractUserLongNameSas9: (response: string) => string;
package/package.json CHANGED
@@ -85,5 +85,5 @@
85
85
  "https": "1.0.0",
86
86
  "tough-cookie": "4.0.0"
87
87
  },
88
- "version": "3.10.16"
88
+ "version": "3.10.17"
89
89
  }
@@ -1,13 +1,15 @@
1
1
  import { ServerType } from '@sasjs/utils/types'
2
2
  import { RequestClient } from '../request/RequestClient'
3
- import { LoginOptions, LoginResult } from '../types/Login'
3
+ import { LoginOptions, LoginResult, LoginResultInternal } from '../types/Login'
4
4
  import { serialize } from '../utils'
5
+ import { extractUserLongNameSas9 } from '../utils/sas9/extractUserLongNameSas9'
5
6
  import { openWebPage } from './openWebPage'
6
7
  import { verifySas9Login } from './verifySas9Login'
7
8
  import { verifySasViyaLogin } from './verifySasViyaLogin'
8
9
 
9
10
  export class AuthManager {
10
11
  public userName = ''
12
+ public userLongName = ''
11
13
  private loginUrl: string
12
14
  private logoutUrl: string
13
15
  private redirectedLoginUrl = `/SASLogon/home`
@@ -33,15 +35,19 @@ export class AuthManager {
33
35
  public async redirectedLogIn({
34
36
  onLoggedOut
35
37
  }: LoginOptions): Promise<LoginResult> {
36
- const { isLoggedIn: isLoggedInAlready, userName: currentSessionUsername } =
37
- await this.fetchUserName()
38
+ const {
39
+ isLoggedIn: isLoggedInAlready,
40
+ userName: currentSessionUserName,
41
+ userLongName: currentSessionUserLongName
42
+ } = await this.fetchUserName()
38
43
 
39
44
  if (isLoggedInAlready) {
40
45
  await this.loginCallback()
41
46
 
42
47
  return {
43
48
  isLoggedIn: true,
44
- userName: currentSessionUsername
49
+ userName: currentSessionUserName,
50
+ userLongName: currentSessionUserLongName
45
51
  }
46
52
  }
47
53
 
@@ -56,7 +62,7 @@ export class AuthManager {
56
62
  )
57
63
 
58
64
  if (!loginPopup) {
59
- return { isLoggedIn: false, userName: '' }
65
+ return { isLoggedIn: false, userName: '', userLongName: '' }
60
66
  }
61
67
 
62
68
  const { isLoggedIn } =
@@ -71,14 +77,14 @@ export class AuthManager {
71
77
  await this.performCASSecurityCheck()
72
78
  }
73
79
 
74
- const { userName } = await this.fetchUserName()
80
+ const { userName, userLongName } = await this.fetchUserName()
75
81
 
76
82
  await this.loginCallback()
77
83
 
78
- return { isLoggedIn: true, userName }
84
+ return { isLoggedIn: true, userName, userLongName }
79
85
  }
80
86
 
81
- return { isLoggedIn: false, userName: '' }
87
+ return { isLoggedIn: false, userName: '', userLongName: '' }
82
88
  }
83
89
 
84
90
  /**
@@ -93,27 +99,26 @@ export class AuthManager {
93
99
  username,
94
100
  password
95
101
  }
102
+ this.userName = ''
103
+ this.userLongName = ''
96
104
 
97
105
  let {
98
106
  isLoggedIn: isLoggedInAlready,
99
107
  loginForm,
100
- userName: currentSessionUsername
108
+ userLongName: currentSessionUserLongName
101
109
  } = await this.checkSession()
102
110
 
103
111
  if (isLoggedInAlready) {
104
- if (currentSessionUsername === loginParams.username) {
105
- await this.loginCallback()
106
-
107
- this.userName = currentSessionUsername!
108
- return {
109
- isLoggedIn: true,
110
- userName: this.userName
111
- }
112
- } else {
113
- await this.logOut()
114
- loginForm = await this.getNewLoginForm()
112
+ await this.loginCallback()
113
+
114
+ this.userName = loginParams.username
115
+ this.userLongName = currentSessionUserLongName
116
+ return {
117
+ isLoggedIn: true,
118
+ userName: this.userName,
119
+ userLongName: this.userLongName
115
120
  }
116
- } else this.userName = ''
121
+ }
117
122
 
118
123
  let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
119
124
 
@@ -128,10 +133,7 @@ export class AuthManager {
128
133
 
129
134
  const res = await this.checkSession()
130
135
  isLoggedIn = res.isLoggedIn
131
-
132
- if (isLoggedIn) this.userName = res.userName
133
- } else {
134
- this.userName = loginParams.username
136
+ this.userLongName = res.userLongName
135
137
  }
136
138
 
137
139
  if (isLoggedIn) {
@@ -140,11 +142,13 @@ export class AuthManager {
140
142
  }
141
143
 
142
144
  this.loginCallback()
143
- } else this.userName = ''
145
+ this.userName = loginParams.username
146
+ }
144
147
 
145
148
  return {
146
149
  isLoggedIn,
147
- userName: this.userName
150
+ userName: this.userName,
151
+ userLongName: this.userLongName
148
152
  }
149
153
  }
150
154
 
@@ -195,15 +199,12 @@ export class AuthManager {
195
199
  * Checks whether a session is active, or login is required.
196
200
  * @returns - a promise which resolves with an object containing three values
197
201
  * - a boolean `isLoggedIn`
198
- * - a string `userName` and
202
+ * - a string `userName`,
203
+ * - a string `userFullName` and
199
204
  * - a form `loginForm` if not loggedin.
200
205
  */
201
- public async checkSession(): Promise<{
202
- isLoggedIn: boolean
203
- userName: string
204
- loginForm?: any
205
- }> {
206
- const { isLoggedIn, userName } = await this.fetchUserName()
206
+ public async checkSession(): Promise<LoginResultInternal> {
207
+ const { isLoggedIn, userName, userLongName } = await this.fetchUserName()
207
208
  let loginForm = null
208
209
 
209
210
  if (!isLoggedIn) {
@@ -216,7 +217,8 @@ export class AuthManager {
216
217
 
217
218
  return Promise.resolve({
218
219
  isLoggedIn,
219
- userName: userName.toLowerCase(),
220
+ userName,
221
+ userLongName,
220
222
  loginForm
221
223
  })
222
224
  }
@@ -245,10 +247,7 @@ export class AuthManager {
245
247
  return await this.getLoginForm(formResponse)
246
248
  }
247
249
 
248
- private async fetchUserName(): Promise<{
249
- isLoggedIn: boolean
250
- userName: string
251
- }> {
250
+ private async fetchUserName(): Promise<LoginResult> {
252
251
  const url =
253
252
  this.serverType === ServerType.SasViya
254
253
  ? `${this.serverUrl}/identities/users/@currentUser`
@@ -263,15 +262,19 @@ export class AuthManager {
263
262
  })
264
263
 
265
264
  const isLoggedIn = loginResponse !== 'authErr'
266
- const userName = isLoggedIn ? this.extractUserName(loginResponse) : ''
267
265
 
268
266
  if (!isLoggedIn) {
269
267
  //We will logout to make sure cookies are removed and login form is presented
270
268
  //Residue can happen in case of session expiration
271
269
  await this.logOut()
270
+ return { isLoggedIn, userName: '', userLongName: '' }
272
271
  }
273
272
 
274
- return { isLoggedIn, userName }
273
+ return {
274
+ isLoggedIn,
275
+ userName: this.extractUserName(loginResponse),
276
+ userLongName: this.extractUserLongName(loginResponse)
277
+ }
275
278
  }
276
279
 
277
280
  private extractUserName = (response: any): string => {
@@ -280,18 +283,27 @@ export class AuthManager {
280
283
  return response?.id
281
284
 
282
285
  case ServerType.Sas9:
283
- const matched = response?.match(/"title":"Log Off [0-1a-zA-Z ]*"/)
284
- const username = matched?.[0].slice(17, -1)
286
+ return ''
287
+
288
+ case ServerType.Sasjs:
289
+ return response?.username
290
+
291
+ default:
292
+ console.error('Server Type not found in extractUserName function')
293
+ return ''
294
+ }
295
+ }
285
296
 
286
- if (!username.includes(' ')) return username
297
+ private extractUserLongName = (response: any): string => {
298
+ switch (this.serverType) {
299
+ case ServerType.SasViya:
300
+ return response?.name
287
301
 
288
- return username
289
- .split(' ')
290
- .map((name: string) => name.slice(0, 3).toLowerCase())
291
- .join('')
302
+ case ServerType.Sas9:
303
+ return extractUserLongNameSas9(response)
292
304
 
293
305
  case ServerType.Sasjs:
294
- return response?.username
306
+ return response?.displayName
295
307
 
296
308
  default:
297
309
  console.error('Server Type not found in extractUserName function')
@@ -5,4 +5,11 @@ export interface LoginOptions {
5
5
  export interface LoginResult {
6
6
  isLoggedIn: boolean
7
7
  userName: string
8
+ userLongName: string
9
+ }
10
+ export interface LoginResultInternal {
11
+ isLoggedIn: boolean
12
+ userName: string
13
+ userLongName: string
14
+ loginForm?: any
8
15
  }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Dictionary should contain only languages in SAS where `logout` text
3
+ * is represented with more then one word
4
+ */
5
+ const dictionary = ['Log Off']
6
+
7
+ /**
8
+ * Extracts user full name assuming the first word after "title" means log off if not found otherwise in the dictionary
9
+ * @param response SAS response content
10
+ * @returns user full name
11
+ */
12
+ export const extractUserLongNameSas9 = (response: string) => {
13
+ const regex = /"title":\s?".*?"/
14
+
15
+ const matched = response?.match(regex)
16
+ let fullName = matched?.[0].split(':')[1].trim()
17
+ let breakIndex = fullName?.indexOf(' ')
18
+
19
+ if (!fullName) return 'unknown'
20
+
21
+ dictionary.map((logoutWord) => {
22
+ const index = fullName?.indexOf(logoutWord) || -1
23
+
24
+ if (index > -1) {
25
+ breakIndex = index + logoutWord.length
26
+ }
27
+ })
28
+
29
+ //Cut only name
30
+ return fullName.slice(breakIndex, -1).trim()
31
+ }
package/types/Login.d.ts CHANGED
@@ -4,4 +4,11 @@ export interface LoginOptions {
4
4
  export interface LoginResult {
5
5
  isLoggedIn: boolean;
6
6
  userName: string;
7
+ userLongName: string;
8
+ }
9
+ export interface LoginResultInternal {
10
+ isLoggedIn: boolean;
11
+ userName: string;
12
+ userLongName: string;
13
+ loginForm?: any;
7
14
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Extracts user full name assuming the first word after "title" means log off if not found otherwise in the dictionary
3
+ * @param response SAS response content
4
+ * @returns user full name
5
+ */
6
+ export declare const extractUserLongNameSas9: (response: string) => string;
@@ -0,0 +1 @@
1
+ export {};