Lint / PHP (phpcs PSR-12) (push) Successful in 32s
Lint / JS (eslint) (push) Successful in 10s
Lint / PHP requirements (version + extensions) (push) Successful in 22s
Lint / Notify on failure (push) Skipped
Security / PHP Security (semgrep) (push) Successful in 1m4s
Lint / Deploy (push) Successful in 2s
MCP requests now run as the signed-in Tinker Tickets user, under exactly
the same rules as the web login, and expose the first two tools:
search_tickets and get_ticket.
Identity:
- IdentityMiddleware maps the validated token to a user with the same
checks as AuthMiddleware: the admin/employee group rule, now extracted
into helpers/AccessPolicy.php so both entry points share one copy, then
UserModel::syncUserFromAuthelia(), which creates/updates the row and
derives is_admin from groups.
- Claims are read from the validated token's server-side PSR-7 request
attributes, not from JSON-RPC _meta. The SDK's OAuthRequestMetaMiddleware
is deliberately not used: it array_merges into client-writable _meta,
so only the keys the validator happens to set are overwritten and a
client could inject others.
Scopes (ToolScopeMiddleware), enforced before dispatch:
- lifecycle messages need only a valid token; write tools (listed in
ToolCatalog, the single registry) need tickets:write; everything else
needs tickets:read, which tickets:write implies.
- Denials are the spec's step-up challenge: 403 +
WWW-Authenticate: Bearer error="insufficient_scope", scope=...,
resource_metadata=...
Tools (read-only, annotated readOnlyHint):
- search_tickets: text/status/priority/category/assignee ("me",
"unassigned", or a username), paginated, via TicketModel::getAllTickets
with the user's visibility filter. Defaults to every non-Closed status.
- get_ticket: details + comments, gated by canUserAccessTicket. A missing
ticket and a non-visible one return the same "not found".
Verified locally against a real MariaDB fixture (public, confidential,
internal+group, and closed tickets across two users), driving the real
pipeline (ToolCatalog, both middlewares, SDK transport) with only JWT
validation stubbed: 20/20 checks pass, including visibility parity per
user, confidential tickets hidden from non-owners, the group check
rejecting a user without admin/employee, a missing preferred_username
rejected, 403 insufficient_scope for a token without tickets:*, and
write implying read. Also exercised the stateless 2026-07-28 era (no
session, _meta + MCP-Protocol-Version/Mcp-Method/Mcp-Name headers), which
returns the same visibility-filtered results.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MGDKHiU5RJdo3dqQUDow3X
79 lines
2.9 KiB
PHP
79 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace TinkerTickets\Mcp\Auth;
|
|
|
|
use Psr\Http\Message\ResponseFactoryInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\StreamFactoryInterface;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
/**
|
|
* Maps a validated access token to a Tinker Tickets user, applying exactly
|
|
* the same rules as the web login (AuthMiddleware): the shared
|
|
* AccessPolicy::hasAppAccess() group check, then
|
|
* UserModel::syncUserFromAuthelia() to create/update the user row and derive
|
|
* is_admin from groups.
|
|
*
|
|
* Must run after the SDK's AuthorizationMiddleware, which has already
|
|
* verified the token's signature, issuer, audience and expiry and attached
|
|
* its claims as `oauth.claims` / `oauth.scopes` request attributes.
|
|
*/
|
|
final class IdentityMiddleware implements MiddlewareInterface
|
|
{
|
|
public function __construct(
|
|
private readonly \mysqli $conn,
|
|
private readonly ResponseFactoryInterface $responseFactory,
|
|
private readonly StreamFactoryInterface $streamFactory,
|
|
) {
|
|
}
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
$claims = $request->getAttribute('oauth.claims');
|
|
$scopes = $request->getAttribute('oauth.scopes') ?? [];
|
|
if (!is_array($claims)) {
|
|
// Only reachable if the stack is misordered; never serve anonymously.
|
|
return $this->deny(500, 'Identity unavailable');
|
|
}
|
|
|
|
$username = is_string($claims['preferred_username'] ?? null) ? trim($claims['preferred_username']) : '';
|
|
if ($username === '') {
|
|
return $this->deny(403, 'Access token has no preferred_username claim');
|
|
}
|
|
|
|
$groupsClaim = $claims['groups'] ?? [];
|
|
$groups = implode(',', array_filter(
|
|
is_array($groupsClaim) ? $groupsClaim : [$groupsClaim],
|
|
'is_string'
|
|
));
|
|
|
|
if (!\AccessPolicy::hasAppAccess($groups)) {
|
|
return $this->deny(403, 'Your account is not permitted to use Tinker Tickets');
|
|
}
|
|
|
|
$user = (new \UserModel($this->conn))->syncUserFromAuthelia(
|
|
$username,
|
|
is_string($claims['name'] ?? null) ? $claims['name'] : '',
|
|
is_string($claims['email'] ?? null) ? $claims['email'] : '',
|
|
$groups
|
|
);
|
|
|
|
McpIdentity::set($user, array_values(array_filter($scopes, 'is_string')));
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
|
|
private function deny(int $status, string $message): ResponseInterface
|
|
{
|
|
return $this->responseFactory->createResponse($status)
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withBody($this->streamFactory->createStream(json_encode([
|
|
'jsonrpc' => '2.0',
|
|
'id' => null,
|
|
'error' => ['code' => -32001, 'message' => $message],
|
|
])));
|
|
}
|
|
}
|