Sha256: 25bd4ea3df9c075160cd7bd3f8419c2cca6d4712924b32229eb7aafb6975bdb7

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 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

    class << self
      private def factory(opts, time)
        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)
      end

      def by_file(name = "~/.edgerc", time = Time.now)
        path = File.expand_path(name)
        return unless File.exist?(path)
        data = File.readlines(path).map(&:strip).reduce([]) do |acc, entry|
          m = entry.match(ENTRY_REGEX)
          acc << [m[1], m[2]] if m
          acc
        end
        factory(Hash[data], time)
      rescue KeyError => e
        raise FileContentError, "bad file content, #{e.message}", e.backtrace
      end
    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

2 entries across 2 versions & 1 rubygems

Version Path
akamai_ccu-1.5.1 lib/akamai_ccu/secret.rb
akamai_ccu-1.5.0 lib/akamai_ccu/secret.rb