Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion e2e/device-client-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
},
"dependencies": {
"@forgerock/device-client": "workspace:*",
"@forgerock/javascript-sdk": "catalog:",
"@forgerock/journey-client": "workspace:*",
"@forgerock/oidc-client": "workspace:*",
"@forgerock/sdk-types": "workspace:*",
"effect": "catalog:effect"
},
"devDependencies": {
Expand Down
166 changes: 104 additions & 62 deletions e2e/device-client-app/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
/*
*
* Copyright (c) 2025 - 2026 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
*/

import { deviceClient } from '@forgerock/device-client';
import type { ConfigOptions, DeviceClient } from '@forgerock/device-client/types';
import {
CallbackType,
Config,
FRAuth,
FRLoginFailure,
FRLoginSuccess,
FRStep,
callbackType,
journey,
NameCallback,
PasswordCallback,
SessionManager,
TokenManager,
UserManager,
} from '@forgerock/javascript-sdk';
StepType,
} from '@forgerock/journey-client';
import type {
JourneyClient,
JourneyClientConfig,
JourneyResult,
JourneyStep,
} from '@forgerock/journey-client/types';
import { oidc } from '@forgerock/oidc-client';
import type { OidcClient, OidcConfig, UserInfoResponse } from '@forgerock/oidc-client/types';
import { Console, Effect } from 'effect';

const logout = Effect.ignore(
Effect.tryPromise({
try: () => SessionManager.logout(),
catch: (err) => new Error(`Logout failed: ${err}`),
}),
);
let cachedOidcClient: OidcClient | null = null;

const start = Effect.tryPromise({
try: () => FRAuth.start(),
catch: (err) => new Error(`Authentication start failed: ${err}`),
}).pipe(Effect.tap((step) => Console.log('Called start', step)));
const oidcClientOrThrow = (): OidcClient => {
if (!cachedOidcClient) {
throw new Error('OIDC client not initialized');
}
return cachedOidcClient;
};

const checkFRStep = (step: FRStep | FRLoginFailure | FRLoginSuccess) =>
const checkForStep = (step: JourneyResult) =>
Effect.try({
try: () => {
if (step.type == 'LoginSuccess' || step.type == 'LoginFailure') {
throw new Error(`Unexpected step type: ${step.type}`);
} else {
if (step && 'type' in step && step.type === StepType.Step) {
return step;
}
throw new Error(`Unexpected step type: ${JSON.stringify(step)}`);
},
catch: (err) => new Error(`Failed to start authentication: ${err}`),
});

const callNext = (step: FRStep) =>
const callNext = (client: JourneyClient, step: JourneyStep) =>
Effect.tryPromise({
try: () => FRAuth.next(step),
try: () => client.next(step),
catch: (err) => new Error(`Failed to proceed to next step: ${err}`),
}).pipe(Effect.tap((step) => Console.log('Got next step', step)));

const getTokens = Effect.tryPromise({
try: () => TokenManager.getTokens(),
catch: (err) => new Error(`Failed to get tokens: ${err}`),
}).pipe(Effect.tap((tokens) => Console.log('Got Tokens', tokens)));
}).pipe(Effect.tap((next) => Console.log('Got next step', next)));

