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
- #auth_digest(username, password) ⇒ Object
- #authorized?(username, key) ⇒ Boolean
- #hash_password(password) ⇒ Object
- #if_auth(username = request.cookies["username"], password = nil) ⇒ Object
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
47 48 49 50 51 |
# File 'lib/etvnet_seek/easy_auth.rb', line 47 def (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.["username"], password = nil) if password.nil? key = request.["key"] else key = auth_digest(username, hash_password(password)) end if (username, key) response.("username", username) response.("key", key) yield else @error = "Login failed" erb :'/admin/login' end end |