class OneLogin::RubySaml::Attributes
SAML2 Attributes
. Parse the Attributes
from the AttributeStatement of the SAML Response
.
Attributes
Public Class Methods
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 35 def initialize(attrs = {}) @attributes = attrs end
@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'], })
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 18 def self.single_value_compatibility @@single_value_compatibility end
@return [Boolean] Get current status of backwards compatibility mode.
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 25 def self.single_value_compatibility=(value) @@single_value_compatibility = value end
Sets the backwards compatibility mode on/off. @param value [Boolean]
Public Instance Methods
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 108 def ==(other) if other.is_a?(Attributes) all == other.all else super end end
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
Source
# 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
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']
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 99 def add(name, values = []) attributes[canonize_name(name)] ||= [] attributes[canonize_name(name)] += Array(values) end
@param name [String] The attribute name @param values [Array] The values
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 84 def all attributes end
@return [Hash] Return all attributes as a hash
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 42 def each attributes.each{|name, values| yield name, values} end
Iterate over all attributes
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 124 def fetch(name) attributes.each_key do |attribute_key| if name.is_a?(Regexp) if name.respond_to? :match? return self[attribute_key] if name.match?(attribute_key) else return self[attribute_key] if name.match(attribute_key) end elsif canonize_name(name) == canonize_name(attribute_key) return self[attribute_key] end end nil end
Fetch attribute value using name or regex @param name [String|Regexp] 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']
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 50 def include?(name) attributes.has_key?(canonize_name(name)) end
Test attribute presence by name @param name [String] The attribute name to be checked
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 66 def multi(name) attributes[canonize_name(name)] end
Return all values for an attribute @param name [String] The attribute name @return [Array] Values of the attribute
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 91 def set(name, values) attributes[canonize_name(name)] = values end
@param name [String] The attribute name @param values [Array] The values
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 58 def single(name) attributes[canonize_name(name)].first if include?(name) end
Return first value for an attribute @param name [String] The attribute name @return [String] The value (First occurrence)
Protected Instance Methods
Source
# File lib/onelogin/ruby-saml/attributes.rb, line 145 def canonize_name(name) name.to_s end
stringifies all names so both ‘email’ and :email return the same result @param name [String] The attribute name @return [String] stringified name