Skip to content

Commit e4bc46b

Browse files
Add oauthTokens to AuthenticationResponse (#1164)
## Description This update introduces the `oauthTokens` in the authentication response interface, serializers, and associated tests. The `oauthTokens` object contains the tokens from OAuth provider. Contact WorkOS support to enable this feature on your environment. ## Documentation Does this require changes to the WorkOS Docs? E.g. the [API Reference](https://workos.com/docs/reference) or code snippets need updates. ``` [ ] Yes ``` If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.
1 parent a7431f9 commit e4bc46b

6 files changed

+65
-3
lines changed

src/user-management/interfaces/authentication-response.interface.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Impersonator, ImpersonatorResponse } from './impersonator.interface';
2+
import { OauthTokens, OauthTokensResponse } from './oauth-tokens.interface';
23
import { User, UserResponse } from './user.interface';
34

45
type AuthenticationMethod =
@@ -18,6 +19,7 @@ export interface AuthenticationResponse {
1819
impersonator?: Impersonator;
1920
authenticationMethod?: AuthenticationMethod;
2021
sealedSession?: string;
22+
oauthTokens?: OauthTokens;
2123
}
2224

2325
export interface AuthenticationResponseResponse {
@@ -27,4 +29,5 @@ export interface AuthenticationResponseResponse {
2729
refresh_token: string;
2830
impersonator?: ImpersonatorResponse;
2931
authentication_method?: AuthenticationMethod;
32+
oauth_tokens?: OauthTokensResponse;
3033
}

src/user-management/interfaces/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export * from './authentication-event.interface';
2-
export * from './authenticate-with-magic-auth-options.interface';
3-
export * from './authenticate-with-password-options.interface';
41
export * from './authenticate-with-code-options.interface';
52
export * from './authenticate-with-email-verification-options.interface';
63
export * from './authenticate-with-magic-auth-options.interface';
@@ -10,6 +7,7 @@ export * from './authenticate-with-password-options.interface';
107
export * from './authenticate-with-refresh-token-options.interface';
118
export * from './authenticate-with-session-cookie.interface';
129
export * from './authenticate-with-totp-options.interface';
10+
export * from './authentication-event.interface';
1311
export * from './authentication-response.interface';
1412
export * from './create-magic-auth-options.interface';
1513
export * from './create-organization-membership-options.interface';
@@ -25,6 +23,7 @@ export * from './list-invitations-options.interface';
2523
export * from './list-organization-memberships-options.interface';
2624
export * from './list-users-options.interface';
2725
export * from './magic-auth.interface';
26+
export * from './oauth-tokens.interface';
2827
export * from './organization-membership.interface';
2928
export * from './password-reset.interface';
3029
export * from './refresh-and-seal-session-data.interface';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface OauthTokens {
2+
accessToken: string;
3+
refreshToken: string;
4+
expiresAt: number;
5+
scopes: string[];
6+
}
7+
8+
export interface OauthTokensResponse {
9+
access_token: string;
10+
refresh_token: string;
11+
expires_at: number;
12+
scopes: string[];
13+
}

src/user-management/serializers/authentication-response.serializer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AuthenticationResponse,
33
AuthenticationResponseResponse,
44
} from '../interfaces';
5+
import { deserializeOauthTokens } from './oauth-tokens.serializer';
56
import { deserializeUser } from './user.serializer';
67

78
export const deserializeAuthenticationResponse = (
@@ -14,6 +15,7 @@ export const deserializeAuthenticationResponse = (
1415
refresh_token,
1516
authentication_method,
1617
impersonator,
18+
oauth_tokens,
1719
...rest
1820
} = authenticationResponse;
1921

@@ -24,6 +26,7 @@ export const deserializeAuthenticationResponse = (
2426
refreshToken: refresh_token,
2527
impersonator,
2628
authenticationMethod: authentication_method,
29+
oauthTokens: deserializeOauthTokens(oauth_tokens),
2730
...rest,
2831
};
2932
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { OauthTokensResponse, OauthTokens } from '../interfaces';
2+
3+
export const deserializeOauthTokens = (
4+
oauthTokens?: OauthTokensResponse,
5+
): OauthTokens | undefined =>
6+
oauthTokens
7+
? {
8+
accessToken: oauthTokens.access_token,
9+
refreshToken: oauthTokens.refresh_token,
10+
expiresAt: oauthTokens.expires_at,
11+
scopes: oauthTokens.scopes,
12+
}
13+
: undefined;

src/user-management/user-management.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,37 @@ describe('UserManagement', () => {
395395
});
396396
});
397397
});
398+
399+
describe('when oauth_tokens is present', () => {
400+
it('deserializes oauth_tokens', async () => {
401+
fetchOnce({
402+
user: userFixture,
403+
oauth_tokens: {
404+
access_token: 'access_token',
405+
refresh_token: 'refresh',
406+
expires_at: 123,
407+
scopes: ['read:users'],
408+
},
409+
});
410+
411+
const resp = await workos.userManagement.authenticateWithCode({
412+
clientId: 'proj_whatever',
413+
code: 'or this',
414+
});
415+
416+
expect(resp).toMatchObject({
417+
user: {
418+
419+
},
420+
oauthTokens: {
421+
accessToken: 'access_token',
422+
refreshToken: 'refresh',
423+
expiresAt: 123,
424+
scopes: ['read:users'],
425+
},
426+
});
427+
});
428+
});
398429
});
399430

400431
describe('authenticateWithRefreshToken', () => {

0 commit comments

Comments
 (0)