Sha256: d754dfbe87a54e62e70e8148d1c5a96d61212d75a7acfc81f7b267f7b8d4ea5a

Contents?: true

Size: 857 Bytes

Versions: 9

Compression:

Stored size: 857 Bytes

Contents

class LHC::Auth < LHC::Interceptor

  def before_request
    options = request.options[:auth] || {}
    authenticate!(request, options)
  end

  private

  def authenticate!(request, options)
    if options[:bearer]
      bearer_authentication!(request, options)
    elsif options[:basic]
      basic_authentication!(request, options)
    end
  end

  def basic_authentication!(request, options)
    auth = options[:basic]
    credentials = "#{auth[:username]}:#{auth[:password]}"
    set_authorization_header request, "Basic #{Base64.encode64(credentials).chomp}"
  end

  def bearer_authentication!(request, options)
    token = options[:bearer]
    token = token.call if token.is_a?(Proc)
    set_authorization_header request, "Bearer #{token}"
  end

  def set_authorization_header(request, value)
    request.headers['Authorization'] = value
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lhc-9.2.0 lib/lhc/interceptors/auth.rb
lhc-9.1.2 lib/lhc/interceptors/auth.rb
lhc-9.1.2.pre lib/lhc/interceptors/auth.rb
lhc-9.1.1 lib/lhc/interceptors/auth.rb
lhc-8.1.1 lib/lhc/interceptors/auth.rb
lhc-9.1.0 lib/lhc/interceptors/auth.rb
lhc-9.0.0 lib/lhc/interceptors/auth.rb
lhc-8.1.0 lib/lhc/interceptors/auth.rb
lhc-8.0.0 lib/lhc/interceptors/auth.rb