Class: VectorMCP::Security::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/security/authorization.rb

Overview

Manages authorization policies for VectorMCP servers Provides fine-grained access control for tools and resources

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthorization

Returns a new instance of Authorization.



10
11
12
13
# File 'lib/vector_mcp/security/authorization.rb', line 10

def initialize
  @policies = {}
  @enabled = false
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



8
9
10
# File 'lib/vector_mcp/security/authorization.rb', line 8

def enabled
  @enabled
end

#policiesObject (readonly)

Returns the value of attribute policies.



8
9
10
# File 'lib/vector_mcp/security/authorization.rb', line 8

def policies
  @policies
end

Instance Method Details

#add_policy(resource_type, &block) ⇒ Object

Add an authorization policy for a resource type

Parameters:

  • resource_type (Symbol)

    the type of resource (e.g., :tool, :resource, :prompt)

  • block (Proc)

    the policy block that receives (user, action, resource)



28
29
30
# File 'lib/vector_mcp/security/authorization.rb', line 28

def add_policy(resource_type, &block)
  @policies[resource_type] = block
end

#authorize(user, action, resource) ⇒ Boolean

Check if a user is authorized to perform an action on a resource

Parameters:

  • user (Object)

    the authenticated user object

  • action (Symbol)

    the action being attempted (e.g., :call, :read, :list)

  • resource (Object)

    the resource being accessed

Returns:

  • (Boolean)

    true if authorized, false otherwise



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vector_mcp/security/authorization.rb', line 43

def authorize(user, action, resource)
  return true unless @enabled

  resource_type = determine_resource_type(resource)
  policy = @policies[resource_type]

  # If no policy is defined, allow access (opt-in authorization)
  return true unless policy

  begin
    policy_result = policy.call(user, action, resource)
    policy_result ? true : false
  rescue StandardError
    # Log error but deny access for safety
    false
  end
end

#disable!Object

Disable authorization (return to pass-through mode)



21
22
23
# File 'lib/vector_mcp/security/authorization.rb', line 21

def disable!
  @enabled = false
end

#enable!Object

Enable authorization system



16
17
18
# File 'lib/vector_mcp/security/authorization.rb', line 16

def enable!
  @enabled = true
end

#policy_typesArray<Symbol>

Get list of resource types with policies

Returns:

  • (Array<Symbol>)

    array of resource types



69
70
71
# File 'lib/vector_mcp/security/authorization.rb', line 69

def policy_types
  @policies.keys
end

#remove_policy(resource_type) ⇒ Object

Remove an authorization policy

Parameters:

  • resource_type (Symbol)

    the resource type to remove policy for



34
35
36
# File 'lib/vector_mcp/security/authorization.rb', line 34

def remove_policy(resource_type)
  @policies.delete(resource_type)
end

#required?Boolean

Check if authorization is required

Returns:

  • (Boolean)

    true if authorization is enabled



63
64
65
# File 'lib/vector_mcp/security/authorization.rb', line 63

def required?
  @enabled
end