const checkForLoginSuccess = (step: FRStep | FRLoginSuccess | FRLoginFailure) => {
if (step.type === 'LoginSuccess') {
return Effect.succeed(step);
} else if (step.type === 'LoginFailure') {
const checkForLoginSuccess = (result: JourneyResult) => {
if (result && 'type' in result && result.type === StepType.LoginSuccess) {
return Effect.succeed(result);
} else if (result && 'type' in result && result.type === StepType.LoginFailure) {
return Effect.fail(new Error(`Login failed`));
} else {
return Effect.fail(
new Error(`Unexpected step, expected to be in a LoginSuccess but got ${step.type}`),
new Error(
`Unexpected step, expected to be in a LoginSuccess but got ${JSON.stringify(result)}`,
),
);
}
};
Expand All @@ -66,7 +70,6 @@ export const LoginAndGetClient = Effect.gen(function* () {
const url = new URL(window.location.href);
const amUrl = url.searchParams.get('amUrl') || 'https://openam-sdks.forgeblocks.com/am';
const realmPath = url.searchParams.get('realmPath') || 'alpha';
const platformHeader = url.searchParams.get('platformHeader') === 'true' ? true : false;
const tree = url.searchParams.get('tree') || 'selfservice';

/**
Expand All @@ -77,53 +80,92 @@ export const LoginAndGetClient = Effect.gen(function* () {
const un = url.searchParams.get('un') || 'devicetestuser';
const pw = url.searchParams.get('pw') || 'password';

const config: ConfigOptions = {
const deviceConfig: ConfigOptions = {
realmPath,
serverConfig: {
baseUrl: amUrl,
timeout: 3000,
},
};

yield* Effect.try(() =>
Config.set({
platformHeader,
realmPath,
tree,
clientId: 'WebOAuthClient',
scope: 'profile email me.read openid',
redirectUri: `${window.location.origin}/src/_callback/index.html`,
serverConfig: {
baseUrl: amUrl,
timeout: 3000,
},
}),
);
yield* logout;
const realmSegment = realmPath ? `/realms/root/realms/${realmPath}` : '';
const wellknown = `${amUrl.replace(/\/$/, '')}/oauth2${realmSegment}/.well-known/openid-configuration`;
const redirectUri = `${window.location.origin}/src/_callback/index.html`;

const journeyConfig: JourneyClientConfig = {
serverConfig: {
wellknown,
},
};

yield* start.pipe(
Effect.flatMap((step) => checkFRStep(step)),
const oidcConfig: OidcConfig = {
clientId: 'WebOAuthClient',
scope: 'profile email me.read openid',
redirectUri,
serverConfig: {
wellknown,
},
};

const journeyClient = yield* Effect.tryPromise({
try: () => journey({ config: journeyConfig }),
catch: (err) => new Error(`Failed to initialize journey client: ${err}`),
});

const oidcClient = yield* Effect.tryPromise({
try: () => oidc({ config: oidcConfig }),
catch: (err) => new Error(`Failed to initialize OIDC client: ${err}`),
});

if ('error' in oidcClient) {
return yield* Effect.fail(new Error(`Failed to initialize OIDC client: ${oidcClient.error}`));
}

cachedOidcClient = oidcClient;

yield* Effect.tryPromise({
try: () => oidcClientOrThrow().user.logout(),
catch: (err) => new Error(`Logout failed: ${err}`),
}).pipe(Effect.catch((err) => Console.warn('Logout failed, continuing:', err)));

yield* Effect.tryPromise({
try: () => journeyClient.start({ journey: tree }),
catch: (err) => new Error(`Authentication start failed: ${err}`),
}).pipe(
Effect.tap((step) => Console.log('Called start', step)),
Effect.flatMap((step) => checkForStep(step)),
Effect.map((step) => {
step.getCallbackOfType<NameCallback>(CallbackType.NameCallback).setName(un);
step.getCallbackOfType<PasswordCallback>(CallbackType.PasswordCallback).setPassword(pw);
step.getCallbackOfType<NameCallback>(callbackType.NameCallback).setName(un);
step.getCallbackOfType<PasswordCallback>(callbackType.PasswordCallback).setPassword(pw);

return step;
}),
Effect.flatMap((step) => callNext(step)),
Effect.flatMap((step) => callNext(journeyClient, step)),
/**
* Don't explicitly need this but if the journey changes
* maybe we dont get a LoginSuccess
*/
Effect.flatMap((step) => checkForLoginSuccess(step)),
Effect.flatMap(() => getTokens),
Effect.flatMap(() =>
Effect.tryPromise({
try: () => oidcClientOrThrow().token.get({ backgroundRenew: true }),
catch: (err) => new Error(`Failed to get tokens: ${err}`),
}).pipe(Effect.tap((tokens) => Console.log('Got Tokens', tokens))),
),
);

const client: DeviceClient = deviceClient(config);
const client: DeviceClient = deviceClient(deviceConfig);
return client;
});

export const getUser = Effect.tryPromise({
try: () => UserManager.getCurrentUser() as Promise<Record<string, string>>,
try: async (): Promise<UserInfoResponse> => {
const response = await oidcClientOrThrow().user.info();
if ('error' in response) {
throw new Error(`Failed to get user info: ${response.error}`);
}
return response;
},
catch: (err) => new Error(`Failed to get current user: ${err}`),
});

Expand Down
9 changes: 9 additions & 0 deletions e2e/device-client-app/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"references": [
{
"path": "../../packages/device-client/tsconfig.lib.json"
},
{
"path": "../../packages/journey-client/tsconfig.lib.json"
},
{
"path": "../../packages/oidc-client/tsconfig.lib.json"
},
{
"path": "../../packages/sdk-types/tsconfig.lib.json"
}
]
}
3 changes: 1 addition & 2 deletions e2e/mock-api-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"main": "./src/main.js",
"scripts": {
"build": "pnpm nx nxBuild",
"build": "pnpm nx build",
"dev": "node dist/src/main.js --watch-path=./",
"lint": "pnpm nx nxLint",
"serve": "node dist/src/main.js",
Expand All @@ -15,7 +15,6 @@
"dependencies": {
"@effect/language-service": "catalog:effect",
"@effect/opentelemetry": "catalog:effect",
"@effect/platform": "catalog:effect",
"@effect/platform-node": "catalog:effect",
"@opentelemetry/sdk-logs": "0.207.0",
"@opentelemetry/sdk-metrics": "2.2.0",
Expand Down
5 changes: 3 additions & 2 deletions e2e/mock-api-v2/src/handlers/authorize.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/
import { Effect, pipe } from 'effect';
import { MockApi } from '../spec.js';
import { HttpApiBuilder, HttpApiError, HttpServerResponse } from '@effect/platform';
import { HttpApiBuilder, HttpApiError } from 'effect/unstable/httpapi';
import * as HttpServerResponse from 'effect/unstable/http/HttpServerResponse';
import { getFirstElementAndRespond } from '../services/mock-env-helpers/index.js';

const AuthorizeHandlerMock = HttpApiBuilder.group(MockApi, 'Authorization', (handlers) =>
handlers.handle('authorize', ({ urlParams }) =>
handlers.handle('authorize', ({ query: urlParams }) =>
Effect.gen(function* () {
const acr_value = urlParams?.acr_values ?? '';

Expand Down
15 changes: 6 additions & 9 deletions e2e/mock-api-v2/src/handlers/capabilities.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
*/
import { Effect, pipe } from 'effect';
import { MockApi } from '../spec.js';
import {
HttpApiBuilder,
HttpApiError,
HttpServerRequest,
HttpServerResponse,
} from '@effect/platform';
import { HttpApiBuilder, HttpApiError } from 'effect/unstable/httpapi';
import * as HttpServerRequest from 'effect/unstable/http/HttpServerRequest';
import * as HttpServerResponse from 'effect/unstable/http/HttpServerResponse';
import { responseMap } from '../responses/index.js';
import { validator } from '../helpers/match.js';
import { returnSuccessResponseRedirect } from '../responses/return-success-redirect.js';
Expand Down Expand Up @@ -105,9 +102,9 @@ const CapabilitiesHandlerMock = HttpApiBuilder.group(MockApi, 'Capabilities', (h
},
),
),
Effect.flatMap((res) => HttpServerResponse.removeCookie(res, 'stepIndex')),
Effect.flatMap((res) => HttpServerResponse.setStatus(res, 200)),
Effect.flatMap((res) =>
Effect.map((res) => HttpServerResponse.removeCookie(res, 'stepIndex')),
Effect.map((res) => HttpServerResponse.setStatus(res, 200)),
Effect.map((res) =>
HttpServerResponse.setHeader(res, 'Content-Type', 'application/json'),
),
Effect.catchTag('CookieError', () => Effect.fail(new HttpApiError.InternalServerError())),
Expand Down
3 changes: 2 additions & 1 deletion e2e/mock-api-v2/src/handlers/end-session.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* of the MIT license. See the LICENSE file for details.
*/
import { Effect, Console } from 'effect';
import { HttpApiBuilder, HttpServerRequest } from '@effect/platform';
import { HttpApiBuilder } from 'effect/unstable/httpapi';
import * as HttpServerRequest from 'effect/unstable/http/HttpServerRequest';
import { MockApi } from '../spec.js';
import { SessionStorage } from '../services/session.service.js';

Expand Down
2 changes: 1 addition & 1 deletion e2e/mock-api-v2/src/handlers/healthcheck.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpApiBuilder } from '@effect/platform';
import { HttpApiBuilder } from 'effect/unstable/httpapi';
import { MockApi } from '../spec.js';
import { Effect } from 'effect';

Expand Down
6 changes: 3 additions & 3 deletions e2e/mock-api-v2/src/handlers/open-id-configuration.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/
import { Effect } from 'effect';
import { MockApi } from '../spec.js';
import { HttpApiBuilder } from '@effect/platform';
import { HttpServerRequest } from '@effect/platform/HttpServerRequest';
import { HttpApiBuilder } from 'effect/unstable/httpapi';
import { HttpServerRequest } from 'effect/unstable/http/HttpServerRequest';

const OpenidConfigMock = HttpApiBuilder.group(MockApi, 'OpenIDConfig', (handlers) =>
handlers.handle('openid', ({ path: { envid } }) =>
handlers.handle('openid', ({ params: { envid } }) =>
Effect.gen(function* () {
const request = yield* HttpServerRequest;
const url = new URL(request.url);
Expand Down
2 changes: 1 addition & 1 deletion e2e/mock-api-v2/src/handlers/revoke.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { MockApi } from '../spec.js';
import { Tokens } from '../services/tokens.service.js';
import { HttpApiBuilder } from '@effect/platform';
import { HttpApiBuilder } from 'effect/unstable/httpapi';
import { Effect } from 'effect';

const RevokeTokenHandler = HttpApiBuilder.group(MockApi, 'Revoke', (handlers) =>
Expand Down
2 changes: 1 addition & 1 deletion e2e/mock-api-v2/src/handlers/token.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { MockApi } from '../spec.js';
import { Tokens } from '../services/tokens.service.js';
import { HttpApiBuilder } from '@effect/platform';
import { HttpApiBuilder } from 'effect/unstable/httpapi';
import { Effect } from 'effect';

const TokensHandler = HttpApiBuilder.group(MockApi, 'Tokens', (handlers) =>
Expand Down
2 changes: 1 addition & 1 deletion e2e/mock-api-v2/src/handlers/userinfo.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Effect } from 'effect';
import { MockApi } from '../spec.js';
import { UserInfo } from '../services/userinfo.service.js';
import { HttpApiBuilder, HttpApiError } from '@effect/platform';
import { HttpApiBuilder, HttpApiError } from 'effect/unstable/httpapi';
import { BearerToken } from '../middleware/Authorization.js';

const UserInfoMockHandler = HttpApiBuilder.group(MockApi, 'ProtectedRequests', (handlers) =>
Expand Down
Loading
Loading