class SAML2::Attribute

Constants

XS_TYPES

Attributes

friendly_name[RW]

@return [String, nil]

name[RW]

@return [String]

name_format[RW]

@return [String, nil]

value[RW]

@return [Object, nil]

Public Class Methods

create(name, value = nil) click to toggle source

Create an appropriate object to represent an attribute.

This will create the most appropriate object (i.e. an {Attribute::X500} if possible) to represent this attribute, based on its name. @param name [String]

The attribute name. This can be a friendly name, or a URI.

@param value optional

The attribute value.

@return [Attribute]

# File lib/saml2/attribute.rb, line 39
def create(name, value = nil)
  (class_for(name) || self).new(name, value)
end
element() click to toggle source

The XML element that this attribute class serializes as. @return [‘Attribute’]

# File lib/saml2/attribute.rb, line 51
def element
  "Attribute"
end
from_xml(node) click to toggle source

(see Base.from_xml)

Calls superclass method
# File lib/saml2/attribute.rb, line 20
def from_xml(node)
  # pass through for subclasses
  return super unless self == Attribute

  # look for an appropriate subclass
  klass = class_for(node)
  klass ? klass.from_xml(node) : super
end
namespace() click to toggle source

The XML namespace that this attribute class serializes as. @return [‘saml’]

# File lib/saml2/attribute.rb, line 45
def namespace
  "saml"
end
new(name = nil, value = nil, friendly_name = nil, name_format = nil) click to toggle source

Create a new generic Attribute

@param name [String] @param value optional [Object, nil] @param friendly_name optional [String, nil] @param name_format optional [String, nil]

Calls superclass method
# File lib/saml2/attribute.rb, line 86
def initialize(name = nil, value = nil, friendly_name = nil, name_format = nil)
  super()
  @name = name
  @value = value
  @friendly_name = friendly_name
  @name_format = name_format
end

Protected Class Methods

class_for(name_or_node) click to toggle source
# File lib/saml2/attribute.rb, line 66
def class_for(name_or_node)
  subclasses.find do |klass|
    klass.respond_to?(:recognizes?) && klass.recognizes?(name_or_node)
  end
end
inherited(klass) click to toggle source
Calls superclass method
# File lib/saml2/attribute.rb, line 61
def inherited(klass)
  super
  subclasses << klass
end
subclasses() click to toggle source
# File lib/saml2/attribute.rb, line 57
def subclasses
  @subclasses ||= []
end

Public Instance Methods

build(builder) click to toggle source

(see Base#build)

# File lib/saml2/attribute.rb, line 95
def build(builder)
  builder[self.class.namespace].__send__(self.class.element, "Name" => name) do |attribute|
    attribute.parent["FriendlyName"] = friendly_name if friendly_name
    attribute.parent["NameFormat"] = name_format if name_format
    Array.wrap(value).each do |value|
      xsi_type, val = convert_to_xsi(value)
      attribute["saml"].AttributeValue(val) do |attribute_value|
        attribute_value.parent["xsi:type"] = xsi_type if xsi_type
      end
    end
  end
end
from_xml(node) click to toggle source

(see Base#from_xml)

Calls superclass method
# File lib/saml2/attribute.rb, line 109
def from_xml(node)
  super
  @name = node["Name"]
  @friendly_name = node["FriendlyName"]
  @name_format = node["NameFormat"]
  values = node.xpath("saml:AttributeValue", Namespaces::ALL).map do |value|
    convert_from_xsi(value.attribute_with_ns("type", Namespaces::XSI), value.content && value.content.strip)
  end
  @value = case values.length
           when 0 then nil
           when 1 then values.first
           else; values
           end
end

Private Instance Methods

convert_from_xsi(type, value) click to toggle source
# File lib/saml2/attribute.rb, line 151
def convert_from_xsi(type, value)
  return value unless type

  qname = self.class.lookup_qname(type.value, type.namespaces)

  info = XS_TYPES[qname]
  value = info.last.call(value) if info&.last
  value
end
convert_to_xsi(value) click to toggle source
# File lib/saml2/attribute.rb, line 137
def convert_to_xsi(value)
  xs_type = nil
  converter = nil
  XS_TYPES.each do |type, (klasses, to_xsi, _from_xsi)|
    next unless Array.wrap(klasses).any? { |klass| value.is_a?(klass) }

    xs_type = "xs:#{type.last}"
    converter = to_xsi
    break
  end
  value = converter.call(value) if converter
  [xs_type, value]
end