Class: OmniAuth::Aleph::Adaptor

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth/aleph/adaptor.rb

Defined Under Namespace

Classes: AlephError

Constant Summary collapse

KEYS =

List of keys, all are required.

[:scheme, :host, :port, :library, :sub_library]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Adaptor

Returns a new instance of Adaptor.



21
22
23
24
25
26
27
28
# File 'lib/omniauth/aleph/adaptor.rb', line 21

def initialize(configuration={})
  self.class.validate(configuration)
  @configuration = configuration.dup
  @logger = @configuration.delete(:logger)
  KEYS.each do |key|
    instance_variable_set("@#{key}", @configuration[key])
  end
end

Class Method Details

.validate(configuration = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/omniauth/aleph/adaptor.rb', line 11

def self.validate(configuration={})
  message = []
  KEYS.each do |key|
    message << key if(configuration[key].nil?)
  end
  unless message.empty?
    raise ArgumentError.new(message.join(",") +" MUST be provided")
  end
end

Instance Method Details

#authenticate(username, password) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/omniauth/aleph/adaptor.rb', line 30

def authenticate(username, password)
  url = bor_auth_url + "&bor_id=#{username}&verification=#{password}"
  response = Faraday.get url
  # If we get a successful response AND we are looking at XML and we have a body
  if response.status == 200 && response.headers["content-type"] == 'text/xml' && response.body
    json = MultiXml.parse(response.body)
    if json["bor_auth"] && (error = json["bor_auth"]["error"]).nil?
      return json
    elsif json["bor_auth"].nil?
      raise AlephError.new("Aleph responded, but it's not a response I understand.")
    else
      raise AlephError.new(error)
    end
  else
    raise AlephError.new("Aleph response:\n\t #{@response.inspect}.")
  end
rescue Faraday::ConnectionFailed => e
  raise AlephError.new("Couldn't connect to Aleph.")
end