Class: PrestoHTTP::HTMLAuth

Inherits:
Object
  • Object
show all
Includes:
PrestoCore::Utils, Fields
Defined in:
lib/presto/http/auth/html.rb

Defined Under Namespace

Modules: Fields

Constant Summary

POOL =
::PrestoCache::Memory.new
ENCRYPT_ALGORITHM =
'MD5'.freeze

Constants included from Fields

Fields::FORM_ID, Fields::PASSWORD_FIELD, Fields::SUBMIT_FIELD, Fields::USERNAME_FIELD

Constants included from PrestoCore::Utils

PrestoCore::Utils::PATH_MODIFIERS, PrestoCore::Utils::STATUS__NOT_FOUND, PrestoCore::Utils::STATUS__OK, PrestoCore::Utils::STATUS__PERMANENT_REDIRECT, PrestoCore::Utils::STATUS__REDIRECT, PrestoCore::Utils::STATUS__RESTRICTED, PrestoCore::Utils::STATUS__SERVER_ERROR

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from PrestoCore::Utils

build_path, #extract_controllers, is_controller?, normalize_path, rootify_url

Constructor Details

- (HTMLAuth) initialize(controller_instance, env, setup = {})

A new instance of HTMLAuth



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/presto/http/auth/html.rb', line 23

def initialize controller_instance, env, setup = {}

  @controller, @controller_instance = controller_instance.class, controller_instance
  @env, @setup = env.dup, setup.dup

  @pool = (pool = @setup[:pool]) ?
      (pool == :session ? @controller_instance.http.session : pool) :
      POOL

  @encrypt_algorithm = (a = @setup[:crypt]) ? a.to_s.upcase : ENCRYPT_ALGORITHM

  @username_field = @setup[:username] || USERNAME_FIELD
  @password_field = @setup[:password] || PASSWORD_FIELD

  @params = ::Rack::Request.new(@env).POST
  @cookie = md5(@setup[:proc].to_s.split('@').last)
end

Instance Attribute Details

- (Object) user (readonly)

Returns the value of attribute user



17
18
19
# File 'lib/presto/http/auth/html.rb', line 17

def user
  @user
end

Class Method Details

+ (Object) authorization_form



19
20
21
# File 'lib/presto/http/auth/html.rb', line 19

def self.authorization_form
  ERB.new(File.read(File.expand_path('../authorization_form.erb', __FILE__))).result(binding)
end

Instance Method Details

- (Object) body



63
64
# File 'lib/presto/http/auth/html.rb', line 63

def body
end

- (Boolean) pass_validation?

this will always return true. you'll have to display page content based on `http.user` value: when it is nil, display authorization form. otherwise, display protected data.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/presto/http/auth/html.rb', line 49

def pass_validation?
  if provided?
    user, pass = *credentials
    return unless pass == @controller_instance.instance_exec(user, &@setup[:proc])
    @pool[seed] = [user, pass]
    return @user = user
  end
  user, pass = @pool[seed]
  if user && pass && pass == @controller_instance.instance_exec(user, &@setup[:proc])
    @user = user
  end
  true
end

- (Boolean) provided?

Returns:

  • (Boolean)


41
42
43
# File 'lib/presto/http/auth/html.rb', line 41

def provided?
  @params[@username_field] && @params[@password_field]
end

- (Object) reset



90
91
92
93
# File 'lib/presto/http/auth/html.rb', line 90

def reset
  @pool.delete seed
  @controller_instance.http.cookies.delete @cookie
end