Class: HaveAPI::Authentication::Basic::Provider

Inherits:
HaveAPI::Authentication::Base show all
Defined in:
lib/haveapi/authentication/basic/provider.rb

Overview

HTTP basic authentication provider.

Example usage:

class MyBasicAuth < HaveAPI::Authentication::Basic::Provider
  protected
  def find_user(request, username, password)
    ::User.find_by(login: username, password: password)
  end
end

Finally put the provider in the authentication chain:

api = HaveAPI.new(...)
...
api.auth_chain << MyBasicAuth

Instance Attribute Summary

Attributes inherited from HaveAPI::Authentication::Base

#name

Instance Method Summary collapse

Methods inherited from HaveAPI::Authentication::Base

auth_method, inherited, #initialize, #register_routes, #resource_module

Constructor Details

This class inherits a constructor from HaveAPI::Authentication::Base

Instance Method Details

#authenticate(request) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/haveapi/authentication/basic/provider.rb', line 22

def authenticate(request)
  user = nil

  auth = Rack::Auth::Basic::Request.new(request.env)
  if auth.provided? && auth.basic? && auth.credentials
    begin
      user = find_user(request, *auth.credentials)
    rescue HaveAPI::AuthenticationError
      user = nil
    end
  end

  user
end

#describeObject



37
38
39
40
41
42
# File 'lib/haveapi/authentication/basic/provider.rb', line 37

def describe
  {
    description: 'Authentication using HTTP basic. Username and password is passed ' \
                 'via HTTP header. Its use is forbidden from web browsers.'
  }
end