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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



138
139
140
141
142
143
144
# File 'lib/onelogin/ruby-saml/attributes.rb', line 138

def method_missing(name, *args, &block)
  if attributes.respond_to?(name)
    attributes.send(name, *args, &block)
  else
    super
  end
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



116
117
118
119
120
121
122
# File 'lib/onelogin/ruby-saml/attributes.rb', line 116

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:



86
87
88
# File 'lib/onelogin/ruby-saml/attributes.rb', line 86

def [](name)
  self.class.single_value_compatibility ? single(name) : multi(name)
end

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

Parameters:

  • name (String)

    The attribute name

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

    The values



107
108
109
110
# File 'lib/onelogin/ruby-saml/attributes.rb', line 107

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

#allArray

Return all attributes as an array

Returns:

  • (Array)

    Return all attributes as an array



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

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

#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)) || attributes.has_key?(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
69
70
71
72
73
74
75
76
# File 'lib/onelogin/ruby-saml/attributes.rb', line 66

def multi(name)
  values = attributes[canonize_name(name)] || attributes[name]
  
  if values.is_a?(Array)
    values
  elsif !values.nil?
    Array(values)
  else
    nil
  end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/onelogin/ruby-saml/attributes.rb', line 124

def respond_to?(name)
  attributes.respond_to?(name) || super
end

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

Parameters:

  • name (String)

    The attribute name

  • values (Array)

    The values



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

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)
  multi(name).first if include?(name)
end