class OpenID::SReg::Request

An object to hold the state of a simple registration request.

Attributes

ns_uri[R]
optional[R]
policy_url[RW]
required[R]

Public Class Methods

from_openid_request(request) click to toggle source

Create a simple registration request that contains the fields that were requested in the OpenID request with the given arguments Takes an OpenID::CheckIDRequest, returns an OpenID::Sreg::Request return nil if the extension was not requested.

# File lib/openid/extensions/sreg.rb, line 101
def self.from_openid_request(request)
  # Since we're going to mess with namespace URI mapping, don't
  # mutate the object that was passed in.
  message = request.message.copy
  ns_uri = OpenID::get_sreg_ns(message)
  args = message.get_args(ns_uri)
  return nil if args == {}
  req = new(nil,nil,nil,ns_uri)
  req.parse_extension_args(args)
  return req
end
new(required = nil, optional = nil, policy_url = nil, ns_uri = NS_URI) click to toggle source
Calls superclass method OpenID::Extension.new
# File lib/openid/extensions/sreg.rb, line 79
def initialize(required = nil, optional = nil, policy_url = nil, ns_uri = NS_URI)
  super()

  @policy_url = policy_url
  @ns_uri = ns_uri
  @ns_alias = 'sreg'
  @required = []
  @optional = []

  if required
    request_fields(required, true, true)
  end
  if optional
    request_fields(optional, false, true)
  end
end

Public Instance Methods

all_requested_fields() click to toggle source

A list of all of the simple registration fields that were requested, whether they were required or optional.

# File lib/openid/extensions/sreg.rb, line 153
def all_requested_fields
  @required + @optional
end
get_extension_args() click to toggle source

Get a hash of unqualified simple registration arguments representing this request. This method is essentially the inverse of parse_extension_args. This method serializes the simple registration request fields.

# File lib/openid/extensions/sreg.rb, line 206
def get_extension_args
  args = {}
  args['required'] = @required.join(',') unless @required.empty?
  args['optional'] = @optional.join(',') unless @optional.empty?
  args['policy_url'] = @policy_url unless @policy_url.nil?
  return args
end
member?(field_name) click to toggle source
# File lib/openid/extensions/sreg.rb, line 214
def member?(field_name)
  all_requested_fields.member?(field_name)
end
parse_extension_args(args, strict = false) click to toggle source

Parse the unqualified simple registration request parameters and add them to this object.

This method is essentially the inverse of getExtensionArgs. This method restores the serialized simple registration request fields.

If you are extracting arguments from a standard OpenID checkid_* request, you probably want to use fromOpenIDRequest, which will extract the sreg namespace and arguments from the OpenID request. This method is intended for cases where the OpenID server needs more control over how the arguments are parsed than that method provides.

# File lib/openid/extensions/sreg.rb, line 126
def parse_extension_args(args, strict = false)
  required_items = args['required']
  unless required_items.nil? or required_items.empty?
    required_items.split(',').each{|field_name|
      begin
        request_field(field_name, true, strict)
      rescue ArgumentError
        raise if strict
      end
    }
  end

  optional_items = args['optional']
  unless optional_items.nil? or optional_items.empty?
    optional_items.split(',').each{|field_name|
      begin
        request_field(field_name, false, strict)
      rescue ArgumentError
        raise if strict
      end
    }
  end
  @policy_url = args['policy_url']
end
request_field(field_name, required=false, strict=false) click to toggle source

Request the specified field from the OpenID user field_name: the unqualified simple registration field name required: whether the given field should be presented

to the user as being a required to successfully complete
the request

strict: whether to raise an exception when a field is

added to a request more than once

Raises ArgumentError if the field_name is not a simple registration field, or if strict is set and a field is added more than once

# File lib/openid/extensions/sreg.rb, line 171
def request_field(field_name, required=false, strict=false)
  OpenID::check_sreg_field_name(field_name)

  if strict
    if (@required + @optional).member? field_name
      raise ArgumentError, 'That field has already been requested'
    end
  else
    return if @required.member? field_name
    if @optional.member? field_name
      if required
        @optional.delete field_name
      else
        return
      end
    end
  end
  if required
    @required << field_name
  else
    @optional << field_name
  end
end
request_fields(field_names, required = false, strict = false) click to toggle source

Add the given list of fields to the request.

# File lib/openid/extensions/sreg.rb, line 196
def request_fields(field_names, required = false, strict = false)
  raise ArgumentError unless field_names.respond_to?(:each) and
                             field_names[0].is_a?(String)
  field_names.each{|fn|request_field(fn, required, strict)}
end
were_fields_requested?() click to toggle source

Have any simple registration fields been requested?

# File lib/openid/extensions/sreg.rb, line 158
def were_fields_requested?
  !all_requested_fields.empty?
end