class Akami::WSSE

Akami::WSSE

Building Web Service Security.

Constants

PASSWORD_DIGEST_URI

PasswordDigest URI.

PASSWORD_TEXT_URI

PasswordText URI.

WSE_NAMESPACE

Namespace for WS Security Secext.

WSU_NAMESPACE

Namespace for WS Security Utility.

Attributes

created_at[RW]
digest[W]
expires_at[RW]
password[RW]
username[RW]

Public Instance Methods

[](key) click to toggle source

Returns a value from the WSSE Hash.

# File lib/akami/wsse.rb, line 26
def [](key)
  hash[key]
end
[]=(key, value) click to toggle source

Sets a value on the WSSE Hash.

# File lib/akami/wsse.rb, line 31
def []=(key, value)
  hash[key] = value
end
credentials(username, password, digest = false) click to toggle source

Sets authentication credentials for a wsse:UsernameToken header. Also accepts whether to use WSSE digest authentication.

# File lib/akami/wsse.rb, line 37
def credentials(username, password, digest = false)
  self.username = username
  self.password = password
  self.digest = digest
end
digest?() click to toggle source

Returns whether to use WSSE digest. Defaults to false.

# File lib/akami/wsse.rb, line 46
def digest?
  !!@digest
end
timestamp=(timestamp) click to toggle source

Sets whether to generate a wsu:Timestamp header.

# File lib/akami/wsse.rb, line 63
def timestamp=(timestamp)
  @wsu_timestamp = timestamp
end
timestamp?() click to toggle source

Returns whether to generate a wsu:Timestamp header.

# File lib/akami/wsse.rb, line 58
def timestamp?
  created_at || expires_at || @wsu_timestamp
end
to_xml() click to toggle source

Returns the XML for a WSSE header.

# File lib/akami/wsse.rb, line 68
def to_xml
  if username_token? && timestamp?
    Gyoku.xml wsse_username_token.merge!(wsu_timestamp) {
      |key, v1, v2| v1.merge!(v2) {
        |key, v1, v2| v1.merge!(v2)
      }
    }
  elsif username_token?
    Gyoku.xml wsse_username_token.merge!(hash)
  elsif timestamp?
    Gyoku.xml wsu_timestamp.merge!(hash)
  else
    ""
  end
end
username_token?() click to toggle source

Returns whether to generate a wsse:UsernameToken header.

# File lib/akami/wsse.rb, line 53
def username_token?
  username && password
end

Private Instance Methods

count() click to toggle source

Returns a new number with every call.

# File lib/akami/wsse.rb, line 144
def count
  @count ||= 0
  @count += 1
end
digest_password() click to toggle source

Returns the WSSE password, encrypted for digest authentication.

# File lib/akami/wsse.rb, line 123
def digest_password
  token = nonce + timestamp + password
  Base64.encode64(Digest::SHA1.hexdigest(token)).chomp!
end
hash() click to toggle source

Returns a memoized and autovivificating Hash.

# File lib/akami/wsse.rb, line 150
def hash
  @hash ||= Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
end
nonce() click to toggle source

Returns a WSSE nonce.

# File lib/akami/wsse.rb, line 129
def nonce
  @nonce ||= Digest::SHA1.hexdigest random_string + timestamp
end
random_string() click to toggle source

Returns a random String of 100 characters.

# File lib/akami/wsse.rb, line 134
def random_string
  (0...100).map { ("a".."z").to_a[rand(26)] }.join
end
security_hash(namespace, tag, hash) click to toggle source

Returns a Hash containing wsse/wsu Security details for a given namespace, tag and hash.

# File lib/akami/wsse.rb, line 112
def security_hash(namespace, tag, hash)
  {
    "wsse:Security" => {
      "#{namespace}:#{tag}" => hash,
      :attributes! => { "#{namespace}:#{tag}" => { "wsu:Id" => "#{tag}-#{count}", "xmlns:wsu" => WSU_NAMESPACE } }
    },
    :attributes! => { "wsse:Security" => { "xmlns:wsse" => WSE_NAMESPACE } }
  }
end
timestamp() click to toggle source

Returns a WSSE timestamp.

# File lib/akami/wsse.rb, line 139
def timestamp
  @timestamp ||= Time.now.xs_datetime
end
wsse_username_token() click to toggle source

Returns a Hash containing wsse:UsernameToken details.

# File lib/akami/wsse.rb, line 87
def wsse_username_token
  if digest?
    security_hash :wsse, "UsernameToken",
      "wsse:Username" => username,
      "wsse:Nonce" => nonce,
      "wsu:Created" => timestamp,
      "wsse:Password" => digest_password,
      :attributes! => { "wsse:Password" => { "Type" => PASSWORD_DIGEST_URI } }
  else
    security_hash :wsse, "UsernameToken",
      "wsse:Username" => username,
      "wsse:Password" => password,
      :attributes! => { "wsse:Password" => { "Type" => PASSWORD_TEXT_URI } }
  end
end
wsu_timestamp() click to toggle source

Returns a Hash containing wsu:Timestamp details.

# File lib/akami/wsse.rb, line 104
def wsu_timestamp
  security_hash :wsu, "Timestamp",
    "wsu:Created" => (created_at || Time.now).xs_datetime,
    "wsu:Expires" => (expires_at || (created_at || Time.now) + 60).xs_datetime
end