Class: OCI::BaseSigner

Inherits:
Object
  • Object
show all
Defined in:
lib/oci/base_signer.rb

Overview

The base class for other classes which are meant to generate a signature

Constant Summary collapse

SIGNING_STRATEGY_ENUM =

enum to define the signing strategy

[STANDARD = 'standard'.freeze, OBJECT_STORAGE = 'object_storage'.freeze].freeze
SIGNATURE_VERSION =

The Oracle Cloud Infrastructure API signature version

'1'.freeze
GENERIC_HEADERS =
%i[date (request-target) host].freeze
BODY_HEADERS =
%i[content-length content-type x-content-sha256].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key, private_key_content, pass_phrase: nil, signing_strategy: STANDARD, headers_to_sign_in_all_requests: GENERIC_HEADERS, body_headers_to_sign: BODY_HEADERS) ⇒ BaseSigner

Creates a BaseSigner

Parameters:

  • api_key (String)

    The API key needed when making calls. For token-based signing this should be ST$<token> but for calling as a user it will be tenancy/user/fingerprint

  • private_key_content (String)

    The private key as a PEM-formatted string

  • pass_phrase (String) (defaults to: nil)

    Optional the pass phrase for the private key (if any)

  • signing_strategy (SIGNING_STRATEGY_ENUM) (defaults to: STANDARD)

    Optional signing for standard service or object storage service

  • headers_to_sign_in_all_requests (Array<String>) (defaults to: GENERIC_HEADERS)

    Optional headers which should be signed on each request

  • body_headers_to_sign (Array<String>) (defaults to: BODY_HEADERS)

    Optional headers which should be signed on requests with bodies



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/oci/base_signer.rb', line 30

def initialize(
  api_key,
  private_key_content,
  pass_phrase: nil,
  signing_strategy: STANDARD,
  headers_to_sign_in_all_requests: GENERIC_HEADERS,
  body_headers_to_sign: BODY_HEADERS
)
  raise 'Missing required parameter api_key.' unless api_key
  raise 'Missing required parameter private_key_content.' unless private_key_content

  @key_id = api_key
  @private_key_content = private_key_content
  @pass_phrase = pass_phrase
  @signing_strategy = signing_strategy

  @headers_to_sign_all_requests = 
  @body_headers_to_sign = body_headers_to_sign
  @operation_header_mapping = {
    options: [],
    get: ,
    head: ,
    delete: ,
    put:  + body_headers_to_sign,
    post:  + body_headers_to_sign,
    patch:  + body_headers_to_sign
  }
end

Instance Method Details

#sign(method, uri, headers, body, operation_signing_strategy = :standard) ⇒ Object

Generates the correct signature and adds it to the headers that are passed in. Also injects any required headers that might be missing.

Parameters:

  • method (Symbol)

    The HTTP method, such as :get or :post.

  • uri (String)
  • headers (Hash)

    A hash of headers

  • body (String)

    The request body

  • operation_signing_strategy (String) (defaults to: :standard)

    the signing strategy for the operation. Default is :standard



68
69
70
71
72
73
74
75
# File 'lib/oci/base_signer.rb', line 68

def sign(method, uri, headers, body, operation_signing_strategy = :standard)
  method = method.to_sym.downcase
  uri = URI(uri)
  path = uri.query.nil? ? uri.path : "#{uri.path}?#{uri.query}"
  inject_missing_headers(method, headers, body, uri, operation_signing_strategy)
  signature = compute_signature(headers, method, path, operation_signing_strategy)
  inject_authorization_header(headers, method, signature, operation_signing_strategy) unless signature.nil?
end