Class: VectorMCP::Security::SessionContext
- Inherits:
-
Object
- Object
- VectorMCP::Security::SessionContext
- Defined in:
- lib/vector_mcp/security/session_context.rb
Overview
Represents the security context for a user session Contains authentication and authorization information
Instance Attribute Summary collapse
-
#auth_strategy ⇒ Object
readonly
Returns the value of attribute auth_strategy.
-
#authenticated ⇒ Object
readonly
Returns the value of attribute authenticated.
-
#authenticated_at ⇒ Object
readonly
Returns the value of attribute authenticated_at.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.anonymous ⇒ SessionContext
Create an anonymous (unauthenticated) session context.
-
.from_auth_result(auth_result) ⇒ SessionContext
Create an authenticated session context from auth result.
Instance Method Summary collapse
-
#add_permission(permission) ⇒ Object
Add a permission to the session.
-
#add_permissions(permissions) ⇒ Object
Add multiple permissions to the session.
-
#auth_method ⇒ String
Get authentication method used.
-
#auth_recent?(max_age: 3600) ⇒ Boolean
Check if authentication is recent (within specified seconds).
-
#authenticated? ⇒ Boolean
Check if the session is authenticated.
-
#can?(permission) ⇒ Boolean
Check if the user has a specific permission.
-
#can_access?(action, resource) ⇒ Boolean
Check if the user can perform an action on a resource.
-
#clear_permissions ⇒ Object
Clear all permissions.
-
#initialize(user: nil, authenticated: false, auth_strategy: nil, authenticated_at: nil) ⇒ SessionContext
constructor
Initialize session context.
-
#remove_permission(permission) ⇒ Object
Remove a permission from the session.
-
#to_h ⇒ Hash
Convert to hash for serialization.
-
#user_identifier ⇒ String
Get user identifier for logging/auditing.
Constructor Details
#initialize(user: nil, authenticated: false, auth_strategy: nil, authenticated_at: nil) ⇒ SessionContext
Initialize session context
15 16 17 18 19 20 21 |
# File 'lib/vector_mcp/security/session_context.rb', line 15 def initialize(user: nil, authenticated: false, auth_strategy: nil, authenticated_at: nil) @user = user @authenticated = authenticated @auth_strategy = auth_strategy @authenticated_at = authenticated_at || Time.now @permissions = Set.new end |
Instance Attribute Details
#auth_strategy ⇒ Object (readonly)
Returns the value of attribute auth_strategy.
8 9 10 |
# File 'lib/vector_mcp/security/session_context.rb', line 8 def auth_strategy @auth_strategy end |
#authenticated ⇒ Object (readonly)
Returns the value of attribute authenticated.
8 9 10 |
# File 'lib/vector_mcp/security/session_context.rb', line 8 def authenticated @authenticated end |
#authenticated_at ⇒ Object (readonly)
Returns the value of attribute authenticated_at.
8 9 10 |
# File 'lib/vector_mcp/security/session_context.rb', line 8 def authenticated_at @authenticated_at end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
8 9 10 |
# File 'lib/vector_mcp/security/session_context.rb', line 8 def @permissions end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
8 9 10 |
# File 'lib/vector_mcp/security/session_context.rb', line 8 def user @user end |
Class Method Details
.anonymous ⇒ SessionContext
Create an anonymous (unauthenticated) session context
112 113 114 |
# File 'lib/vector_mcp/security/session_context.rb', line 112 def self.anonymous new(authenticated: false) end |
.from_auth_result(auth_result) ⇒ SessionContext
Create an authenticated session context from auth result
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/vector_mcp/security/session_context.rb', line 119 def self.from_auth_result(auth_result) return anonymous unless auth_result&.dig(:authenticated) user_data = auth_result[:user] # Handle special marker for authenticated nil user if user_data == :authenticated_nil_user new( user: nil, authenticated: true, auth_strategy: "custom", authenticated_at: Time.now ) else # Extract strategy and authenticated_at only if user_data is a Hash strategy = user_data.is_a?(Hash) ? user_data[:strategy] : nil auth_time = user_data.is_a?(Hash) ? user_data[:authenticated_at] : nil new( user: user_data, authenticated: true, auth_strategy: strategy, authenticated_at: auth_time ) end end |
Instance Method Details
#add_permission(permission) ⇒ Object
Add a permission to the session
46 47 48 |
# File 'lib/vector_mcp/security/session_context.rb', line 46 def () @permissions << .to_s end |
#add_permissions(permissions) ⇒ Object
Add multiple permissions to the session
52 53 54 |
# File 'lib/vector_mcp/security/session_context.rb', line 52 def () .each { |perm| (perm) } end |
#auth_method ⇒ String
Get authentication method used
85 86 87 |
# File 'lib/vector_mcp/security/session_context.rb', line 85 def auth_method @auth_strategy || "none" end |
#auth_recent?(max_age: 3600) ⇒ Boolean
Check if authentication is recent (within specified seconds)
92 93 94 95 96 |
# File 'lib/vector_mcp/security/session_context.rb', line 92 def auth_recent?(max_age: 3600) return false unless authenticated? (Time.now - @authenticated_at) <= max_age end |
#authenticated? ⇒ Boolean
Check if the session is authenticated
25 26 27 |
# File 'lib/vector_mcp/security/session_context.rb', line 25 def authenticated? @authenticated end |
#can?(permission) ⇒ Boolean
Check if the user has a specific permission
32 33 34 |
# File 'lib/vector_mcp/security/session_context.rb', line 32 def can?() @permissions.include?(.to_s) end |
#can_access?(action, resource) ⇒ Boolean
Check if the user can perform an action on a resource
40 41 42 |
# File 'lib/vector_mcp/security/session_context.rb', line 40 def can_access?(action, resource) can?("#{action}:#{resource}") || can?("#{action}:*") || can?("*:#{resource}") || can?("*:*") end |
#clear_permissions ⇒ Object
Clear all permissions
63 64 65 |
# File 'lib/vector_mcp/security/session_context.rb', line 63 def @permissions.clear end |
#remove_permission(permission) ⇒ Object
Remove a permission from the session
58 59 60 |
# File 'lib/vector_mcp/security/session_context.rb', line 58 def () @permissions.delete(.to_s) end |
#to_h ⇒ Hash
Convert to hash for serialization
100 101 102 103 104 105 106 107 108 |
# File 'lib/vector_mcp/security/session_context.rb', line 100 def to_h { authenticated: @authenticated, user_identifier: user_identifier, auth_strategy: @auth_strategy, authenticated_at: @authenticated_at&.iso8601, permissions: @permissions.to_a } end |
#user_identifier ⇒ String
Get user identifier for logging/auditing
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vector_mcp/security/session_context.rb', line 69 def user_identifier return "anonymous" unless authenticated? return "anonymous" if @user.nil? case @user when Hash @user[:user_id] || @user[:sub] || @user[:email] || @user[:api_key] || "authenticated_user" when String @user else @user.respond_to?(:id) ? @user.id.to_s : "authenticated_user" end end |