Skip to content
Merged
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
203 changes: 203 additions & 0 deletions src/admin-portal/admin-portal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import fetch from 'jest-fetch-mock';
import { fetchBody, fetchOnce } from '../common/utils/test-utils';
import { WorkOS } from '../workos';
import portalLinkResponse from './fixtures/portal-link-response.fixture.json';
import generateLinkInvalid from './fixtures/generate-link-invalid.fixture.json';

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

describe('AdminPortal', () => {
beforeEach(() => fetch.resetMocks());

describe('generateLink', () => {
describe('with the sso intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'sso',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'sso',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with the dsync intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'dsync',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'dsync',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with the audit_logs intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'audit_logs',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'audit_logs',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with the log_streams intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'log_streams',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'log_streams',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with the domain_verification intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'domain_verification',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'domain_verification',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with the certificate_renewal intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'certificate_renewal',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'certificate_renewal',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with the bring_your_own_key intent', () => {
it('returns an Admin Portal link', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'bring_your_own_key',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
});

expect(fetchBody()).toEqual({
intent: 'bring_your_own_key',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
});
expect(link).toBeDefined();
});
});

describe('with intentOptions and adminEmails', () => {
it('serializes the new parameters correctly', async () => {
fetchOnce(portalLinkResponse, { status: 201 });

const { link } = await workos.adminPortal.generateLink({
intent: 'sso',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
returnUrl: 'https://www.example.com',
intentOptions: {
sso: {
bookmarkSlug: 'chatgpt',
providerType: 'GoogleSAML',
},
},
adminEmails: ['admin@example.com'],
});

expect(fetchBody()).toEqual({
intent: 'sso',
organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
return_url: 'https://www.example.com',
intent_options: {
sso: {
bookmark_slug: 'chatgpt',
provider_type: 'GoogleSAML',
},
},
admin_emails: ['admin@example.com'],
});
expect(link).toBeDefined();
});
});

describe('with an invalid organization', () => {
it('throws an error', async () => {
fetchOnce(generateLinkInvalid, {
status: 400,
headers: { 'X-Request-ID': 'a-request-id' },
});

await expect(
workos.adminPortal.generateLink({
intent: 'sso',
organization: 'bogus-id',
returnUrl: 'https://www.example.com',
}),
).rejects.toThrow(
'Could not find an organization with the id, bogus-id.',
);
expect(fetchBody()).toEqual({
intent: 'sso',
organization: 'bogus-id',
return_url: 'https://www.example.com',
});
});
});
});
});
33 changes: 33 additions & 0 deletions src/admin-portal/admin-portal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is auto-generated by oagen. Do not edit.

import type { WorkOS } from '../workos';
import type {
PortalLinkResponse,
PortalLinkResponseWire,
} from './interfaces/portal-link-response.interface';
import type { GenerateLink } from './interfaces/generate-link.interface';
import { deserializePortalLinkResponse } from './serializers/portal-link-response.serializer';
import { serializeGenerateLink } from './serializers/generate-link.serializer';

export class AdminPortal {
constructor(private readonly workos: WorkOS) {}

/**
* Generate a Portal Link
*
* Generate a Portal Link scoped to an Organization.
* @param payload - Admin Portal link generation parameters.
* @returns {Promise<PortalLinkResponse>}
* @throws {BadRequestException} 400
* @throws {UnauthorizedException} 401
* @throws {NotFoundException} 404
* @throws {UnprocessableEntityException} 422
*/
async generateLink(payload: GenerateLink): Promise<PortalLinkResponse> {
const { data } = await this.workos.post<PortalLinkResponseWire>(
'/portal/generate_link',
serializeGenerateLink(payload),
);
return deserializePortalLinkResponse(data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Could not find an organization with the id, bogus-id."
}
13 changes: 13 additions & 0 deletions src/admin-portal/fixtures/generate-link.fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"return_url": "https://example.com/admin-portal/return",
"success_url": "https://example.com/admin-portal/success",
"organization": "org_01EHZNVPK3SFK441A1RGBFSHRT",
"intent": "sso",
"intent_options": {
"sso": {
"bookmark_slug": "chatgpt",
"provider_type": "GoogleSAML"
}
},
"admin_emails": ["admin@example.com"]
}
6 changes: 6 additions & 0 deletions src/admin-portal/fixtures/intent-options.fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sso": {
"bookmark_slug": "chatgpt",
"provider_type": "GoogleSAML"
}
}
3 changes: 3 additions & 0 deletions src/admin-portal/fixtures/portal-link-response.fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"link": "https://setup.workos.com?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Replace secret-like token in fixture to avoid leak-scanner failures.

