class OneLogin::RubySaml::Attributes

SAML2 Attributes. Parse the Attributes from the AttributeStatement of the SAML Response.

Attributes

attributes[R]

Public Class Methods

new(attrs = {}) click to toggle source

@param attrs [Hash] The attrs must be a Hash with attribute names as keys and *arrays* as values:

Attributes.new({
  'name' => ['value1', 'value2'],
  'mail' => ['value1'],
})
# File lib/onelogin/ruby-saml/attributes.rb, line 35
def initialize(attrs = {})
  @attributes = attrs
end
single_value_compatibility() click to toggle source

@return [Boolean] Get current status of backwards compatibility mode.

# File lib/onelogin/ruby-saml/attributes.rb, line 18
def self.single_value_compatibility
  @@single_value_compatibility
end
single_value_compatibility=(value) click to toggle source

Sets the backwards compatibility mode on/off. @param value [Boolean]

# File lib/onelogin/ruby-saml/attributes.rb, line 25
def self.single_value_compatibility=(value)
  @@single_value_compatibility = value
end

Public Instance Methods

==(other) click to toggle source

Make comparable to another Attributes collection based on attributes @param other [Attributes] An Attributes object to compare with @return [Boolean] True if are contains the same attributes and values

Calls superclass method
# File lib/onelogin/ruby-saml/attributes.rb, line 108
def ==(other)
  if other.is_a?(Attributes)
    all == other.all
  else
    super
  end
end
[](name) click to toggle source

Retrieve attribute value(s) @param name [String] The attribute name @return [String|Array] Depending on the single value compatibility status this returns:

- First value if single_value_compatibility = true
  response.attributes['mail']  # => 'user@example.com'
- All values if single_value_compatibility = false
  response.attributes['mail']  # => ['user@example.com','user@example.net']
# File lib/onelogin/ruby-saml/attributes.rb, line 78
def [](name)
  self.class.single_value_compatibility ? single(canonize_name(name)) : multi(canonize_name(name))
end
[]=(name, values)
Alias for: set
add(name, values = []) click to toggle source

@param name [String] The attribute name @param values [Array] The values

# File lib/onelogin/ruby-saml/attributes.rb, line 99
def add(name, values = [])
  attributes[canonize_name(name)] ||= []
  attributes[canonize_name(name)] += Array(values)
end
all() click to toggle source

@return [Array] Return all attributes as an array

# File lib/onelogin/ruby-saml/attributes.rb, line 84
def all
  attributes
end
each() { |name, values| ... } click to toggle source

Iterate over all attributes

# File lib/onelogin/ruby-saml/attributes.rb, line 42
def each
  attributes.each{|name, values| yield name, values}
end
include?(name) click to toggle source

Test attribute presence by name @param name [String] The attribute name to be checked

# File lib/onelogin/ruby-saml/attributes.rb, line 50
def include?(name)
  attributes.has_key?(canonize_name(name))
end
multi(name) click to toggle source

Return all values for an attribute @param name [String] The attribute name @return [Array] Values of the attribute

# File lib/onelogin/ruby-saml/attributes.rb, line 66
def multi(name)
  attributes[canonize_name(name)]
end
set(name, values) click to toggle source

@param name [String] The attribute name @param values [Array] The values

# File lib/onelogin/ruby-saml/attributes.rb, line 91
def set(name, values)
  attributes[canonize_name(name)] = values
end
Also aliased as: []=
single(name) click to toggle source

Return first value for an attribute @param name [String] The attribute name @return [String] The value (First occurrence)

# File lib/onelogin/ruby-saml/attributes.rb, line 58
def single(name)
  attributes[canonize_name(name)].first if include?(name)
end

Protected Instance Methods

canonize_name(name) click to toggle source

stringifies all names so both 'email' and :email return the same result @param name [String] The attribute name @return [String] stringified name

# File lib/onelogin/ruby-saml/attributes.rb, line 122
def canonize_name(name)
  name.to_s
end