Class: MCP::Server::OAuth::Authenticator
- Inherits:
-
Object
- Object
- MCP::Server::OAuth::Authenticator
- Defined in:
- lib/mcp/server/oauth/authenticator.rb
Overview
The verification core shared by Middleware and the streamable HTTP transport's built-in enforcement:
extracts the bearer token from a Rack env, runs it through the verifier, checks scopes, and maps each failure
to the RFC 6750 challenge response it deserves.
token_verifier- anything responding toverify(token); seeTokenVerifierfor the contract. Built-ins:JWTVerifier,IntrospectionVerifier.required_scopes- scopes the token must include, all of them; a shortfall produces 403insufficient_scope(step-up).resource_metadata- aProtectedResourceMetadata; its well-known URL andscopes_supportedfeed the challenges. Passresource_metadata_url:instead to point at a document served elsewhere.scope_matcher- optional callable(required_scope, granted_scopes) -> Booleanfor deployments whose scopes form a hierarchy; the MCP authorization specification requires a broader scope to satisfy the narrower scopes it implies, which exact membership (the default) cannot express. The matcher also rides on the returnedAccessToken, sorequire_scopes!andscope?inside handlers apply the same semantics.
Constant Summary collapse
- MAX_TOKEN_BYTES =
Upper bound on the presented token, applied before any verification so oversized garbage neither reaches cryptographic parsing nor gets forwarded to an introspection endpoint. Real-world access tokens stay far below this; web servers cap the whole header section around 16 KiB.
8192
Instance Method Summary collapse
- #authenticate(env) ⇒ AccessToken
-
#challenge_response(error) ⇒ Object
Maps an
OAuth::Errorraised byauthenticate(or by a handler'srequire_scopes!) to the Rack response carrying the matching challenge. -
#initialize(token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil) ⇒ Authenticator
constructor
A new instance of Authenticator.
Constructor Details
#initialize(token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil) ⇒ Authenticator
Returns a new instance of Authenticator.
36 37 38 39 40 41 42 |
# File 'lib/mcp/server/oauth/authenticator.rb', line 36 def initialize(token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil) @verifier = token_verifier @required_scopes = Array(required_scopes) @resource_metadata = @resource_metadata_url = || &.well_known_url @scope_matcher = scope_matcher end |
Instance Method Details
#authenticate(env) ⇒ AccessToken
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mcp/server/oauth/authenticator.rb', line 47 def authenticate(env) token = extract_bearer_token(env) access_token = @verifier.verify(token) raise InvalidTokenError if access_token.nil? access_token = attach_scope_matcher(access_token) check_scopes!(access_token) access_token end |
#challenge_response(error) ⇒ Object
Maps an OAuth::Error raised by authenticate (or by a handler's require_scopes!) to the Rack response carrying
the matching challenge. An Error outside the four built-in classes, such as one a custom verifier defines,
is a token rejection and answers 401 like an invalid token; anything else is a programming error.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mcp/server/oauth/authenticator.rb', line 61 def challenge_response(error) case error when MissingTokenError Challenge.missing_token_response(scope: scope_hint, resource_metadata: @resource_metadata_url) when InvalidRequestError Challenge.invalid_request_response(error_description: error., resource_metadata: @resource_metadata_url) when InsufficientScopeError Challenge.insufficient_scope_response( error_description: error., scope: scope_hint(error.required_scopes), resource_metadata: @resource_metadata_url, ) when Error Challenge.invalid_token_response( error_description: error., scope: scope_hint, resource_metadata: @resource_metadata_url, ) else raise ArgumentError, "unexpected error class: #{error.class}" end end |