* Scope MCP OAuth tokens to user + workspace
Bind authorization-code grants to the authorizing workspace (via auth codes),
inherit workspace on refresh, resolve MCP/API requests from the token instead
of current_workspace_id, backfill existing grants, and revoke workspace tokens
when a member is removed.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Add multi-workspace MCP OAuth coverage
Cover coexistence of the same client across workspaces, settings
list/disconnect scoped to the current workspace, and API key
controllers excluding workspace-bound MCP grants.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Use constrained foreignUuid for oauth_auth_codes.workspace_id
Match the project's UUID foreign-key convention instead of a separate
foreign() call.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Localize the MCP OAuth authorize consent screen
Wire authorize.blade.php to mcp.* translation keys (including the
workspace scope copy) and cover pt-BR rendering.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Fix invalid Mockery import in bind workspace test
CI treats the non-compound `use Mockery` as an ErrorException and
aborts the whole parallel suite.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Inline MCP OAuth workspace backfill into the migration
Move the one-shot backfill out of a dedicated Action and wrap it in an
explicit transaction so a failure rolls back partial binds/revokes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Nest MCP authorize i18n keys and test backfill rollback
Group consent-screen copy under mcp.authorize.*, and assert the
workspace backfill migration rolls back binds when it fails before
commit.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Hardcode TryPost in the MCP authorize page title
Drop the config('app.name') interpolation from the consent screen title.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Add workspace picker to MCP OAuth consent screen
Let users choose which workspace to bind at authorize time instead of
always using current_workspace_id; silent re-consent still falls back.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Tighten MCP authorize workspace select spacing
Match NativeSelect styling and give the label, control, and helper text room to breathe.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Convert MCP OAuth consent screen to Inertia Vue
Reuse AuthCardLayout, Button, and NativeSelect so the authorize page
matches the app UI. Keep native form posts so Passport's external
redirect still works for MCP client popups.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Polish MCP authorize layout with logo and workspace combobox
Drop the shield and AuthCardLayout double-logo, put TryPost branding
at the top, and reuse the app Combobox pattern for workspace search.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Align MCP OAuth workspace backfill with mcpOAuth scope
Reuse AccessToken::mcpOAuth() so the migration only touches mcp:use
grants on non-PAT clients, matching the rest of the codebase.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Tighten MCP OAuth workspace backfill heuristics
Only touch connected MCP sessions, bind a sole membership or a valid
current workspace, and revoke ambiguous multi-workspace grants instead
of guessing the oldest workspace.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Drop Passport connection override from auth code migration
Always use the app default database connection from .env.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Bind MCP OAuth workspace in AccessTokenRepository
Replace the AccessTokenCreated listener with the same Passport repository
override pattern used for auth codes, so workspace_id is set at persist.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Simplify AccessTokenRepository workspace binding
Drop redundant string casts and the oldest-workspace fallback; keep a
small ownedWorkspace/payloadId helper surface instead.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Extract Passport MCP authorization view from AppServiceProvider
Keep configurePassport thin by moving the Inertia consent props into an
invokable App\Passport\AuthorizationView class.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Simplify AuthorizationView and cover it with direct tests
Use collection higher-order mapping for workspaces/scopes and add focused
tests for current-workspace selection and empty-user props.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Rename BindWorkspaceToAccessTokenTest after listener removal
The suite now covers AuthCodeRepository and AccessTokenRepository
workspace binding, not an AccessTokenCreated listener.
* Fail closed when auth code has no bindable workspace
Authorization-code grants no longer fall back to the user's current
workspace, so a token cannot be minted for a different tenant than consent.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Retrigger CI after GitHub Actions infrastructure failures
Co-authored-by: Cursor <cursoragent@cursor.com>
* chore: retrigger CI
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: harden MCP OAuth workspace binding on refresh and backfill
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: always show MCP OAuth consent to pick a workspace
Disable Passport silent re-consent and require an explicit workspace_id
from the consent form, with Passport wiring moved to its own provider.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: sort MCP connected clients by last used
Show most recently used OAuth connections first on the workspace MCP settings page.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
247 lines
8.2 KiB
PHP
247 lines
8.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\UserWorkspace\Role;
|
|
use App\Models\AccessToken;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
|
|
function createApiKeyApiToken(array $overrides = []): array
|
|
{
|
|
return createApiTestToken($overrides);
|
|
}
|
|
|
|
test('list api keys', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
foreach (['Second', 'Third'] as $name) {
|
|
$token = $result['user']->createToken($name)->token;
|
|
AccessToken::query()->findOrFail($token->id)
|
|
->forceFill(['workspace_id' => $result['workspace']->id])
|
|
->saveQuietly();
|
|
}
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$result['plain_token'],
|
|
])->getJson(
|
|
route('api.api-keys.index'),
|
|
['HTTP_HOST' => 'api.trypost.test']
|
|
);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonCount(3);
|
|
});
|
|
|
|
test('list api keys excludes revoked and other-workspace tokens', function () {
|
|
$result = createApiKeyApiToken();
|
|
$revoked = $result['user']->createToken('Revoked')->token;
|
|
AccessToken::query()->findOrFail($revoked->id)
|
|
->forceFill([
|
|
'workspace_id' => $result['workspace']->id,
|
|
'revoked' => true,
|
|
])
|
|
->saveQuietly();
|
|
|
|
$otherWorkspace = Workspace::factory()->create([
|
|
'account_id' => $result['user']->account_id,
|
|
'user_id' => $result['user']->id,
|
|
]);
|
|
$other = $result['user']->createToken('Other workspace')->token;
|
|
AccessToken::query()->findOrFail($other->id)
|
|
->forceFill(['workspace_id' => $otherWorkspace->id])
|
|
->saveQuietly();
|
|
|
|
$this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->getJson(route('api.api-keys.index'))
|
|
->assertOk()
|
|
->assertJsonCount(1)
|
|
->assertJsonMissing(['id' => $revoked->id])
|
|
->assertJsonMissing(['id' => $other->id]);
|
|
});
|
|
|
|
test('list api keys excludes workspace-bound mcp oauth grants', function () {
|
|
$result = createApiKeyApiToken();
|
|
$oauth = mcpAccessToken($result['user'], mcpOauthClient('Claude'), $result['workspace']);
|
|
|
|
$this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->getJson(route('api.api-keys.index'))
|
|
->assertOk()
|
|
->assertJsonCount(1)
|
|
->assertJsonMissing(['id' => $oauth->id]);
|
|
});
|
|
|
|
test('cannot delete a workspace-bound mcp oauth grant through the api', function () {
|
|
$result = createApiKeyApiToken();
|
|
$oauth = mcpAccessToken($result['user'], mcpOauthClient('Claude'), $result['workspace']);
|
|
|
|
$this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->deleteJson(route('api.api-keys.destroy', $oauth->id))
|
|
->assertNotFound();
|
|
|
|
expect($oauth->fresh()->revoked)->toBeFalse();
|
|
});
|
|
|
|
test('create api key returns plain token', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$result['plain_token'],
|
|
])->postJson(
|
|
route('api.api-keys.store'),
|
|
['name' => 'CI/CD Token'],
|
|
['HTTP_HOST' => 'api.trypost.test']
|
|
);
|
|
|
|
$response->assertCreated();
|
|
$response->assertJsonStructure([
|
|
'token' => ['id', 'name', 'created_at'],
|
|
'plain_token',
|
|
]);
|
|
|
|
expect($response->json('plain_token'))->toBeString();
|
|
});
|
|
|
|
test('workspace members cannot manage api keys through the api', function () {
|
|
$result = createApiKeyApiToken();
|
|
$member = User::factory()->create(['account_id' => $result['user']->account_id]);
|
|
$result['workspace']->members()->attach($member->id, ['role' => Role::Member->value]);
|
|
$member->update(['current_workspace_id' => $result['workspace']->id]);
|
|
$plainToken = passportToken($member, $result['workspace']);
|
|
|
|
$this->withHeaders(['Authorization' => "Bearer {$plainToken}"])
|
|
->getJson(route('api.api-keys.index'))
|
|
->assertForbidden();
|
|
|
|
$this->withHeaders(['Authorization' => "Bearer {$plainToken}"])
|
|
->postJson(route('api.api-keys.store'), ['name' => 'Escalation Key'])
|
|
->assertForbidden();
|
|
});
|
|
|
|
test('create api key validation errors', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$result['plain_token'],
|
|
])->postJson(
|
|
route('api.api-keys.store'),
|
|
[],
|
|
['HTTP_HOST' => 'api.trypost.test']
|
|
);
|
|
|
|
$response->assertUnprocessable();
|
|
$response->assertJsonValidationErrors(['name']);
|
|
});
|
|
|
|
test('delete api key', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$tokenToDelete = $result['user']->createToken('To delete')->token;
|
|
AccessToken::find($tokenToDelete->id)
|
|
->forceFill(['workspace_id' => $result['workspace']->id])
|
|
->saveQuietly();
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$result['plain_token'],
|
|
])->deleteJson(
|
|
route('api.api-keys.destroy', $tokenToDelete->id),
|
|
[],
|
|
['HTTP_HOST' => 'api.trypost.test']
|
|
);
|
|
|
|
$response->assertNoContent();
|
|
expect(AccessToken::find($tokenToDelete->id)->revoked)->toBeTrue();
|
|
});
|
|
|
|
test('cannot delete api key from another workspace', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$otherWorkspace = Workspace::factory()->create();
|
|
$otherUser = $otherWorkspace->owner ?? User::factory()->create([
|
|
'account_id' => $otherWorkspace->account_id,
|
|
]);
|
|
$otherToken = $otherUser->createToken('Other')->token;
|
|
AccessToken::find($otherToken->id)
|
|
->forceFill(['workspace_id' => $otherWorkspace->id])
|
|
->saveQuietly();
|
|
|
|
$response = $this->withHeaders([
|
|
'Authorization' => 'Bearer '.$result['plain_token'],
|
|
])->deleteJson(
|
|
route('api.api-keys.destroy', $otherToken->id),
|
|
[],
|
|
['HTTP_HOST' => 'api.trypost.test']
|
|
);
|
|
|
|
$response->assertNotFound();
|
|
});
|
|
|
|
it('validates api key expires_at must be future date', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->postJson(route('api.api-keys.store'), [
|
|
'name' => 'Test Key',
|
|
'expires_at' => '2020-01-01',
|
|
])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['expires_at']);
|
|
});
|
|
|
|
it('creates an api key without expiration', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$response = $this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->postJson(route('api.api-keys.store'), [
|
|
'name' => 'Never Expires',
|
|
])
|
|
->assertCreated();
|
|
|
|
expect($response->json('token.expires_at'))->toBeNull()
|
|
->and(AccessToken::query()->findOrFail($response->json('token.id'))->expires_at)->toBeNull();
|
|
});
|
|
|
|
it('creates an api key with expiration at end of day', function () {
|
|
$result = createApiKeyApiToken();
|
|
$expiresAt = now()->addDays(14)->startOfDay();
|
|
|
|
$response = $this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->postJson(route('api.api-keys.store'), [
|
|
'name' => 'Expiring Key',
|
|
'expires_at' => $expiresAt->toDateString(),
|
|
])
|
|
->assertCreated();
|
|
|
|
$token = AccessToken::query()->findOrFail($response->json('token.id'));
|
|
|
|
expect($token->expires_at->toDateString())->toBe($expiresAt->toDateString())
|
|
->and($token->expires_at->format('H:i:s'))->toBe('23:59:59');
|
|
});
|
|
|
|
it('allows an api key expiration of today', function () {
|
|
$result = createApiKeyApiToken();
|
|
$today = now()->toDateString();
|
|
|
|
$response = $this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->postJson(route('api.api-keys.store'), [
|
|
'name' => 'Expires Today',
|
|
'expires_at' => $today,
|
|
])
|
|
->assertCreated();
|
|
|
|
$token = AccessToken::query()->findOrFail($response->json('token.id'));
|
|
|
|
expect($token->expires_at->toDateString())->toBe($today)
|
|
->and($token->expires_at->format('H:i:s'))->toBe('23:59:59');
|
|
});
|
|
|
|
it('validates api key name max length', function () {
|
|
$result = createApiKeyApiToken();
|
|
|
|
$this->withHeaders(['Authorization' => 'Bearer '.$result['plain_token']])
|
|
->postJson(route('api.api-keys.store'), [
|
|
'name' => str_repeat('a', 256),
|
|
])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['name']);
|
|
});
|