Class: Aker::Form::Mode

Inherits:
Modes::Base
  • Object
show all
Includes:
Modes::Support::AttemptedPath, Rack::Utils
Defined in:
lib/aker/form/mode.rb

Overview

An interactive mode that accepts a username and password POSTed from an HTML form.

It expects the username in a username parameter and the unobfuscated password in a password parameter.

This mode also renders said HTML form if authentication fails. This is provided by Aker::Form::Middleware::LoginRenderer.

Author:

  • David Yip

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Modes::Support::AttemptedPath

#attempted_path

Methods inherited from Modes::Base

#authenticate!, #authority, #configuration, #interactive?, #store?

Methods included from Rack::EnvironmentHelper

#authority, #configuration, #interactive?

Class Method Details

.append_middleware(builder) ⇒ Object

Appends the login responder to its position in the Rack middleware stack.



46
47
48
49
# File 'lib/aker/form/mode.rb', line 46

def self.append_middleware(builder)
  builder.use(Middleware::LoginResponder, )
  builder.use(Middleware::LogoutResponder)
end

.keySymbol

A key that refers to this mode; used for configuration convenience.



26
27
28
# File 'lib/aker/form/mode.rb', line 26

def self.key
  :form
end

.login_pathString

The path at which the login form will be accessible. Currently hard-wired as /login.

This path is specified relative to the application's mount point. If you're looking for the absolute URL of the login form, you need to use login_url.



39
40
41
# File 'lib/aker/form/mode.rb', line 39

def self.
  '/login'
end

.prepend_middleware(builder) ⇒ Object

Prepends the login form renderer to its position in the Rack middleware stack.



54
55
56
# File 'lib/aker/form/mode.rb', line 54

def self.prepend_middleware(builder)
  builder.use(Middleware::LoginRenderer, )
end

Instance Method Details

#credentialsArray<String>

Extracts username and password from request parameters.



71
72
73
# File 'lib/aker/form/mode.rb', line 71

def credentials
  [request['username'], request['password']].compact
end

#kindSymbol

The type of credentials supplied by this mode.



62
63
64
# File 'lib/aker/form/mode.rb', line 62

def kind
  :user
end

#login_urlString

The absolute URL for the login form.



85
86
87
88
89
# File 'lib/aker/form/mode.rb', line 85

def 
  uri = URI.parse(request.url)
  uri.path = env['SCRIPT_NAME'] + self.class.
  uri.to_s
end

#on_ui_failureRack::Response

Builds a Rack response that redirects to the login form.



95
96
97
98
99
# File 'lib/aker/form/mode.rb', line 95

def on_ui_failure
  ::Rack::Response.new do |resp|
    resp.redirect( + '?url=' + escape(attempted_path))
  end
end

#valid?Boolean

Returns true if username and password are present, false otherwise.



77
78
79
# File 'lib/aker/form/mode.rb', line 77

def valid?
  credentials.length == 2
end