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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

Instance Attribute Details

#ns_uriObject (readonly)

Returns the value of attribute ns_uri.



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

def ns_uri
  @ns_uri
end

#optionalObject (readonly)

Returns the value of attribute optional.



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

def optional
  @optional
end

#policy_urlObject

Returns the value of attribute policy_url.



78
79
80
# File 'lib/openid/extensions/sreg.rb', line 78

def policy_url
  @policy_url
end

#requiredObject (readonly)

Returns the value of attribute required.



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

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.



101
102
103
104
105
106
107
108
109
110
111
# 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

Instance Method Details

#all_requested_fieldsObject

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



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

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.



206
207
208
209
210
211
212
# 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) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# 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) ⇒ 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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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) ⇒ 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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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) ⇒ Object

Add the given list of fields to the request.

Raises:

  • (ArgumentError)


196
197
198
199
200
# 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?Boolean

Have any simple registration fields been requested?

Returns:

  • (Boolean)


158
159
160
# File 'lib/openid/extensions/sreg.rb', line 158

def were_fields_requested?
  !all_requested_fields.empty?
end