* Add workspace MCP settings and token access controls. Ship MCP settings UI, OAuth revoke/list helpers, Passport deploy wiring, and workspace.token:mcp gating so assistants can connect without pulling in welcome/onboarding from the parent epic. Co-authored-by: Cursor <cursoragent@cursor.com> * Type MCP client config shapes instead of string checks. Encode http/config-root on each advanced client and tighten primary client ids so snippet generation does not branch on magic strings. Co-authored-by: Cursor <cursoragent@cursor.com> * Polish MCP settings follow-ups from review. Translate Ukrainian MCP copy, deep-link ChatGPT into connector creation, drop an unused asset and revoke arg, and assert PATs are rejected on the MCP endpoint. Co-authored-by: Cursor <cursoragent@cursor.com> * Harden MCP connected clients, revoke scope, and OAuth consent. List recoverable sessions with live refresh tokens, revoke only PATs, throttle registration alone, and block viewers from authorizing MCP. Co-authored-by: Cursor <cursoragent@cursor.com> * Simplify MCP OAuth route throttling to a single middleware group. Co-authored-by: Cursor <cursoragent@cursor.com> * Allow workspace viewers read-only MCP access with web policy writes. Mirror the web app: MCP connects on view + OAuth mcp:use, write tools enforce createPost/update/delete/manageAccounts/manageTeam, and demotion to Viewer keeps grants. Cover role denials, consent, and disconnect. Co-authored-by: Cursor <cursoragent@cursor.com> * Harden MCP tool authz with shared workspace helpers. Route ApiKey tools through AuthorizesMcpTool, fail closed on null user or policy argument, and resolve the current workspace before mutating. Co-authored-by: Cursor <cursoragent@cursor.com> * Drop redundant string casts on validated request data. Enum::from and validated() fields are already strings, so the casts add noise without changing behavior. Co-authored-by: Cursor <cursoragent@cursor.com> * Show only the current user's MCP connections in settings. Match API keys privacy: list and disconnect your own OAuth clients, not teammates' across the account. Co-authored-by: Cursor <cursoragent@cursor.com> * Cover LoadWorkspaceFromToken gaps and harden AuthorizesMcpTool tests. Co-authored-by: Cursor <cursoragent@cursor.com> * Drop redundant is_string guard before UpdatePostTool find. Co-authored-by: Cursor <cursoragent@cursor.com> * Refactor AppSidebar to always show MCP link and simplify route middleware definition in ai.php. The MCP link is now consistently displayed regardless of the current workspace state, and the route middleware syntax has been streamlined. * Refresh MCP connected clients with Inertia usePoll. Co-authored-by: Cursor <cursoragent@cursor.com> * Bump laravel/mcp to 0.9.1 and add the TryPost server icon. Requires laravel/boost 2.5 for the Icon attribute; expose images/trypost/icon.png on TryPostServer. Co-authored-by: Cursor <cursoragent@cursor.com> * Drop no-op ReflectionClass import in TryPostServerTest. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
240 lines
6.7 KiB
PHP
240 lines
6.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Laravel\Passport\Token;
|
|
|
|
class AccessToken extends Token
|
|
{
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'id',
|
|
'user_id',
|
|
'client_id',
|
|
'workspace_id',
|
|
'name',
|
|
'scopes',
|
|
'revoked',
|
|
'expires_at',
|
|
'last_used_at',
|
|
];
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'scopes' => 'json',
|
|
'revoked' => 'bool',
|
|
'expires_at' => 'datetime',
|
|
'last_used_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function workspace(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Workspace::class);
|
|
}
|
|
|
|
/**
|
|
* Passport resolves the user model via the OAuth client's provider, which
|
|
* breaks eager-loading `user` (the relation is built on an empty token with
|
|
* no client). Tokens in TryPost always belong to App\Models\User.
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Active OAuth grants used by MCP clients (excludes personal access API keys).
|
|
*
|
|
* @param Builder<static> $query
|
|
* @return Builder<static>
|
|
*/
|
|
public function scopeActiveMcpOAuth(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->mcpOAuth()
|
|
->where('revoked', false)
|
|
->where(function (Builder $expires): void {
|
|
$expires->whereNull('expires_at')
|
|
->orWhere('expires_at', '>', now());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* MCP OAuth grants that still represent a live or recoverable session
|
|
* (unexpired access token, or expired access with a live refresh token).
|
|
*
|
|
* @param Builder<static> $query
|
|
* @return Builder<static>
|
|
*/
|
|
public function scopeConnectedMcpOAuth(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->mcpOAuth()
|
|
->where('revoked', false)
|
|
->where(function (Builder $alive): void {
|
|
$alive
|
|
->where(function (Builder $expires): void {
|
|
$expires->whereNull('expires_at')
|
|
->orWhere('expires_at', '>', now());
|
|
})
|
|
->orWhereHas(
|
|
'refreshToken',
|
|
fn (Builder $refresh): Builder => $refresh
|
|
->where('revoked', false)
|
|
->where(function (Builder $refreshExpires): void {
|
|
$refreshExpires->whereNull('expires_at')
|
|
->orWhere('expires_at', '>', now());
|
|
}),
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param Builder<static> $query
|
|
* @return Builder<static>
|
|
*/
|
|
public function scopeMcpOAuth(Builder $query): Builder
|
|
{
|
|
return $query
|
|
->whereJsonContains('scopes', 'mcp:use')
|
|
->whereHas(
|
|
'client',
|
|
fn (Builder $client): Builder => $client
|
|
->where('revoked', false)
|
|
->whereJsonDoesntContain('grant_types', 'personal_access'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Personal-access API keys (REST), excluding MCP OAuth clients.
|
|
*
|
|
* @param Builder<static> $query
|
|
* @return Builder<static>
|
|
*/
|
|
public function scopePersonalAccessApiKey(Builder $query): Builder
|
|
{
|
|
return $query->whereHas(
|
|
'client',
|
|
fn (Builder $client): Builder => $client
|
|
->where('revoked', false)
|
|
->whereJsonContains('grant_types', 'personal_access'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Whether this token was issued by a live personal-access client (REST API keys).
|
|
*/
|
|
public function isPersonalAccessToken(): bool
|
|
{
|
|
$this->loadMissing('client');
|
|
|
|
return $this->client !== null
|
|
&& ! $this->client->revoked
|
|
&& $this->client->hasGrantType('personal_access');
|
|
}
|
|
|
|
/**
|
|
* Whether this is a non-revoked MCP OAuth grant with mcp:use (ignores expiry).
|
|
*/
|
|
public function isMcpOAuthGrant(): bool
|
|
{
|
|
$this->loadMissing('client');
|
|
|
|
if ($this->revoked) {
|
|
return false;
|
|
}
|
|
|
|
if (! in_array('mcp:use', $this->scopes ?? [], true)) {
|
|
return false;
|
|
}
|
|
|
|
return $this->client !== null
|
|
&& ! $this->client->revoked
|
|
&& ! $this->client->hasGrantType('personal_access');
|
|
}
|
|
|
|
/**
|
|
* Whether this is a non-revoked, unexpired MCP OAuth grant with mcp:use.
|
|
*/
|
|
public function isActiveMcpGrant(): bool
|
|
{
|
|
if (! $this->isMcpOAuthGrant()) {
|
|
return false;
|
|
}
|
|
|
|
return $this->expires_at === null || ! $this->expires_at->isPast();
|
|
}
|
|
|
|
/**
|
|
* Whether a refresh token can still mint a new access token for this grant.
|
|
*/
|
|
public function hasLiveRefreshToken(): bool
|
|
{
|
|
$this->loadMissing('refreshToken');
|
|
|
|
$refresh = $this->refreshToken;
|
|
|
|
if ($refresh === null || $refresh->revoked) {
|
|
return false;
|
|
}
|
|
|
|
return $refresh->expires_at === null || $refresh->expires_at->isFuture();
|
|
}
|
|
|
|
/**
|
|
* Whether this MCP grant can actually use the product (active token + a
|
|
* workspace the owner can view — write tools enforce createPost themselves).
|
|
*/
|
|
public function isUsableMcpGrant(?User $user = null, ?Workspace $workspace = null): bool
|
|
{
|
|
if (! $this->isActiveMcpGrant()) {
|
|
return false;
|
|
}
|
|
|
|
return $this->ownerCanViewWorkspace($user, $workspace);
|
|
}
|
|
|
|
/**
|
|
* Whether this MCP grant should appear in the connected-clients list
|
|
* (usable now, or recoverable via refresh, for a user who can view a workspace).
|
|
*/
|
|
public function isListedMcpConnection(?User $user = null, ?Workspace $workspace = null): bool
|
|
{
|
|
if (! $this->isMcpOAuthGrant()) {
|
|
return false;
|
|
}
|
|
|
|
if (! $this->isActiveMcpGrant() && ! $this->hasLiveRefreshToken()) {
|
|
return false;
|
|
}
|
|
|
|
return $this->ownerCanViewWorkspace($user, $workspace);
|
|
}
|
|
|
|
private function ownerCanViewWorkspace(?User $user = null, ?Workspace $workspace = null): bool
|
|
{
|
|
$user ??= User::query()
|
|
->with('currentWorkspace')
|
|
->find($this->user_id);
|
|
|
|
if (! $user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
$workspace ??= $this->workspace ?? $user->currentWorkspace;
|
|
|
|
return $workspace instanceof Workspace
|
|
&& $user->can('view', $workspace);
|
|
}
|
|
}
|