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.

By default, the form is rendered at and the credentials are received on '/login'; this can be overridden in the configuration like so:

Aker.configure {
  rack_parameters :login_path => '/log-in-here'
}

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

Author:

  • David Yip

Direct Known Subclasses

CustomViewsMode

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.



41
42
43
44
# File 'lib/aker/form/mode.rb', line 41

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.

Returns:

  • (Symbol)


34
35
36
# File 'lib/aker/form/mode.rb', line 34

def self.key
  :form
end

.prepend_middleware(builder) ⇒ Object

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



49
50
51
# File 'lib/aker/form/mode.rb', line 49

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

Instance Method Details

#credentialsArray<String>

Extracts username and password from request parameters.

Returns:

  • (Array<String>)

    username and password, username (if password missing), or an empty array



66
67
68
# File 'lib/aker/form/mode.rb', line 66

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

#kindSymbol

The type of credentials supplied by this mode.

Returns:

  • (Symbol)


57
58
59
# File 'lib/aker/form/mode.rb', line 57

def kind
  :user
end

#login_urlString

The absolute URL for the login form.

Returns:

  • (String)


80
81
82
83
84
# File 'lib/aker/form/mode.rb', line 80

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

#on_ui_failureRack::Response

Builds a Rack response that redirects to the login form.

Returns:

  • (Rack::Response)


90
91
92
93
94
95
96
97
98
# File 'lib/aker/form/mode.rb', line 90

def on_ui_failure
  ::Rack::Response.new do |resp|
    target =  + '?url=' + escape(attempted_path)
    if env['aker.session_expired']
      target += '&session_expired=true'
    end
    resp.redirect(target)
  end
end

#valid?Boolean

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

Returns:

  • (Boolean)


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

def valid?
  credentials.length == 2
end