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
17 changes: 17 additions & 0 deletions src/sso/interfaces/authorize-logout.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface AuthorizeLogoutOptions {
profileId: string;
}

export interface SerializedAuthorizeLogoutOptions {
profile_id: string;
}

export interface AuthorizeLogoutResponse {
logout_token: string;
logout_url: string;
}

export interface AuthorizeLogout {
logoutToken: string;
logoutUrl: string;
}
1 change: 1 addition & 0 deletions src/sso/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './authorize-logout.interface';
export * from './authorization-url-options.interface';
export * from './connection-type.enum';
export * from './connection.interface';
Expand Down
19 changes: 19 additions & 0 deletions src/sso/serializers/authorize-logout.serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
AuthorizeLogout,
AuthorizeLogoutOptions,
AuthorizeLogoutResponse,
SerializedAuthorizeLogoutOptions,
} from '../interfaces';

export const serializeAuthorizeLogoutOptions = (
options: AuthorizeLogoutOptions,
): SerializedAuthorizeLogoutOptions => ({
profile_id: options.profileId,
});

export const deserializeAuthorizeLogout = (
response: AuthorizeLogoutResponse,
): AuthorizeLogout => ({
logoutToken: response.logout_token,
logoutUrl: response.logout_url,
});
1 change: 1 addition & 0 deletions src/sso/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './authorize-logout.serializer';
export * from './connection.serializer';
export * from './connection-event.serializer';
export * from './list-connections-options.serializer';
Expand Down
27 changes: 27 additions & 0 deletions src/sso/sso.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,33 @@ describe('SSO', () => {
});
});

describe('authorizeLogout', () => {
it('requests a logout token for the profile', async () => {
fetchOnce({
logout_token: 'logout_token_123',
logout_url:
'https://auth.workos.com/sso/logout?token=logout_token_123',
});

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
const logout = await workos.sso.authorizeLogout({
profileId: 'prof_123',
});

expect(fetchURL()).toContain('/sso/logout/authorize');
expect(fetchMethod()).toBe('POST');
expect(fetchBody()).toEqual({ profile_id: 'prof_123' });
expect(fetchHeaders()).toMatchObject({
Authorization: 'Bearer sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU',
});
expect(logout).toEqual({
logoutToken: 'logout_token_123',
logoutUrl:
'https://auth.workos.com/sso/logout?token=logout_token_123',
});
});
});

describe('getProfile', () => {
it('calls the `/sso/profile` endpoint with the provided access token', async () => {
fetchOnce({
Expand Down
26 changes: 26 additions & 0 deletions src/sso/sso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { AutoPaginatable } from '../common/utils/pagination';
import { toQueryString } from '../common/utils/query-string';
import { WorkOS } from '../workos';
import {
AuthorizeLogout,
AuthorizeLogoutOptions,
AuthorizeLogoutResponse,
Connection,
ConnectionResponse,
GetProfileAndTokenOptions,
Expand All @@ -15,12 +18,15 @@ import {
ProfileResponse,
SSOAuthorizationURLOptions,
SSOPKCEAuthorizationURLResult,
SerializedAuthorizeLogoutOptions,
SerializedListConnectionsOptions,
} from './interfaces';
import {
deserializeAuthorizeLogout,
deserializeConnection,
deserializeProfile,
deserializeProfileAndToken,
serializeAuthorizeLogoutOptions,
serializeListConnectionsOptions,
} from './serializers';
import { encodePathParameter } from '../common/utils/encode-path-parameter';
Expand Down Expand Up @@ -294,4 +300,24 @@ export class SSO {

return deserializeProfile(data);
}

/**
* Logout Authorize
*
* Generate a logout token and URL for RP-initiated Single Logout. Redirect the user to `logoutUrl` to sign them out of the identity provider.
* @param options - The profile to log out.
* @returns {Promise<AuthorizeLogout>}
* @throws {BadRequestException} 400
* @throws {NotFoundException} 404
*/
async authorizeLogout(
options: AuthorizeLogoutOptions,
): Promise<AuthorizeLogout> {
const { data } = await this.workos.post<
AuthorizeLogoutResponse,
SerializedAuthorizeLogoutOptions
>('/sso/logout/authorize', serializeAuthorizeLogoutOptions(options));

return deserializeAuthorizeLogout(data);
}
}