UNPKG

@umbraco/playwright-testhelpers

Version:

Test helpers for making playwright tests for Umbraco solutions

105 lines 4.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoginApiHelper = void 0; const crypto_1 = require("crypto"); class LoginApiHelper { api; page; constructor(api, page) { this.api = api; this.page = page; } async login(userEmail, password) { const codeVerifier = "12345"; // A static state value for testing const stateValue = 'myStateValue'; // A static state value for testing const cookie = await this.getCookie(userEmail, password); const codeChallenge = await this.createCodeChallenge(codeVerifier); const authorizationCode = await this.getAuthorizationCode(codeChallenge, cookie, stateValue); const refreshToken = await this.getRefreshToken(cookie, codeVerifier, authorizationCode); const accessToken = await this.getAccessToken(cookie, refreshToken.refresh_token); return { cookie, accessToken, refreshToken }; } async getCookie(userEmail, password) { const response = await this.page.request.post(this.api.baseUrl + '/umbraco/management/api/v1/security/back-office/login', { headers: { 'Content-Type': 'application/json', Referer: this.api.baseUrl, Origin: this.api.baseUrl, }, data: { username: userEmail, password: password }, ignoreHTTPSErrors: true }); // Ensure the cookie is properly captured return response.headers()['set-cookie']; } async createCodeChallenge(codeVerifier) { return (0, crypto_1.createHash)('sha256').update(codeVerifier, 'utf8').digest('base64').replace(/=/g, '').trim(); } async getAuthorizationCode(codeChallenge, cookie, stateValue) { const authorizationUrl = `${this.api.baseUrl}/umbraco/management/api/v1/security/back-office/authorize?client_id=umbraco-back-office&response_type=code&redirect_uri=${encodeURIComponent(this.api.baseUrl + '/umbraco/oauth_complete')}&code_challenge_method=S256&code_challenge=${codeChallenge}&state=${stateValue}&scope=offline_access&prompt=consent&access_type=offline`; const response = await this.page.request.get(authorizationUrl, { headers: { Cookie: cookie, Referer: this.api.baseUrl, }, ignoreHTTPSErrors: true, maxRedirects: 0 }); // Parse the authorization code from the redirect URL const locationHeader = response.headers()['location']; if (!locationHeader) { throw new Error('Authorization redirect location not found'); } // Extract the authorization code from the location header return new URLSearchParams(locationHeader.split('?')[1]).get('code'); } async getRefreshToken(cookie, codeVerifier, authorizationCode) { const response = await this.page.request.post(this.api.baseUrl + '/umbraco/management/api/v1/security/back-office/token', { headers: { 'Content-Type': 'application/x-www-form-urlencoded', Cookie: cookie, Origin: this.api.baseUrl }, form: { grant_type: 'authorization_code', client_id: 'umbraco-back-office', redirect_uri: this.api.baseUrl + '/umbraco/oauth_complete', code: authorizationCode, code_verifier: codeVerifier }, ignoreHTTPSErrors: true }); if (response.status() !== 200) { console.error('Failed to retrieve refresh token'); } return await response.json(); } async getAccessToken(cookie, refreshToken) { const response = await this.page.request.post(this.api.baseUrl + '/umbraco/management/api/v1/security/back-office/token', { headers: { 'Content-Type': 'application/x-www-form-urlencoded', Cookie: cookie, Origin: this.api.baseUrl }, form: { grant_type: 'refresh_token', client_id: 'umbraco-back-office', redirect_uri: this.api.baseUrl + '/umbraco/oauth_complete', refresh_token: refreshToken, }, ignoreHTTPSErrors: true }); if (response.status() === 200) { console.log('Login successful'); } else { console.error('Login failed'); } return await response.json(); } } exports.LoginApiHelper = LoginApiHelper; //# sourceMappingURL=LoginApiHelper.js.map