Class: AkamaiCCU::Secret

Inherits:
Object
  • Object
show all
Defined in:
lib/akamai_ccu/secret.rb

Defined Under Namespace

Classes: FileContentError

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_secret:, host:, access_token:, client_token:, max_body: BODY_SIZE, nonce: SecureRandom.uuid, time: Time.now) ⇒ Secret

Returns a new instance of Secret.



43
44
45
46
47
48
49
50
51
52
# File 'lib/akamai_ccu/secret.rb', line 43

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 = self.class.format_utc(time) 
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



41
42
43
# File 'lib/akamai_ccu/secret.rb', line 41

def host
  @host
end

#max_bodyObject (readonly)

Returns the value of attribute max_body.



41
42
43
# File 'lib/akamai_ccu/secret.rb', line 41

def max_body
  @max_body
end

#nonceObject (readonly)

Returns the value of attribute nonce.



41
42
43
# File 'lib/akamai_ccu/secret.rb', line 41

def nonce
  @nonce
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



41
42
43
# File 'lib/akamai_ccu/secret.rb', line 41

def timestamp
  @timestamp
end

Class Method Details

.by_file(name = "~/.edgerc", time = Time.now) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/akamai_ccu/secret.rb', line 28

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

.format_utc(time) ⇒ Object



14
15
16
# File 'lib/akamai_ccu/secret.rb', line 14

def self.format_utc(time)
  time.utc.strftime("%Y%m%dT%H:%M:%S+0000")
end

.sign(data) ⇒ Object



18
19
20
21
# File 'lib/akamai_ccu/secret.rb', line 18

def self.sign(data)
  digest = OpenSSL::Digest::SHA256.new.digest(data)
  Base64.encode64(digest).strip
end

.sign_HMAC(key, data) ⇒ Object



23
24
25
26
# File 'lib/akamai_ccu/secret.rb', line 23

def self.sign_HMAC(key, data)
  digest = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data)
  Base64.encode64(digest).strip
end

Instance Method Details

#auth_headerObject



64
65
66
67
68
69
70
71
72
# File 'lib/akamai_ccu/secret.rb', line 64

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

#signed_keyObject



60
61
62
# File 'lib/akamai_ccu/secret.rb', line 60

def signed_key
  self.class.sign_HMAC(@client_secret, @timestamp)
end

#touchObject



54
55
56
57
58
# File 'lib/akamai_ccu/secret.rb', line 54

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