Class: PadlockAuth::Config

Inherits:
Object
  • Object
show all
Extended by:
Option
Includes:
Mixins::BuildWith
Defined in:
lib/padlock_auth/config.rb,
lib/padlock_auth/config/option.rb,
lib/padlock_auth/config/scopes.rb

Overview

Configuration for PadlockAuth.

Examples:

PadlockAuth.configure do |config|
  config.secure_with :token do
    secret_key "my_secret_key"
  end

  config.default_scopes :read, :write
  config.access_token_methods :from_bearer_authorization, :from_access_token_param
  config.raise_on_errors!
end

Defined Under Namespace

Modules: Option Classes: Builder, Scopes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Option

extended, option

Instance Attribute Details

#access_token_methodsArray<Symbol> (readonly)

Methods to extract the access token from the request.

Returns:

  • (Array<Symbol>)

    Methods to extract the access token

See Also:

  • for available methods


166
167
168
169
170
171
172
# File 'lib/padlock_auth/config.rb', line 166

def access_token_methods
  @access_token_methods ||= i[
    from_bearer_authorization
    from_access_token_param
    from_bearer_param
  ]
end

#default_scopesPadlockAuth::Config::Scopes (readonly)

Default required scopes, used whenever ‘padlock_authorize!` is called without arguments.

Empty by default.

Returns:



154
155
156
# File 'lib/padlock_auth/config.rb', line 154

def default_scopes
  @default_scopes ||= PadlockAuth::Config::Scopes.new
end

#handle_auth_errorsSymbol (readonly)

How to handle authentication errors.

  • ‘:raise` - Raise an exception when authentication fails

  • ‘:render` - Render an error response when authentication fails

Returns:

  • (Symbol)

    The error handling method



190
# File 'lib/padlock_auth/config.rb', line 190

option :handle_auth_errors, default: :render

#realmString (readonly)

WWW-Authenticate Realm (default “PadlockAuth”).

Returns:

  • (String)

    The Authentication realm



136
# File 'lib/padlock_auth/config.rb', line 136

attr_reader :strategy

#strategyPadlockAuth::AbstractStrategy (readonly)

The strategy to use for authentication.

Returns:



136
137
138
# File 'lib/padlock_auth/config.rb', line 136

def strategy
  @strategy
end

Class Method Details

.build { ... } ⇒ PadlockAuth::Config

Builds the configuration instance using the builder.

Yields:

  • block to configure the configuration

Returns:

See Also:



126
# File 'lib/padlock_auth/config.rb', line 126

build_with Builder

Instance Method Details

#action_cable_methodsObject



174
175
176
177
178
179
# File 'lib/padlock_auth/config.rb', line 174

def action_cable_methods
  @action_cable_methods ||= i[
    from_access_token_param
    from_bearer_param
  ]
end

#raise_on_errors?Boolean

Returns Whether to raise an exception when authentication fails.

Returns:

  • (Boolean)

    Whether to raise an exception when authentication fails



200
201
202
# File 'lib/padlock_auth/config.rb', line 200

def raise_on_errors?
  handle_auth_errors == :raise
end

#render_on_errors?Boolean

Returns Whether to render an error response when authentication fails.

Returns:

  • (Boolean)

    Whether to render an error response when authentication fails



194
195
196
# File 'lib/padlock_auth/config.rb', line 194

def render_on_errors?
  handle_auth_errors == :render
end

#validate!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called by PadlockAuth::Utils::AbstractBuilder#build to validate the configuration.

Raises:

  • (ArgumentError)

    If the configuration is invalid



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/padlock_auth/config.rb', line 210

def validate!
  raise ArgumentError, "strategy has not been configured via secure_with" if strategy.nil?

  raise ArgumentError, "realm is required" if realm.blank?

  unless handle_auth_errors.in? i[raise render]
    raise ArgumentError, "handle_auth_errors must be :raise, or :render"
  end

  true
end