Class: OneLogin::RubySaml::Attributes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/onelogin/ruby-saml/attributes.rb

Overview

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

Constant Summary collapse

@@single_value_compatibility =

By default Attributes#[] is backwards compatible and returns only the first value for the attribute Setting this to ‘false` returns all values for an attribute

true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Attributes

Returns a new instance of Attributes.

Parameters:

  • attrs (Hash) (defaults to: {})

    The attrs must be a Hash with attribute names as keys and arrays as values: Attributes.new(

    'name' => ['value1', 'value2'],
    'mail' => ['value1'],
    

    )



35
36
37
# File 'lib/onelogin/ruby-saml/attributes.rb', line 35

def initialize(attrs = {})
  @attributes = attrs
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/onelogin/ruby-saml/attributes.rb', line 9

def attributes
  @attributes
end

Class Method Details

.single_value_compatibilityBoolean

Returns Get current status of backwards compatibility mode.

Returns:

  • (Boolean)

    Get current status of backwards compatibility mode.



18
19
20
# File 'lib/onelogin/ruby-saml/attributes.rb', line 18

def self.single_value_compatibility
  @@single_value_compatibility
end

.single_value_compatibility=(value) ⇒ Object

Sets the backwards compatibility mode on/off.

Parameters:

  • value (Boolean)


25
26
27
# File 'lib/onelogin/ruby-saml/attributes.rb', line 25

def self.single_value_compatibility=(value)
  @@single_value_compatibility = value
end

Instance Method Details

#==(other) ⇒ Boolean

Make comparable to another Attributes collection based on attributes

Parameters:

  • other (Attributes)

    An Attributes object to compare with

Returns:

  • (Boolean)

    True if are contains the same attributes and values



108
109
110
111
112
113
114
# File 'lib/onelogin/ruby-saml/attributes.rb', line 108

def ==(other)
  if other.is_a?(Attributes)
    all == other.all
  else
    super
  end
end

#[](name) ⇒ String|Array

Retrieve attribute value(s)

Parameters:

  • name (String)

    The attribute name

Returns:



78
79
80
# 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

#add(name, values = []) ⇒ Object

Parameters:

  • name (String)

    The attribute name

  • values (Array) (defaults to: [])

    The values



99
100
101
102
# File 'lib/onelogin/ruby-saml/attributes.rb', line 99

def add(name, values = [])
  attributes[canonize_name(name)] ||= []
  attributes[canonize_name(name)] += Array(values)
end

#allHash

Return all attributes as a hash

Returns:

  • (Hash)

    Return all attributes as a hash



84
85
86
# File 'lib/onelogin/ruby-saml/attributes.rb', line 84

def all
  attributes
end

#eachObject

Iterate over all attributes



42
43
44
# File 'lib/onelogin/ruby-saml/attributes.rb', line 42

def each
  attributes.each{|name, values| yield name, values}
end

#fetch(name) ⇒ String|Array

Fetch attribute value using name or regex

Parameters:

  • name (String|Regexp)

    The attribute name

Returns:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

#include?(name) ⇒ Boolean

Test attribute presence by name

Parameters:

  • name (String)

    The attribute name to be checked

Returns:

  • (Boolean)


50
51
52
# File 'lib/onelogin/ruby-saml/attributes.rb', line 50

def include?(name)
  attributes.has_key?(canonize_name(name))
end

#multi(name) ⇒ Array

Return all values for an attribute

Parameters:

  • name (String)

    The attribute name

Returns:

  • (Array)

    Values of the attribute



66
67
68
# File 'lib/onelogin/ruby-saml/attributes.rb', line 66

def multi(name)
  attributes[canonize_name(name)]
end

#set(name, values) ⇒ Object Also known as: []=

Parameters:

  • name (String)

    The attribute name

  • values (Array)

    The values



91
92
93
# File 'lib/onelogin/ruby-saml/attributes.rb', line 91

def set(name, values)
  attributes[canonize_name(name)] = values
end

#single(name) ⇒ String

Return first value for an attribute

Parameters:

  • name (String)

    The attribute name

Returns:

  • (String)

    The value (First occurrence)



58
59
60
# File 'lib/onelogin/ruby-saml/attributes.rb', line 58

def single(name)
  attributes[canonize_name(name)].first if include?(name)
end