* 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>
188 lines
7.1 KiB
PHP
188 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\UserWorkspace\Role;
|
|
use App\Http\Middleware\App\EnsureCanAuthorizeMcp;
|
|
use App\Models\Account;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
test('dynamic oauth client registration is rate limited', function () {
|
|
$payload = [
|
|
'client_name' => 'MCP Client',
|
|
'redirect_uris' => ['https://client.example/callback'],
|
|
'grant_types' => ['authorization_code'],
|
|
'response_types' => ['code'],
|
|
'token_endpoint_auth_method' => 'none',
|
|
];
|
|
|
|
for ($attempt = 0; $attempt < 30; $attempt++) {
|
|
$this->postJson('/oauth/register', $payload)->assertSuccessful();
|
|
}
|
|
|
|
$this->postJson('/oauth/register', $payload)->assertTooManyRequests();
|
|
});
|
|
|
|
test('dynamic oauth registration rejects custom callback schemes', function (string $redirectUri) {
|
|
$this->postJson('/oauth/register', [
|
|
'client_name' => 'Native MCP Client',
|
|
'redirect_uris' => [$redirectUri],
|
|
'grant_types' => ['authorization_code'],
|
|
'response_types' => ['code'],
|
|
'token_endpoint_auth_method' => 'none',
|
|
])->assertBadRequest();
|
|
})->with([
|
|
'cursor' => 'cursor://oauth/callback',
|
|
'vscode' => 'vscode://oauth/callback',
|
|
]);
|
|
|
|
test('mcp oauth consent page is available for workspace viewers', function () {
|
|
$account = Account::factory()->create();
|
|
$owner = User::factory()->create(['account_id' => $account->id]);
|
|
$account->update(['owner_id' => $owner->id]);
|
|
$workspace = Workspace::factory()->create([
|
|
'account_id' => $account->id,
|
|
'user_id' => $owner->id,
|
|
'name' => 'Viewer Workspace',
|
|
]);
|
|
$workspace->members()->attach($owner->id, ['role' => Role::Admin->value]);
|
|
|
|
$viewer = User::factory()->create(['account_id' => $account->id]);
|
|
$workspace->members()->attach($viewer->id, ['role' => Role::Viewer->value]);
|
|
$viewer->update(['current_workspace_id' => $workspace->id]);
|
|
|
|
$clientId = mcpOauthClient('Viewer Agent');
|
|
DB::table('oauth_clients')->where('id', $clientId)->update([
|
|
'redirect_uris' => json_encode(['https://client.example/callback']),
|
|
]);
|
|
|
|
$this->actingAs($viewer)
|
|
->get(route('passport.authorizations.authorize', oauthAuthorizeQuery($clientId)))
|
|
->assertOk()
|
|
->assertInertia(fn ($page) => $page
|
|
->component('mcp/Authorize')
|
|
->where('client.name', 'Viewer Agent')
|
|
->where('user.email', $viewer->email)
|
|
->where('selectedWorkspaceId', (string) $workspace->id)
|
|
->has('workspaces', 1)
|
|
->where('workspaces.0.id', (string) $workspace->id)
|
|
->where('workspaces.0.name', 'Viewer Workspace')
|
|
->has('scopes', 1)
|
|
->where('scopes.0.id', 'mcp:use')
|
|
->has('authToken')
|
|
->where('state', 'test-state'));
|
|
|
|
expect(view()->exists('mcp.authorize-denied'))->toBeFalse()
|
|
->and(class_exists(EnsureCanAuthorizeMcp::class))->toBeFalse();
|
|
});
|
|
|
|
test('mcp oauth consent page uses the active locale', function () {
|
|
app()->setLocale('pt-BR');
|
|
|
|
expect(__('mcp.authorize.heading', ['client' => 'Claude']))->toBe('Autorizar Claude')
|
|
->and(__('mcp.authorize.logged_in_as'))->toBe('Conectado como:')
|
|
->and(__('mcp.authorize.workspace_scope'))->toBe('Esta conexão terá acesso somente ao workspace selecionado.')
|
|
->and(__('mcp.authorize.approve'))->toBe('Autorizar')
|
|
->and(__('mcp.authorize.cancel'))->toBe('Cancelar');
|
|
});
|
|
|
|
test('mcp oauth consent page lists every workspace the user can access', function () {
|
|
$account = Account::factory()->create();
|
|
$user = User::factory()->create(['account_id' => $account->id]);
|
|
$account->update(['owner_id' => $user->id]);
|
|
|
|
$alpha = Workspace::factory()->create([
|
|
'account_id' => $account->id,
|
|
'user_id' => $user->id,
|
|
'name' => 'Alpha',
|
|
]);
|
|
$beta = Workspace::factory()->create([
|
|
'account_id' => $account->id,
|
|
'user_id' => $user->id,
|
|
'name' => 'Beta',
|
|
]);
|
|
$alpha->members()->attach($user->id, ['role' => Role::Admin->value]);
|
|
$beta->members()->attach($user->id, ['role' => Role::Admin->value]);
|
|
$user->update(['current_workspace_id' => $alpha->id]);
|
|
|
|
$clientId = mcpOauthClient('Claude');
|
|
DB::table('oauth_clients')->where('id', $clientId)->update([
|
|
'redirect_uris' => json_encode(['https://client.example/callback']),
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('passport.authorizations.authorize', oauthAuthorizeQuery($clientId)))
|
|
->assertOk()
|
|
->assertInertia(fn ($page) => $page
|
|
->component('mcp/Authorize')
|
|
->where('selectedWorkspaceId', (string) $alpha->id)
|
|
->has('workspaces', 2)
|
|
->where('workspaces.0.name', 'Alpha')
|
|
->where('workspaces.1.name', 'Beta')
|
|
->where('workspaces.0.id', (string) $alpha->id)
|
|
->where('workspaces.1.id', (string) $beta->id));
|
|
});
|
|
|
|
test('mcp oauth always shows consent even when scopes were previously granted', function () {
|
|
$account = Account::factory()->create();
|
|
$user = User::factory()->create(['account_id' => $account->id]);
|
|
$account->update(['owner_id' => $user->id]);
|
|
$workspace = Workspace::factory()->create([
|
|
'account_id' => $account->id,
|
|
'user_id' => $user->id,
|
|
]);
|
|
$workspace->members()->attach($user->id, ['role' => Role::Admin->value]);
|
|
$user->update(['current_workspace_id' => $workspace->id]);
|
|
|
|
$clientId = mcpOauthClient('Reconnect Agent');
|
|
DB::table('oauth_clients')->where('id', $clientId)->update([
|
|
'redirect_uris' => json_encode(['https://client.example/callback']),
|
|
]);
|
|
mcpAccessToken($user, $clientId, $workspace);
|
|
|
|
$query = oauthAuthorizeQuery($clientId);
|
|
unset($query['prompt']);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('passport.authorizations.authorize', $query))
|
|
->assertOk()
|
|
->assertInertia(fn ($page) => $page
|
|
->component('mcp/Authorize')
|
|
->where('client.name', 'Reconnect Agent')
|
|
->where('selectedWorkspaceId', (string) $workspace->id));
|
|
});
|
|
|
|
test('passport approve route has no mcp create-post role gate', function () {
|
|
$route = app('router')->getRoutes()->getByName('passport.authorizations.approve');
|
|
|
|
expect($route)->not->toBeNull();
|
|
|
|
$middleware = collect($route->gatherMiddleware())
|
|
->map(fn (mixed $middleware): string => is_string($middleware) ? $middleware : $middleware::class)
|
|
->implode(',');
|
|
|
|
expect($middleware)->not->toContain('EnsureCanAuthorizeMcp');
|
|
});
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
function oauthAuthorizeQuery(string $clientId, string $redirectUri = 'https://client.example/callback'): array
|
|
{
|
|
$verifier = Str::random(64);
|
|
$challenge = rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=');
|
|
|
|
return [
|
|
'client_id' => $clientId,
|
|
'redirect_uri' => $redirectUri,
|
|
'response_type' => 'code',
|
|
'scope' => 'mcp:use',
|
|
'state' => 'test-state',
|
|
'code_challenge' => $challenge,
|
|
'code_challenge_method' => 'S256',
|
|
'prompt' => 'consent',
|
|
];
|
|
}
|