Class: DecisionAgent::Auth::Authenticator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_store: nil, session_manager: nil, password_reset_manager: nil) ⇒ Authenticator



8
9
10
11
12
# File 'lib/decision_agent/auth/authenticator.rb', line 8

def initialize(user_store: nil, session_manager: nil, password_reset_manager: nil)
  @user_store = user_store || InMemoryUserStore.new
  @session_manager = session_manager || SessionManager.new
  @password_reset_manager = password_reset_manager || PasswordResetManager.new
end

Instance Attribute Details

#password_reset_managerObject (readonly)

Returns the value of attribute password_reset_manager.



6
7
8
# File 'lib/decision_agent/auth/authenticator.rb', line 6

def password_reset_manager
  @password_reset_manager
end

#session_managerObject (readonly)

Returns the value of attribute session_manager.



6
7
8
# File 'lib/decision_agent/auth/authenticator.rb', line 6

def session_manager
  @session_manager
end

#user_storeObject (readonly)

Returns the value of attribute user_store.



6
7
8
# File 'lib/decision_agent/auth/authenticator.rb', line 6

def user_store
  @user_store
end

Instance Method Details

#authenticate(token) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/decision_agent/auth/authenticator.rb', line 27

def authenticate(token)
  session = @session_manager.get_session(token)
  return nil unless session

  user = @user_store.find_by_id(session.user_id)
  return nil unless user
  return nil unless user.active

  { user: user, session: session }
end

#create_user(email:, password:, roles: []) ⇒ Object



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

def create_user(email:, password:, roles: [])
  user = User.new(email: email, password: password, roles: roles)
  @user_store.save(user)
  user
end

#find_user(user_id) ⇒ Object



44
45
46
# File 'lib/decision_agent/auth/authenticator.rb', line 44

def find_user(user_id)
  @user_store.find_by_id(user_id)
end

#find_user_by_email(email) ⇒ Object



48
49
50
# File 'lib/decision_agent/auth/authenticator.rb', line 48

def find_user_by_email(email)
  @user_store.find_by_email(email)
end

#login(email, password) ⇒ Object



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

def (email, password)
  user = @user_store.find_by_email(email)
  return nil unless user
  return nil unless user.active
  return nil unless user.authenticate(password)

  @session_manager.create_session(user.id)
end

#logout(token) ⇒ Object



23
24
25
# File 'lib/decision_agent/auth/authenticator.rb', line 23

def logout(token)
  @session_manager.delete_session(token)
end

#request_password_reset(email) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/decision_agent/auth/authenticator.rb', line 52

def request_password_reset(email)
  user = @user_store.find_by_email(email)
  return nil unless user
  return nil unless user.active

  # Delete any existing reset tokens for this user
  @password_reset_manager.delete_user_tokens(user.id)

  # Create a new reset token (expires in 1 hour)
  @password_reset_manager.create_token(user.id, expires_in: 3600)
end

#reset_password(token_string, new_password) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/decision_agent/auth/authenticator.rb', line 64

def reset_password(token_string, new_password)
  token = @password_reset_manager.get_token(token_string)
  return nil unless token

  user = @user_store.find_by_id(token.user_id)
  return nil unless user
  return nil unless user.active

  # Update the password
  user.update_password(new_password)
  @user_store.save(user)

  # Delete the used token and all other tokens for this user
  @password_reset_manager.delete_user_tokens(user.id)

  # Invalidate all existing sessions for security
  @session_manager.delete_user_sessions(user.id)

  user
end