Class: Uploadcare::Internal::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/uploadcare/internal/authenticator.rb

Overview

Handles authentication for Uploadcare REST API requests.

Supports two authentication modes:

  • Simple authentication: Basic auth with public_key:secret_key
  • Secure authentication: signature-based authentication

Examples:

Using the authenticator

authenticator = Uploadcare::Internal::Authenticator.new(config: config)
headers = authenticator.headers('GET', '/files/', '')

See Also:

Constant Summary collapse

BODY_DIGEST_NAME =
'MD5'
SIGNATURE_DIGEST_NAME =
'SHA1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Authenticator

Initialize a new Authenticator.

Parameters:



27
28
29
30
31
32
33
# File 'lib/uploadcare/internal/authenticator.rb', line 27

def initialize(config:)
  @config = config
  @default_headers = {
    'Accept' => 'application/vnd.uploadcare-v0.7+json',
    'User-Agent' => Uploadcare::Internal::UserAgent.call(config: config)
  }
end

Instance Attribute Details

#default_headersHash (readonly)

Returns Default headers included in all requests.

Returns:

  • (Hash)

    Default headers included in all requests



22
23
24
# File 'lib/uploadcare/internal/authenticator.rb', line 22

def default_headers
  @default_headers
end

Instance Method Details

#headers(http_method, uri, body = '', content_type = nil) ⇒ Hash

Generate authentication headers for an API request.

Parameters:

  • http_method (String)

    HTTP method (GET, POST, PUT, DELETE)

  • uri (String)

    Request URI path

  • body (String) (defaults to: '')

    Request body content (default: '')

  • content_type (String) (defaults to: nil)

    Content-Type header value (default: 'application/json')

Returns:

  • (Hash)

    Headers hash including authentication

Raises:



43
44
45
46
47
48
49
50
51
52
# File 'lib/uploadcare/internal/authenticator.rb', line 43

def headers(http_method, uri, body = '', content_type = nil)
  resolved_content_type = content_type || 'application/json'
  raise Uploadcare::Exception::AuthError, 'Secret Key is blank.' if @config.secret_key.to_s.empty?

  validate_public_key

  return simple_auth_headers(resolved_content_type) if @config.auth_type == 'Uploadcare.Simple'

  secure_auth_headers(http_method, uri, body, resolved_content_type)
end