-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.authentication.ts
72 lines (59 loc) · 1.8 KB
/
functions.authentication.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable @eslint-community/eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-misused-promises, @typescript-eslint/strict-boolean-expressions */
import { AdWebAuthConnector } from '@cityssm/ad-web-auth-connector'
import ActiveDirectory from 'activedirectory2'
import type { ConfigActiveDirectory } from '../types/configTypes.js'
import { getConfigProperty } from './functions.config.js'
const userDomain = getConfigProperty('application.userDomain')
const activeDirectoryConfig = getConfigProperty('activeDirectory')
async function authenticateViaActiveDirectory(
userName: string,
password: string
): Promise<boolean> {
return await new Promise((resolve) => {
try {
const ad = new ActiveDirectory(
activeDirectoryConfig as ConfigActiveDirectory
)
ad.authenticate(
`${userDomain}\\${userName}`,
password,
async (error, auth) => {
if (error) {
resolve(false)
}
resolve(auth)
}
)
} catch {
resolve(false)
}
})
}
const adWebAuthConfig = getConfigProperty('adWebAuthConfig')
const adWebAuth =
adWebAuthConfig === undefined
? undefined
: new AdWebAuthConnector(adWebAuthConfig)
async function authenticateViaADWebAuth(
userName: string,
password: string
): Promise<boolean> {
return (
(await adWebAuth?.authenticate(`${userDomain}\\${userName}`, password)) ??
false
)
}
const authenticateFunction =
activeDirectoryConfig === undefined
? authenticateViaADWebAuth
: authenticateViaActiveDirectory
export const authenticate = async (
userName: string,
password: string
): Promise<boolean> => {
if ((userName ?? '') === '' || (password ?? '') === '') {
return false
}
return await authenticateFunction(userName, password)
}