Class: DecisionAgent::Auth::PasswordResetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/auth/password_reset_manager.rb

Instance Method Summary collapse

Constructor Details

#initializePasswordResetManager



6
7
8
9
10
11
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 6

def initialize
  @tokens = {}
  @mutex = Mutex.new
  @cleanup_interval = 300 # 5 minutes
  @last_cleanup = Time.now
end

Instance Method Details

#cleanup_expired_tokensObject



44
45
46
47
48
49
50
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 44

def cleanup_expired_tokens
  now = Time.now
  return if (now - @last_cleanup) < @cleanup_interval

  @tokens.delete_if { |_token_string, token| token.expired? }
  @last_cleanup = now
end

#countObject



52
53
54
55
56
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 52

def count
  @mutex.synchronize do
    @tokens.size
  end
end

#create_token(user_id, expires_in: 3600) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 13

def create_token(user_id, expires_in: 3600)
  token = PasswordResetToken.new(user_id: user_id, expires_in: expires_in)
  @mutex.synchronize do
    @tokens[token.token] = token
    cleanup_expired_tokens
  end
  token
end

#delete_token(token_string) ⇒ Object



32
33
34
35
36
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 32

def delete_token(token_string)
  @mutex.synchronize do
    @tokens.delete(token_string)
  end
end

#delete_user_tokens(user_id) ⇒ Object



38
39
40
41
42
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 38

def delete_user_tokens(user_id)
  @mutex.synchronize do
    @tokens.delete_if { |_token_string, token| token.user_id == user_id }
  end
end

#get_token(token_string) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/decision_agent/auth/password_reset_manager.rb', line 22

def get_token(token_string)
  @mutex.synchronize do
    token = @tokens[token_string]
    return nil unless token
    return nil if token.expired?

    token
  end
end