Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Add external_account_id to OAuth access token response #2982

Merged
merged 1 commit into from Mar 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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