Line 2 uses a JWT-looking token string. Even in test fixtures, this pattern can be treated as exposed credentials and fail security checks. Use an obviously fake token value instead.

Suggested change
-  "link": "https://setup.workos.com?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
+  "link": "https://setup.workos.com?token=test_admin_portal_token"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"link": "https://setup.workos.com?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
"link": "https://setup.workos.com?token=test_admin_portal_token"
🧰 Tools
🪛 Betterleaks (1.1.2)

[high] 2-2: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/admin-portal/fixtures/portal-link-response.fixture.json` at line 2, The
fixture's "link" value contains a JWT-like token that triggers leak scanners;
update the value for the "link" key in portal-link-response.fixture.json to use
a clearly fake placeholder token (e.g., replace the query param value with
"REDACTED_TOKEN" or "fake-token-123") while keeping the URL structure
(https://setup.workos.com?token=...) so tests still parse the URL format but no
real/secret-looking token remains.

}
4 changes: 4 additions & 0 deletions src/admin-portal/fixtures/sso-intent-options.fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bookmark_slug": "chatgpt",
"provider_type": "GoogleSAML"
}
41 changes: 41 additions & 0 deletions src/admin-portal/interfaces/generate-link.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is auto-generated by oagen. Do not edit.

import type {
IntentOptions,
IntentOptionsResponse,
} from './intent-options.interface';
import type { GenerateLinkIntent } from '../../common/interfaces/generate-link-intent.interface';

export interface GenerateLink {
/** The URL to go to when an admin clicks on your logo in the Admin Portal. If not specified, the return URL configured on the [Redirects](https://dashboard.workos.com/redirects) page will be used. */
returnUrl?: string;
/** The URL to redirect the admin to when they finish setup. If not specified, the success URL configured on the [Redirects](https://dashboard.workos.com/redirects) page will be used. */
successUrl?: string;
/** An [Organization](https://workos.com/docs/reference/organization) identifier. */
organization: string;
/**
*
* The intent of the Admin Portal.
* - `sso` - Launch Admin Portal for creating SSO connections
* - `dsync` - Launch Admin Portal for creating Directory Sync connections
* - `audit_logs` - Launch Admin Portal for viewing Audit Logs
* - `log_streams` - Launch Admin Portal for creating Log Streams
* - `domain_verification` - Launch Admin Portal for Domain Verification
* - `certificate_renewal` - Launch Admin Portal for renewing SAML Certificates
* - `bring_your_own_key` - Launch Admin Portal for configuring Bring Your Own Key
*/
intent?: GenerateLinkIntent;
/** Options to configure the Admin Portal based on the intent. */
intentOptions?: IntentOptions;
/** The email addresses of the IT admins to grant access to the Admin Portal for the given organization. Accepts up to 20 emails. */
adminEmails?: string[];
}

export interface GenerateLinkResponse {
return_url?: string;
success_url?: string;
organization: string;
intent?: GenerateLinkIntent;
intent_options?: IntentOptionsResponse;
admin_emails?: string[];
}
6 changes: 6 additions & 0 deletions src/admin-portal/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is auto-generated by oagen. Do not edit.

export * from './generate-link.interface';
export * from './intent-options.interface';
export * from './portal-link-response.interface';
export * from './sso-intent-options.interface';
15 changes: 15 additions & 0 deletions src/admin-portal/interfaces/intent-options.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is auto-generated by oagen. Do not edit.

import type {
SSOIntentOptions,
SSOIntentOptionsResponse,
} from './sso-intent-options.interface';

export interface IntentOptions {
/** SSO-specific options for the Admin Portal. */
sso: SSOIntentOptions;
}

export interface IntentOptionsResponse {
sso: SSOIntentOptionsResponse;
}
10 changes: 10 additions & 0 deletions src/admin-portal/interfaces/portal-link-response.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is auto-generated by oagen. Do not edit.

export interface PortalLinkResponse {
/** An ephemeral link to initiate the Admin Portal. */
link: string;
}

export interface PortalLinkResponseWire {
link: string;
}
13 changes: 13 additions & 0 deletions src/admin-portal/interfaces/sso-intent-options.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file is auto-generated by oagen. Do not edit.

export interface SSOIntentOptions {
/** The bookmark slug to use for SSO. */
bookmarkSlug?: string;
/** The SSO provider type to configure. */
providerType?: 'GoogleSAML';
}

export interface SSOIntentOptionsResponse {
bookmark_slug?: string;
provider_type?: 'GoogleSAML';
}
Loading
Loading