class Xcflushd::Credentials

Credentials contains all the fields required to authenticate an app. In 3scale there are 3 authentication modes:

* App ID: app_id (required), app_key, referrer, user_id
* API key: user_key (required), referrer, user_id
* Oauth: access_token (required), app_id, referrer, user_id

Constants

FIELDS

Attributes

creds[R]

Public Class Methods

from(escaped_s) click to toggle source

Creates a Credentials object from an escaped string. The string has this format: credential1:value1,credential2:value2, etc. ',' and ':' are escaped in the values

# File lib/xcflushd/credentials.rb, line 44
def self.from(escaped_s)
  creds_hash = escaped_s.split(/(?<!\\),/)
                        .map { |field_value| field_value.split(/(?<!\\):/) }
                        .map { |split| [unescaped(split[0]).to_sym,
                                        unescaped(split[1])] }
                        .to_h

  new(creds_hash)
end
new(creds) click to toggle source

Initializes a credentials object from a 'creds' hash. The accepted fields of the hash are:

app_id, app_key, referrer, user_id, user_key, and access_token.

Extra fields are discarded.

# File lib/xcflushd/credentials.rb, line 19
def initialize(creds)
  @creds = creds.select { |k, _| FIELDS.include?(k) }
end

Private Class Methods

unescaped(string) click to toggle source
# File lib/xcflushd/credentials.rb, line 61
def self.unescaped(string)
  string.gsub(/\\([,:])/, '\1')
end

Public Instance Methods

==(other) click to toggle source
# File lib/xcflushd/credentials.rb, line 33
def ==(other)
  self.class == other.class && creds == other.creds
end
oauth?() click to toggle source
# File lib/xcflushd/credentials.rb, line 37
def oauth?
  !creds[:access_token].nil?
end
to_sorted_escaped_s() click to toggle source

This method returns all the credentials with this format: credential1:value1,credential2:value2, etc. The delimiters used, ',' and ':', are escaped in the values. Also, the credentials appear in alphabetical order.

# File lib/xcflushd/credentials.rb, line 27
def to_sorted_escaped_s
  creds.sort_by { |cred, _| cred }
       .map { |cred, value| "#{escaped(cred.to_s)}:#{escaped(value)}" }
       .join(',')
end

Private Instance Methods

escaped(string) click to toggle source
# File lib/xcflushd/credentials.rb, line 56
def escaped(string)
  string.gsub(','.freeze, "\\,".freeze)
        .gsub(':'.freeze, "\\:".freeze)
end