Class: BSV::Auth::SessionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/auth/session_manager.rb

Overview

Thread-safe store for PeerSession objects.

Supports dual-index lookup: by session_nonce (primary) or by peer_identity_key (secondary). Multiple concurrent sessions per peer identity key are supported — the most recently updated session is returned when looking up by identity key.

Matches the ts-sdk SessionManager dual-index design.

Instance Method Summary collapse

Constructor Details

#initializeSessionManager

Returns a new instance of SessionManager.



14
15
16
17
18
19
20
# File 'lib/bsv/auth/session_manager.rb', line 14

def initialize
  # session_nonce -> PeerSession
  @by_nonce    = {}
  # peer_identity_key -> Set of session_nonces
  @by_identity = {}
  @mutex       = Mutex.new
end

Instance Method Details

#add_session(session) ⇒ Object

Adds a session to the manager.

Parameters:

Raises:

  • (ArgumentError)

    if session_nonce is blank



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bsv/auth/session_manager.rb', line 26

def add_session(session)
  raise ArgumentError, 'session_nonce is required' unless session.session_nonce.is_a?(String) && !session.session_nonce.empty?

  @mutex.synchronize do
    @by_nonce[session.session_nonce] = session

    if session.peer_identity_key.is_a?(String)
      nonces = @by_identity[session.peer_identity_key] ||= []
      nonces << session.session_nonce unless nonces.include?(session.session_nonce)
    end
  end
end

#get_session(identifier) ⇒ PeerSession?

Retrieves a session by session_nonce or peer_identity_key.

When the identifier is a session nonce, returns that exact session. When the identifier is a peer identity key, returns the most recently updated session for that peer.

Parameters:

  • identifier (String)

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bsv/auth/session_manager.rb', line 57

def get_session(identifier)
  @mutex.synchronize do
    direct = @by_nonce[identifier]
    return direct if direct

    nonces = @by_identity[identifier]
    return nil if nonces.nil? || nonces.empty?

    best = nil
    nonces.each do |nonce|
      s = @by_nonce[nonce]
      next if s.nil?

      best = s if best.nil? || (s.last_update || 0) > (best.last_update || 0)
    end
    best
  end
end

#remove_session(session) ⇒ Object

Removes a session from the manager.

Parameters:



79
80
81
# File 'lib/bsv/auth/session_manager.rb', line 79

def remove_session(session)
  @mutex.synchronize { remove_session_locked(session) }
end

#session?(identifier) ⇒ Boolean

Parameters:

  • identifier (String)

    session nonce or identity key

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/bsv/auth/session_manager.rb', line 85

def session?(identifier)
  @mutex.synchronize do
    return true if @by_nonce.key?(identifier)

    nonces = @by_identity[identifier]
    !nonces.nil? && !nonces.empty?
  end
end

#update_session(session) ⇒ Object

Updates an existing session (removes old references and re-adds).

Parameters:



42
43
44
45
46
47
# File 'lib/bsv/auth/session_manager.rb', line 42

def update_session(session)
  @mutex.synchronize do
    remove_session_locked(session)
    add_session_locked(session)
  end
end