class Gruf::Interceptors::Authentication::Basic

Handles basic authentication for gRPC requests

Public Instance Methods

call() { || ... } click to toggle source

Validate authentication

# File lib/gruf/interceptors/authentication/basic.rb, line 30
def call
  fail!(:unauthenticated, :unauthenticated) unless bypass? || valid?
  yield
end

Private Instance Methods

bypass?() click to toggle source

@return [Boolean] If this method is in the excluded list, bypass

# File lib/gruf/interceptors/authentication/basic.rb, line 40
def bypass?
  options.fetch(:excluded_methods, []).include?(request.method_name)
end
request_credentials() click to toggle source

@return [String] The decoded request credentials

# File lib/gruf/interceptors/authentication/basic.rb, line 69
def request_credentials
  credentials = if request.active_call.respond_to?(:metadata)
                  request.active_call.metadata['authorization'].to_s
                else
                  ''
                end
  Base64.decode64(credentials.to_s.gsub('Basic ', '').strip)
end
request_password() click to toggle source

@return [String] The decoded request password

# File lib/gruf/interceptors/authentication/basic.rb, line 81
def request_password
  @request_password ||= request_credentials.split(':').last
end
server_credentials() click to toggle source

@return [Array<Hash>] An array of valid server credentials for this service

# File lib/gruf/interceptors/authentication/basic.rb, line 62
def server_credentials
  options.fetch(:credentials, [])
end
valid?() click to toggle source

@return [Boolean] True if the basic authentication was valid

# File lib/gruf/interceptors/authentication/basic.rb, line 47
def valid?
  server_credentials.any? do |cred|
    username = cred.fetch(:username, '').to_s
    password = cred.fetch(:password, '').to_s
    if username.empty?
      request_password == password
    else
      request_credentials == "#{username}:#{password}"
    end
  end
end