Module: EasyAuth

Defined in:
lib/etvnet_seek/easy_auth.rb

Constant Summary collapse

AUTHORIZED_USERS =

To generate a hashed password (in irb): require ‘easy_auth’ EasyAuth.hash(‘my_password’) # Put this in AUTHORIZED_USERS

{
  'patrick' => "4ded8fa58a5c16298e665b35353555c89b786d8"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hash_password(password) ⇒ Object



15
16
17
18
19
# File 'lib/etvnet_seek/easy_auth.rb', line 15

def self.hash_password(password)
  #Digest::SHA256.digest(password)

  Digest::SHA1.hexdigest(password)
end

Instance Method Details

#auth_digest(username, password) ⇒ Object



25
26
27
28
# File 'lib/etvnet_seek/easy_auth.rb', line 25

def auth_digest(username, password)
  key = "#{username}::#{hash_password(password)}::#{request.env['REMOTE_ADDR']}::#{request.env['HTTP_USER_AGENT']}"
  hash_password key
end

#authorized?(username, key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/etvnet_seek/easy_auth.rb', line 47

def authorized?(username, key)
  return false unless AUTHORIZED_USERS.has_key?(username)

  key == auth_digest(username, AUTHORIZED_USERS[username])
end

#hash_password(password) ⇒ Object



21
22
23
# File 'lib/etvnet_seek/easy_auth.rb', line 21

def hash_password(password)
  EasyAuth.hash_password(password)
end

#if_auth(username = request.cookies["username"], password = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/etvnet_seek/easy_auth.rb', line 30

def if_auth(username = request.cookies["username"], password = nil)
  if password.nil?
    key = request.cookies["key"]
  else
    key = auth_digest(username, hash_password(password))
  end

  if authorized?(username, key)
    response.set_cookie("username", username)
    response.set_cookie("key", key)
    yield
  else
    @error = "Login failed"
    erb :'/admin/login'
  end
end