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



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

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.



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

def user_model
  @user_model
end

Instance Method Details

#authenticateObject

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



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

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

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

      user.failed_authentication!
      user = nil
    end
  end

  nil
end

#password?Boolean

Returns:

  • (Boolean)


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

def password?
  !!@password
end

#persist?Boolean

Returns:

  • (Boolean)


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

def persist?
  true
end

#usernameObject



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

def username
  @username
end

#username?Boolean

Returns:

  • (Boolean)


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

def username?
  !!@username
end