Sha256: 85595a5d4316b30e96240fe21997cd055c8581ae7667bbb01f99534aca51c966

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require "uri"
require "securerandom"

module AkamaiCCU
  class Secret
    DIGEST = "EG1-HMAC-SHA256"
    EQUALITY = " = "

    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", 2048), time: time)
      end

      def by_txt(name, time = Time.now)
        path = File.expand_path(name)
        return unless File.exist?(path)
        data = File.readlines(path).map(&:strip).reject(&:empty?).map do |entry| 
          entry.split(EQUALITY)
        end
        factory(Hash[data], time)
      end

      def by_edgerc(name = "~/.edgerc", time = Time.now)
        path = File.expand_path(name)
        return unless File.exist?(path)
        data = File.readlines(path).map(&:strip)
        data.shift
        data.map! { |entry| entry.split(EQUALITY) }
        factory(Hash[data], time)
      end
    end

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

    def initialize(client_secret:, host:, access_token:, client_token:, 
                   max_body: 2048, 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.4.1 lib/akamai_ccu/secret.rb
akamai_ccu-1.4.0 lib/akamai_ccu/secret.rb