Class: Prescient::MCP::Authentication::BearerToken

Inherits:
Object
  • Object
show all
Defined in:
lib/prescient/mcp/authentication.rb

Overview

Authenticates requests with a configured bearer token.

Instance Method Summary collapse

Constructor Details

#initialize(token:, principal: nil) ⇒ BearerToken

Returns a new instance of BearerToken.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
# File 'lib/prescient/mcp/authentication.rb', line 11

def initialize(token:, principal: nil)
  raise ArgumentError, "token must be a non-empty string" unless token.is_a?(String) && !token.empty?

  @token = token
  @principal = principal
end

Instance Method Details

#call(env) ⇒ Object, false

Authenticate a Rack environment without exposing the token.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Object, false)

    Principal on success or false on failure



21
22
23
24
25
26
27
# File 'lib/prescient/mcp/authentication.rb', line 21

def call(env)
  value = env["HTTP_AUTHORIZATION"].to_s
  scheme, candidate = value.split(" ", 2)
  return false unless scheme&.casecmp("Bearer")&.zero? && secure_equal?(candidate.to_s, @token)

  @principal || { type: "bearer" }
end