Module: Jsapi::Controller::Authentication::ClassMethods

Defined in:
lib/jsapi/controller/authentication/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#_api_authentication_handler(scheme_name) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
# File 'lib/jsapi/controller/authentication/class_methods.rb', line 49

def _api_authentication_handler(scheme_name) # :nodoc:
  scheme_name = scheme_name&.to_s

  _api_authentication_handlers[scheme_name] ||
    superclass.try(:_api_authentication_handler, scheme_name) ||
    (_api_authentication_handler(nil) unless scheme_name.nil?)
end

#api_after_authentication(method_or_proc = nil, **options, &block) ⇒ Object

:call-seq:

api_after_authentication(method_or_proc, **options)
api_after_authentication(**options, &block)

Registers a callback triggered after a request has been authenticated successfully.



13
14
15
# File 'lib/jsapi/controller/authentication/class_methods.rb', line 13

def api_after_authentication(method_or_proc = nil, **options, &block)
  _api_add_callback(:after_authentication, method_or_proc, **options, &block)
end

#api_authenticate(*scheme_names, with: nil, &block) ⇒ Object

:call-seq:

api_authenticate(*scheme_names, with:)
api_authenticate(*scheme_names, &block)

Registers a handler to authenticate requests according to the specified security schemes.

api_authenticate 'basic_auth', with: :authenticate

If no scheme names are specified, the handler is used as fallback for all security schemes for which no handler is registered.

The :with option specifies the method to be called to authenticate requests. Alternatively, a block can be given as handler.

If the handler returns a truthy value, the request is assumed to be authenticated successfully.

def authenticate(credentials)
  credentials.username == 'api_user' &&
    credentials.password == 'secret'
end

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/jsapi/controller/authentication/class_methods.rb', line 39

def api_authenticate(*scheme_names, with: nil, &block)
  handler = with || block
  raise ArgumentError, 'either the :with keyword argument or a block ' \
                       'must be specified' unless handler

  (scheme_names.presence || [nil]).each do |scheme_name|
    _api_authentication_handlers[scheme_name&.to_s] = handler
  end
end