Class: RosettAi::Mcp::Middleware::Authentication

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

Overview

Rack middleware for API key authentication.

Supports multi-key keyfile or single env var modes. Returns 401 Unauthorized for invalid credentials.

Author:

  • hugo

  • claude

Instance Method Summary collapse

Constructor Details

#initialize(app, config: nil) ⇒ Authentication

Returns a new instance of Authentication.

Parameters:

  • app (#call)

    the next Rack application

  • config (#type, #key_source, #keyfile_path, #api_key_env) (defaults to: nil)

    auth config



19
20
21
22
23
24
25
26
# File 'lib/rosett_ai/mcp/middleware/authentication.rb', line 19

def initialize(app, config: nil)
  @app = app
  @config = config
  @keyfile = nil
  validate_auth_type
  load_keyfile if keyfile_mode?
  install_sighup_handler if keyfile_mode?
end

Instance Method Details

#call(env) ⇒ Array

Returns Rack response triplet.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    Rack response triplet



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rosett_ai/mcp/middleware/authentication.rb', line 30

def call(env)
  case auth_type
  when 'none'
    env['rosett_ai.authenticated'] = false
    @app.call(env)
  when 'api_key'
    authenticate_api_key(env)
  else
    unauthorized('Unsupported authentication type')
  end
end