Class: OpenID::SReg::Request

Inherits:
Extension show all
Defined in:
lib/openid/extensions/sreg.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Extension

#to_message

Constructor Details

#initialize(required = nil, optional = nil, policy_url = nil, ns_uri = NS_URI) ⇒ Request

Returns a new instance of Request.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openid/extensions/sreg.rb', line 78

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 = []

  request_fields(required, true, true) if required
  return unless optional

  request_fields(optional, false, true)
end

Instance Attribute Details

#ns_uriObject (readonly)

Returns the value of attribute ns_uri.



75
76
77
# File 'lib/openid/extensions/sreg.rb', line 75

def ns_uri
  @ns_uri
end

#optionalObject (readonly)

Returns the value of attribute optional.



75
76
77
# File 'lib/openid/extensions/sreg.rb', line 75

def optional
  @optional
end

#policy_urlObject

Returns the value of attribute policy_url.



76
77
78
# File 'lib/openid/extensions/sreg.rb', line 76

def policy_url
  @policy_url
end

#requiredObject (readonly)

Returns the value of attribute required.



75
76
77
# File 'lib/openid/extensions/sreg.rb', line 75

def required
  @required
end

Class Method Details

.from_openid_request(request) ⇒ Object

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.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openid/extensions/sreg.rb', line 98

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 if args == {}

  req = new(nil, nil, nil, ns_uri)
  req.parse_extension_args(args)
  req
end

Instance Method Details

#all_requested_fieldsObject

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



147
148
149
# File 'lib/openid/extensions/sreg.rb', line 147

def all_requested_fields
  @required + @optional
end

#get_extension_argsObject

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.



199
200
201
202
203
204
205
# File 'lib/openid/extensions/sreg.rb', line 199

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?
  args
end

#member?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/openid/extensions/sreg.rb', line 207

def member?(field_name)
  all_requested_fields.member?(field_name)
end

#parse_extension_args(args, strict = false) ⇒ Object

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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/openid/extensions/sreg.rb', line 124

def parse_extension_args(args, strict = false)
  required_items = args["required"]
  unless required_items.nil? or required_items.empty?
    required_items.split(",").each do |field_name|
      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 do |field_name|
      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) ⇒ Object

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/openid/extensions/sreg.rb', line 165

def request_field(field_name, required = false, strict = false)
  OpenID.check_sreg_field_name(field_name)

  if strict
    raise ArgumentError, "That field has already been requested" if (@required + @optional).member?(field_name)
  else
    return if @required.member?(field_name)

    if @optional.member?(field_name)
      return unless required

      @optional.delete(field_name)

    end
  end
  if required
    @required << field_name
  else
    @optional << field_name
  end
end

#request_fields(field_names, required = false, strict = false) ⇒ Object

Add the given list of fields to the request.

Raises:

  • (ArgumentError)


188
189
190
191
192
193
# File 'lib/openid/extensions/sreg.rb', line 188

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?Boolean

Have any simple registration fields been requested?

Returns:

  • (Boolean)


152
153
154
# File 'lib/openid/extensions/sreg.rb', line 152

def were_fields_requested?
  !all_requested_fields.empty?
end