Class: SwaggerMCPTool::AuthHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_mcp_tool/auth_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(default_auth = {}) ⇒ AuthHandler

Initialize with optional default auth settings



6
7
8
9
10
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 6

def initialize(default_auth = {})
  @config = Config.instance
  @default_auth = default_auth || {}
  @auth_tokens = {}
end

Instance Method Details

#clear_auth_token(user_id) ⇒ Object

Clear auth token for a user



24
25
26
27
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 24

def clear_auth_token(user_id)
  @auth_tokens.delete(user_id.to_s)
  @config.logger.info "Auth token cleared for user: #{user_id}"
end

#get_auth_headers(user_id, api_key_header = nil) ⇒ Object

Get auth headers for a request



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 35

def get_auth_headers(user_id, api_key_header = nil)
  headers = {}

  token = get_auth_token(user_id)
  if token
    headers['Authorization'] = token.start_with?('Bearer ', 'Token ', 'Basic ') ? token : "Bearer #{token}"
  end

  # Add API key if provided
  headers[api_key_header] = @default_auth[:api_key] if api_key_header && @default_auth[:api_key]

  headers
end

#get_auth_token(user_id) ⇒ Object

Get auth token for a user



19
20
21
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 19

def get_auth_token(user_id)
  @auth_tokens[user_id.to_s] || @default_auth[:token] || @config.default_token
end

#has_auth_token?(user_id) ⇒ Boolean

Check if a user has an auth token

Returns:

  • (Boolean)


30
31
32
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 30

def has_auth_token?(user_id)
  @auth_tokens.key?(user_id.to_s) || @default_auth[:token].nil? == false || @config.default_token.nil? == false
end

#process_swagger_security(swagger_spec) ⇒ Object

Process auth from Swagger security definitions



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 50

def process_swagger_security(swagger_spec)
  return {} unless swagger_spec['securityDefinitions'] || swagger_spec['components']&.dig('securitySchemes')

  # Get security definitions based on Swagger/OpenAPI version
  security_defs = if swagger_spec['swagger'] == '2.0'
                    swagger_spec['securityDefinitions'] || {}
                  else
                    swagger_spec['components']&.dig('securitySchemes') || {}
                  end

  auth_config = {}

  security_defs.each_value do |definition|
    type = definition['type']&.downcase

    case type
    when 'apikey'
      auth_config[:api_key_name] = definition['name']
      auth_config[:api_key_in] = definition['in'] # 'header' or 'query'
    when 'oauth2', 'http'
      scheme = definition['scheme']&.downcase
      if scheme == 'bearer' || definition['bearerFormat'] || type == 'oauth2'
        auth_config[:token_type] = 'Bearer'
      elsif scheme == 'basic'
        auth_config[:token_type] = 'Basic'
      end
    end
  end

  auth_config
end

#set_auth_token(user_id, token) ⇒ Object

Set auth token for a user



13
14
15
16
# File 'lib/swagger_mcp_tool/auth_handler.rb', line 13

def set_auth_token(user_id, token)
  @auth_tokens[user_id.to_s] = token
  @config.logger.info "Auth token set for user: #{user_id}"
end