Class: ApipieBindings::Authenticators::BasicAuthExternal

Inherits:
BasicAuth
  • Object
show all
Defined in:
lib/apipie_bindings/authenticators/basic_auth_external.rb

Instance Attribute Summary

Attributes inherited from Base

#auth_cookie

Instance Method Summary collapse

Methods inherited from Base

#error, #name, #response

Constructor Details

#initialize(user, password, authentication_url, auth_request_options = {}) ⇒ BasicAuthExternal

Returns a new instance of BasicAuthExternal.



6
7
8
9
10
# File 'lib/apipie_bindings/authenticators/basic_auth_external.rb', line 6

def initialize(user, password, authentication_url, auth_request_options = {})
  super(user, password)
  @authentication_url = authentication_url
  @auth_request_options = auth_request_options
end

Instance Method Details

#authenticate(original_request, _args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/apipie_bindings/authenticators/basic_auth_external.rb', line 12

def authenticate(original_request, _args)
  request = RestClient::Resource.new(
    @authentication_url,
    @auth_request_options.merge({ user: @user, password: @password })
  )
  request.get do |response, _, raw_response|
    if response.code == 401
      raise RestClient::Unauthorized.new(response), 'External authentication did not pass.'
    end

    cookie = raw_response['set-cookie'].split('; ')[0]
    @auth_cookie = cookie
    original_request['Cookie'] = cookie
  end

  original_request
end