-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathCertAuthenticator.ts
87 lines (78 loc) · 3.63 KB
/
CertAuthenticator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { CertAuthenticator as CertAuthenticatorType } from "../../types/wdi5.types.js"
import Authenticator from "./Authenticator.js"
import { CertificateAuthSession } from "playwright-client-certificate-login"
class CertAuthenticator extends Authenticator {
private origin: string
private url: string
private pfxPath: string
private pfxPassword: string
constructor(options: CertAuthenticatorType, browserInstanceName) {
super(browserInstanceName)
this.origin = options.certificationOrigin
this.url = options.certificateUrl
this.pfxPath = options.certificatePfxPath
this.pfxPassword = options.certificatePfxPassword
}
async login() {
if (!(await this.getIsLoggedIn())) {
let sapSession
try {
// Instantiate the CertificateAuthSession
sapSession = new CertificateAuthSession({
origin: "https://accounts.sap.com",
url: "https://emea.cockpit.btp.cloud.sap/cockpit#/", // Target URL for login
pfxPath: "./sap.pfx", // Updated PFX file path created in workflow
passphrase: "", // Passphrase for PFX if needed
timeout: 60000 // Optional timeout
})
// Authenticate
await sapSession.authenticate()
// Check if login was successful by verifying the URL or a page element
// const page = sapSession.getPage()
const cookies = sapSession.getCookies()
for (const cookie of cookies) {
try {
// Handle __HOST- prefixed cookies differently
if (cookie.name.startsWith("__HOST-")) {
await this.browserInstance.setCookies({
name: cookie.name,
value: cookie.value,
path: "/",
secure: true,
sameSite: cookie.sameSite || "Strict",
httpOnly: cookie.httpOnly
})
} else {
// Remove leading dot from domain if present
// const domain = cookie.domain.startsWith(".") ? cookie.domain.slice(1) : cookie.domain
const domain = "sap.com"
await this.browserInstance.setCookies({
name: cookie.name,
value: cookie.value,
domain: domain,
path: cookie.path,
httpOnly: cookie.httpOnly,
secure: cookie.secure,
sameSite: cookie.sameSite
})
}
} catch (error) {
console.warn(`Failed to set cookie ${cookie.name}: ${error.message}`)
// Continue with other cookies even if one fails
}
}
await this.browserInstance.refresh()
} catch (error) {
console.error("Error during login:", error.message)
process.exit(1) // Exit with failure on error
} finally {
// Ensure the session closes properly
if (sapSession) {
await sapSession.close()
}
}
this.setIsLoggedIn(true)
}
}
}
export default CertAuthenticator