Class: AnswerLayer::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/answerlayer/authentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Authentication

Returns a new instance of Authentication.



5
6
7
# File 'lib/answerlayer/authentication.rb', line 5

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#apply(headers, mode: :api_key, subject: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/answerlayer/authentication.rb', line 9

def apply(headers, mode: :api_key, subject: false)
  @configuration.validate!(auth_mode: mode)
  authenticated = headers.dup

  case mode
  when :api_key, :oauth
    authenticated["X-API-Key"] = @configuration.api_key
  when :bearer
    authenticated["Authorization"] = "Bearer #{@configuration.bearer_token}"
  when :none
    authenticated
  else
    raise ConfigurationError, "unsupported auth mode: #{mode}"
  end

  apply_subject_headers(authenticated) if subject && mode != :bearer
  authenticated
end