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
26 changes: 26 additions & 0 deletions src/api-keys/api-keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../common/utils/test-utils';
import { WorkOS } from '../workos';
import validateApiKeyFixture from './fixtures/validate-api-key.json';
import validateApiKeyUserOwnerFixture from './fixtures/validate-api-key-user-owner.json';
import listOrganizationApiKeysFixture from './fixtures/list-organization-api-keys.json';
import createOrganizationApiKeyFixture from './fixtures/create-organization-api-key.json';

Expand Down Expand Up @@ -49,6 +50,31 @@ describe('ApiKeys', () => {
});
});

it('deserializes a user owner with organizationId', async () => {
fetchOnce(validateApiKeyUserOwnerFixture);
const response = await workos.apiKeys.createValidation({
value: 'sk_123',
});

expect(response).toEqual({
apiKey: {
object: 'api_key',
id: 'api_key_01H5JQDV7R7ATEYZDEG0W5PRYS',
owner: {
type: 'user',
id: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
organizationId: 'org_01H5JQDV7R7ATEYZDEG0W5PRYS',
},
name: 'Test User Api Key',
obfuscatedValue: 'sk_…PRYS',
lastUsedAt: null,
permissions: ['read', 'write'],
createdAt: '2023-07-18T02:07:19.911Z',
updatedAt: '2023-07-18T02:07:19.911Z',
},
});
});

it('returns null if key is invalid', async () => {
fetchOnce({ api_key: null });
const response = await workos.apiKeys.createValidation({
Expand Down
17 changes: 17 additions & 0 deletions src/api-keys/fixtures/validate-api-key-user-owner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"api_key": {
"object": "api_key",
"id": "api_key_01H5JQDV7R7ATEYZDEG0W5PRYS",
"owner": {
"type": "user",
"id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS",
"organization_id": "org_01H5JQDV7R7ATEYZDEG0W5PRYS"
},
"name": "Test User Api Key",
"obfuscated_value": "sk_…PRYS",
"last_used_at": null,
"permissions": ["read", "write"],
"created_at": "2023-07-18T02:07:19.911Z",
"updated_at": "2023-07-18T02:07:19.911Z"
}
}
28 changes: 20 additions & 8 deletions src/api-keys/interfaces/api-key.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ export interface ApiKey {
/** Unique identifier of the API Key. */
id: string;
/** The entity that owns the API Key. */
owner: {
type: 'organization';
id: string;
};
owner:
| {
type: 'organization';
id: string;
}
| {
type: 'user';
id: string;
organizationId: string;
};
/** A descriptive name for the API Key. */
name: string;
/** An obfuscated representation of the API Key value. */
Expand All @@ -26,10 +32,16 @@ export interface ApiKey {
export interface SerializedApiKey {
object: 'api_key';
id: string;
owner: {
type: 'organization';
id: string;
};
owner:
| {
type: 'organization';
id: string;
}
| {
type: 'user';
id: string;
organization_id: string;
};
name: string;
obfuscated_value: string;
last_used_at: string | null;
Expand Down
9 changes: 8 additions & 1 deletion src/api-keys/serializers/api-key.serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ export function deserializeApiKey(apiKey: SerializedApiKey): ApiKey {
return {
object: apiKey.object,
id: apiKey.id,
owner: apiKey.owner,
owner:
apiKey.owner.type === 'user'
? {
type: 'user',
id: apiKey.owner.id,
organizationId: apiKey.owner.organization_id,
}
: apiKey.owner,
name: apiKey.name,
obfuscatedValue: apiKey.obfuscated_value,
lastUsedAt: apiKey.last_used_at,
Expand Down
Loading