Class: Challah::PasswordTechnique

Inherits:
Object
  • Object
show all
Defined in:
lib/challah/techniques/password_technique.rb

Overview

Allows authentication by username and password.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ PasswordTechnique

grab the params we want from this request



8
9
10
11
# File 'lib/challah/techniques/password_technique.rb', line 8

def initialize(session)
  @username   = session.username? ? session.username : nil
  @password   = session.password? ? session.password : nil
end

Instance Attribute Details

#user_modelObject

Returns the value of attribute user_model.



5
6
7
# File 'lib/challah/techniques/password_technique.rb', line 5

def user_model
  @user_model
end

Instance Method Details

#authenticateObject

if we can successfully authenticate, return a User instance, otherwise nil



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/challah/techniques/password_technique.rb', line 14

def authenticate
  if username? and password?
    user = user_model.find_for_session(username)

    if user
      if user.valid_session?
        if user.authenticate(@password)
          return user
        end
      end

      user.failed_authentication!
      user = nil
    end
  end

  nil
end

#password?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/challah/techniques/password_technique.rb', line 33

def password?
  !!@password
end

#persist?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/challah/techniques/password_technique.rb', line 37

def persist?
  true
end

#usernameObject



49
50
51
# File 'lib/challah/techniques/password_technique.rb', line 49

def username
  @username
end

#username?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/challah/techniques/password_technique.rb', line 45

def username?
  !!@username
end