Sha256: 84789d4dab92bcc760d01eddebe6d8f4d964518ccb47ab72ab1a7bf30affee64

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require "uri"
require "securerandom"

module AkamaiCCU
  class Secret
    DIGEST = "EG1-HMAC-SHA256"
    ENTRY_REGEX = /(.+?)\s?=\s?(.+)/ 
    BODY_SIZE = 131072

    class FileContentError < ArgumentError; end

    def self.by_file(name = "~/.edgerc", time = Time.now)
      path = File.expand_path(name)
      return unless File.exist?(path)
      opts = File.readlines(path).reduce({}) do |acc, entry|
        _, k, v = Array(entry.match(ENTRY_REGEX))
        acc[k] = v if k && v
        acc
      end
      new(client_secret: opts.fetch("client_secret"), host: opts.fetch("host"), access_token: opts.fetch("access_token"), client_token: opts.fetch("client_token"), max_body: opts.fetch("max-body", BODY_SIZE), time: time)
    rescue KeyError => e
      raise FileContentError, "bad file content, #{e.message}", e.backtrace
    end

    attr_reader :host, :max_body, :nonce, :timestamp

    def initialize(client_secret:, host:, access_token:, client_token:, 
                   max_body: BODY_SIZE, nonce: SecureRandom.uuid, time: Time.now)
      @client_secret = client_secret
      @host = URI(host)
      @access_token = access_token
      @client_token = client_token
      @max_body = max_body.to_i
      @nonce = nonce
      @timestamp = AkamaiCCU.format_utc(time) 
    end

    def touch
      @nonce = SecureRandom.uuid
      @timestamp = AkamaiCCU.format_utc(Time.now)
      self
    end

    def signed_key
      AkamaiCCU.sign_HMAC(key: @client_secret, data: @timestamp)
    end

    def auth_header
      DIGEST.dup.tap do |header|
        header << " "
        header << "client_token=#{@client_token};"
        header << "access_token=#{@access_token};"
        header << "timestamp=#{@timestamp};"
        header << "nonce=#{@nonce};"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
akamai_ccu-1.5.4 lib/akamai_ccu/secret.rb