Skip to content

Commit

Permalink
feat(backend): Add external_account_id to OAuth access token response
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaspt committed Mar 12, 2024
1 parent ef72c0a commit c91ddef
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-poets-leave.md
@@ -0,0 +1,5 @@
---
'@clerk/backend': minor
---

Add `external_account_id` to OAuth access token response
35 changes: 35 additions & 0 deletions packages/backend/src/api/__tests__/factory.test.ts
Expand Up @@ -212,5 +212,40 @@ export default (QUnit: QUnit) => {
}),
);
});

test('successfully retrieves user access tokens from backend API for a specific provider', async assert => {
const fakeResponse = [
{
external_account_id: 'eac_2dYS7stz9bgxQsSRvNqEAHhuxvW',
object: 'oauth_access_token',
token: '<token>',
provider: 'oauth_google',
public_metadata: {},
label: null,
scopes: ['email', 'profile'],
},
];

fakeFetch = sinon.stub(runtime, 'fetch');
fakeFetch.onCall(0).returns(jsonOk(fakeResponse));

const response = await apiClient.users.getUserOauthAccessToken('user_deadbeef', 'oauth_google');

assert.equal(response[0].externalAccountId, 'eac_2dYS7stz9bgxQsSRvNqEAHhuxvW');
assert.equal(response[0].provider, 'oauth_google');
assert.equal(response[0].token, '<token>');
assert.deepEqual(response[0].scopes, ['email', 'profile']);

assert.ok(
fakeFetch.calledOnceWith('https://api.clerk.test/v1/users/user_deadbeef/oauth_access_tokens/oauth_google', {
method: 'GET',
headers: {
Authorization: 'Bearer deadbeef',
'Content-Type': 'application/json',
'User-Agent': '@clerk/backend@0.0.0-test',
},
}),
);
});
});
};
1 change: 1 addition & 0 deletions packages/backend/src/api/resources/JSON.ts
Expand Up @@ -119,6 +119,7 @@ export interface InvitationJSON extends ClerkResourceJSON {
}

export interface OauthAccessTokenJSON {
external_account_id: string;
object: typeof ObjectType.OauthAccessToken;
token: string;
provider: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/api/resources/OauthAccessToken.ts
Expand Up @@ -2,6 +2,7 @@ import type { OauthAccessTokenJSON } from './JSON';

export class OauthAccessToken {
constructor(
readonly externalAccountId: string,
readonly provider: string,
readonly token: string,
readonly publicMetadata: Record<string, unknown> = {},
Expand All @@ -12,6 +13,7 @@ export class OauthAccessToken {

static fromJSON(data: OauthAccessTokenJSON) {
return new OauthAccessToken(
data.external_account_id,
data.provider,
data.token,
data.public_metadata,
Expand Down

0 comments on commit c91ddef

Please sign in to comment.