Class: OpenID::AX::FetchRequest

Inherits:
AXMessage show all
Defined in:
lib/openid/extensions/ax.rb

Overview

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.

Constant Summary collapse

MODE =
'fetch_request'

Constants inherited from AXMessage

AXMessage::NS_URI

Instance Attribute Summary collapse

Attributes inherited from AXMessage

#mode, #ns_alias, #ns_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Extension

#to_message

Constructor Details

#initialize(update_url = nil) ⇒ FetchRequest

Returns a new instance of FetchRequest.



123
124
125
126
127
128
# File 'lib/openid/extensions/ax.rb', line 123

def initialize(update_url = nil)
  super()
  @mode = MODE
  @requested_attributes = {}
  @update_url = update_url
end

Instance Attribute Details

#requested_attributesObject (readonly)

Returns the value of attribute requested_attributes.



118
119
120
# File 'lib/openid/extensions/ax.rb', line 118

def requested_attributes
  @requested_attributes
end

#update_urlObject

Returns the value of attribute update_url.



119
120
121
# File 'lib/openid/extensions/ax.rb', line 119

def update_url
  @update_url
end

Class Method Details

.from_openid_request(oidreq) ⇒ Object

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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

Instance Method Details

#add(attribute) ⇒ Object

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.


134
135
136
137
138
139
# 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

#attributesObject

return the list of AttrInfo objects contained in the FetchRequest



255
256
257
# File 'lib/openid/extensions/ax.rb', line 255

def attributes
  @requested_attributes.values
end

#get_extension_argsObject

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# 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_attrsObject

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



176
177
178
179
180
181
182
183
184
# 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) ⇒ Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/openid/extensions/ax.rb', line 264

def member?(type_uri)
  ! @requested_attributes[type_uri].nil?
end

#parse_extension_args(ax_args) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# 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_typesObject

return the list of requested attribute type URIs



260
261
262
# File 'lib/openid/extensions/ax.rb', line 260

def requested_types
  @requested_attributes.keys
end