-
Notifications
You must be signed in to change notification settings - Fork 8.9k
feat(core): let apps report a connection auth failure with a reason #25297
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abdulrahmancodes
wants to merge
9
commits into
main
Choose a base branch
from
claude/cool-pascal-5ay683
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
729b077
feat(core): let apps report a connection auth failure with a reason
abdulrahmancodes 87f811f
docs(apps): document authFailedReason and reportConnectionAuthFailure
abdulrahmancodes 12c37e6
docs(apps): format connections page
abdulrahmancodes ecb248e
refactor: address quality nits on auth-failure reporting
abdulrahmancodes ad931a5
fix: guard auth-failure report against racing reconnect and deleted row
abdulrahmancodes 2e47f12
fix: add authFailedReason to connected-account story mocks
abdulrahmancodes c990b76
chore: regenerate client-sdk metadata client for auth-failure reporting
abdulrahmancodes c298fd9
fix: enforce input validation on the app connections resolver
abdulrahmancodes a9e439f
Merge branch 'main' into claude/cool-pascal-5ay683
abdulrahmancodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...y-sdk/src/sdk/logic-function/connections/__tests__/report-connection-auth-failure.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { | ||
| afterEach, | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| it, | ||
| vi, | ||
| type MockInstance, | ||
| } from 'vitest'; | ||
|
|
||
| import { reportConnectionAuthFailure } from '@/sdk/logic-function/connections/report-connection-auth-failure'; | ||
|
|
||
| describe('reportConnectionAuthFailure', () => { | ||
| let fetchSpy: MockInstance<typeof fetch>; | ||
|
|
||
| beforeEach(() => { | ||
| process.env.TWENTY_API_URL = 'https://api.test'; | ||
| process.env.TWENTY_APP_ACCESS_TOKEN = 'app-token'; | ||
| fetchSpy = vi.spyOn(globalThis, 'fetch'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete process.env.TWENTY_API_URL; | ||
| delete process.env.TWENTY_APP_ACCESS_TOKEN; | ||
| fetchSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('sends the mutation with the connection id and reason', async () => { | ||
| fetchSpy.mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ data: { reportAppConnectionAuthFailure: true } }), | ||
| { status: 200 }, | ||
| ), | ||
| ); | ||
|
|
||
| await reportConnectionAuthFailure('c-1', 'Slack rejected the token'); | ||
|
|
||
| const [url, requestInit] = fetchSpy.mock.calls[0]; | ||
|
|
||
| expect(String(url)).toBe('https://api.test/metadata'); | ||
|
|
||
| const body = JSON.parse(String(requestInit?.body)); | ||
|
|
||
| expect(body.query).toContain('reportAppConnectionAuthFailure'); | ||
| expect(body.variables).toEqual({ | ||
| input: { id: 'c-1', reason: 'Slack rejected the token' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('omits the reason when none is given', async () => { | ||
| fetchSpy.mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ data: { reportAppConnectionAuthFailure: true } }), | ||
| { status: 200 }, | ||
| ), | ||
| ); | ||
|
|
||
| await reportConnectionAuthFailure('c-2'); | ||
|
|
||
| const body = JSON.parse(String(fetchSpy.mock.calls[0][1]?.body)); | ||
|
|
||
| expect(body.variables).toEqual({ input: { id: 'c-2' } }); | ||
| }); | ||
|
|
||
| it('propagates a GraphQL error', async () => { | ||
| fetchSpy.mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ errors: [{ message: 'Connection c-3 not found' }] }), | ||
| { status: 200 }, | ||
| ), | ||
| ); | ||
|
|
||
| await expect(reportConnectionAuthFailure('c-3')).rejects.toThrow( | ||
| 'Connection c-3 not found', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ const GET_APP_CONNECTION_QUERY = ` | |
| accessToken | ||
| scopes | ||
| authFailedAt | ||
| authFailedReason | ||
| } | ||
| } | ||
| `; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ const LIST_APP_CONNECTIONS_QUERY = ` | |
| accessToken | ||
| scopes | ||
| authFailedAt | ||
| authFailedReason | ||
| } | ||
| } | ||
| `; | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
packages/twenty-sdk/src/sdk/logic-function/connections/report-connection-auth-failure.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { postGraphqlRequest } from '@/sdk/logic-function/utils/post-graphql-request.util'; | ||
|
|
||
| const REPORT_APP_CONNECTION_AUTH_FAILURE_MUTATION = ` | ||
| mutation ReportAppConnectionAuthFailure($input: ReportAppConnectionAuthFailureInput!) { | ||
| reportAppConnectionAuthFailure(input: $input) | ||
| } | ||
| `; | ||
|
|
||
| // Marks one of the app's connections as auth-failed (`authFailedAt`), with an | ||
| // optional human-readable reason shown on the connection row in settings. | ||
| // For providers whose tokens the platform never refreshes (a Slack bot token, | ||
| // for example), this is the only way a dead credential becomes visible: the | ||
| // row flips to "Reconnect needed" and `getConnection` starts throwing | ||
| // `AppConnectionAuthFailedError`. The flag clears automatically when the | ||
| // user reconnects. | ||
| export const reportConnectionAuthFailure = async ( | ||
| connectionId: string, | ||
| reason?: string, | ||
| ): Promise<void> => { | ||
| await postGraphqlRequest< | ||
| { input: { id: string; reason?: string } }, | ||
| { reportAppConnectionAuthFailure: boolean } | ||
| >({ | ||
| query: REPORT_APP_CONNECTION_AUTH_FAILURE_MUTATION, | ||
| variables: { input: { id: connectionId, reason } }, | ||
| caller: 'reportConnectionAuthFailure', | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...8/2-38-instance-command-fast-1788445931849-add-auth-failed-reason-to-connected-account.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { QueryRunner } from 'typeorm'; | ||
|
|
||
| import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator'; | ||
| import { FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface'; | ||
|
|
||
| @RegisteredInstanceCommand('2.38.0', 1788445931849) | ||
| export class AddAuthFailedReasonToConnectedAccountFastInstanceCommand implements FastInstanceCommand { | ||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| 'ALTER TABLE "core"."connectedAccount" ADD "authFailedReason" text', | ||
| ); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| 'ALTER TABLE "core"."connectedAccount" DROP COLUMN "authFailedReason"', | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...lication/connection-provider/connections/dtos/report-app-connection-auth-failure.input.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Field, ID, InputType } from '@nestjs/graphql'; | ||
|
|
||
| import { IsOptional, IsString, IsUUID, MaxLength } from 'class-validator'; | ||
|
|
||
| @InputType('ReportAppConnectionAuthFailureInput') | ||
| export class ReportAppConnectionAuthFailureInput { | ||
| @IsUUID() | ||
| @Field(() => ID) | ||
| id: string; | ||
|
|
||
| @IsString() | ||
| @IsOptional() | ||
| @MaxLength(1000) | ||
| @Field({ nullable: true }) | ||
| reason?: string; | ||
|
abdulrahmancodes marked this conversation as resolved.
|
||
| } | ||
66 changes: 66 additions & 0 deletions
66
...n/connection-provider/connections/services/application-connection-auth-failure.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
|
|
||
| import { Repository } from 'typeorm'; | ||
|
|
||
| import { ConnectedAccountProvider } from 'twenty-shared/types'; | ||
| import { isDefined } from 'twenty-shared/utils'; | ||
|
|
||
| import { ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity'; | ||
|
|
||
| type ReportAuthFailureArgs = { | ||
| applicationId: string; | ||
| workspaceId: string; | ||
| requestUserWorkspaceId: string | null; | ||
| id: string; | ||
| reason: string | null; | ||
| }; | ||
|
|
||
| // Lets an app mark one of its own connections as auth-failed, for providers | ||
| // whose tokens never go through the platform refresh flow (a revoked Slack | ||
| // bot token, for example, only ever fails at call time inside the app). | ||
| // The reconnect flow clears the flag the same way it does for refresh | ||
| // failures. | ||
| @Injectable() | ||
| export class ApplicationConnectionAuthFailureService { | ||
| constructor( | ||
| @InjectRepository(ConnectedAccountEntity) | ||
| private readonly connectedAccountRepository: Repository<ConnectedAccountEntity>, | ||
| ) {} | ||
|
|
||
| async reportAuthFailure({ | ||
| applicationId, | ||
| workspaceId, | ||
| requestUserWorkspaceId, | ||
| id, | ||
| reason, | ||
| }: ReportAuthFailureArgs): Promise<void> { | ||
| const account = await this.connectedAccountRepository.findOne({ | ||
| where: { | ||
| id, | ||
| applicationId, | ||
| workspaceId, | ||
| provider: ConnectedAccountProvider.APP, | ||
| }, | ||
| }); | ||
|
|
||
| if (!isDefined(account)) { | ||
| throw new NotFoundException(`Connection ${id} not found`); | ||
| } | ||
|
|
||
| // Same privacy rule as reading a connection: a request-user can only act | ||
| // on their own user-visibility credentials. | ||
| if ( | ||
|
abdulrahmancodes marked this conversation as resolved.
|
||
| isDefined(requestUserWorkspaceId) && | ||
| account.visibility === 'user' && | ||
| account.userWorkspaceId !== requestUserWorkspaceId | ||
| ) { | ||
| throw new NotFoundException(`Connection ${id} not found`); | ||
| } | ||
|
|
||
| await this.connectedAccountRepository.update( | ||
|
abdulrahmancodes marked this conversation as resolved.
Outdated
|
||
| { id: account.id, workspaceId }, | ||
| { authFailedAt: new Date(), authFailedReason: reason }, | ||
|
abdulrahmancodes marked this conversation as resolved.
|
||
| ); | ||
|
abdulrahmancodes marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.