class OpenID::AX::FetchRequest

An attribute exchange 'fetch_request' message. This message is sent by a relying party when it wishes to obtain attributes about the subject of an OpenID authentication request.

Constants

MODE

Attributes

requested_attributes[R]
update_url[RW]

Public Class Methods

from_openid_request(oidreq) click to toggle source

Extract a FetchRequest from an OpenID message message: OpenID::Message return a FetchRequest or nil if AX arguments are not present

# File lib/openid/extensions/ax.rb, line 189
def self.from_openid_request(oidreq)
  message = oidreq.message
  ax_args = message.get_args(NS_URI)
  return nil if ax_args == {} or ax_args['mode'] != MODE
  req = new
  req.parse_extension_args(ax_args)

  if req.update_url
    realm = message.get_arg(OPENID_NS, 'realm',
                            message.get_arg(OPENID_NS, 'return_to'))
    if realm.nil? or realm.empty?
      raise Error, "Cannot validate update_url #{req.update_url.inspect} against absent realm"
    end
    tr = TrustRoot::TrustRoot.parse(realm)
    unless tr.validate_url(req.update_url)
      raise Error, "Update URL #{req.update_url.inspect} failed validation against realm #{realm.inspect}"
    end
  end

  return req
end
new(update_url = nil) click to toggle source
Calls superclass method OpenID::AX::AXMessage.new
# File lib/openid/extensions/ax.rb, line 123
def initialize(update_url = nil)
  super()
  @mode = MODE
  @requested_attributes = {}
  @update_url = update_url
end

Public Instance Methods

add(attribute) click to toggle source

Add an attribute to this attribute exchange request. attribute: AttrInfo, the attribute being requested Raises IndexError if the requested attribute is already present

in this request.
# File lib/openid/extensions/ax.rb, line 134
def add(attribute)
  if @requested_attributes[attribute.type_uri]
    raise IndexError, "The attribute #{attribute.type_uri} has already been requested"
  end
  @requested_attributes[attribute.type_uri] = attribute
end
attributes() click to toggle source

return the list of AttrInfo objects contained in the FetchRequest

# File lib/openid/extensions/ax.rb, line 255
def attributes
  @requested_attributes.values
end
get_extension_args() click to toggle source

Get the serialized form of this attribute fetch request. returns a hash of the arguments

# File lib/openid/extensions/ax.rb, line 143
def get_extension_args
  aliases = NamespaceMap.new
  required = []
  if_available = []
  ax_args = new_args
  @requested_attributes.each{|type_uri, attribute|
    if attribute.ns_alias
      name = aliases.add_alias(type_uri, attribute.ns_alias)
    else
      name = aliases.add(type_uri)
    end
    if attribute.required
      required << name
    else
      if_available << name
    end
    if attribute.count != 1
      ax_args["count.#{name}"] = attribute.count.to_s
    end
    ax_args["type.#{name}"] = type_uri
  }

  unless required.empty?
    ax_args['required'] = required.join(',')
  end
  unless if_available.empty?
    ax_args['if_available'] = if_available.join(',')
  end
  return ax_args
end
get_required_attrs() click to toggle source

Get the type URIs for all attributes that have been marked as required.

# File lib/openid/extensions/ax.rb, line 176
def get_required_attrs
  @requested_attributes.inject([]) {|required, (type_uri, attribute)|
    if attribute.required
      required << type_uri
    else
      required
    end
  }
end
member?(type_uri) click to toggle source
# File lib/openid/extensions/ax.rb, line 264
def member?(type_uri)
  ! @requested_attributes[type_uri].nil?
end
parse_extension_args(ax_args) click to toggle source
# File lib/openid/extensions/ax.rb, line 211
def parse_extension_args(ax_args)
  check_mode(ax_args)

  aliases = NamespaceMap.new

  ax_args.each{|k,v|
    if k.index('type.') == 0
      name = k[5..-1]
      type_uri = v
      aliases.add_alias(type_uri, name)

      count_key = 'count.'+name
      count_s = ax_args[count_key]
      count = 1
      if count_s
        if count_s == UNLIMITED_VALUES
          count = count_s
        else
          count = count_s.to_i
          if count <= 0
            raise Error, "Invalid value for count #{count_key.inspect}: #{count_s.inspect}"
          end
        end
      end
      add(AttrInfo.new(type_uri, name, false, count))
    end
  }

  required = AX.to_type_uris(aliases, ax_args['required'])
  required.each{|type_uri|
    @requested_attributes[type_uri].required = true
  }
  if_available = AX.to_type_uris(aliases, ax_args['if_available'])
  all_type_uris = required + if_available

  aliases.namespace_uris.each{|type_uri|
    unless all_type_uris.member? type_uri
      raise Error, "Type URI #{type_uri.inspect} was in the request but not present in 'required' or 'if_available'"
    end
  }
  @update_url = ax_args['update_url']
end
requested_types() click to toggle source

return the list of requested attribute type URIs

# File lib/openid/extensions/ax.rb, line 260
def requested_types
  @requested_attributes.keys
end