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.



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

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.



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

def requested_attributes
  @requested_attributes
end

#update_urlObject

Returns the value of attribute update_url.



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

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/openid/extensions/ax.rb', line 185

def self.from_openid_request(oidreq)
  message = oidreq.message
  ax_args = message.get_args(NS_URI)
  return 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

  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.


135
136
137
138
139
140
141
# File 'lib/openid/extensions/ax.rb', line 135

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/openid/extensions/ax.rb', line 145

def get_extension_args
  aliases = NamespaceMap.new
  required = []
  if_available = []
  ax_args = new_args
  @requested_attributes.each do |type_uri, attribute|
    name = if attribute.ns_alias
      aliases.add_alias(type_uri, attribute.ns_alias)
    else
      aliases.add(type_uri)
    end
    if attribute.required
      required << name
    else
      if_available << name
    end
    ax_args["count.#{name}"] = attribute.count.to_s if attribute.count != 1
    ax_args["type.#{name}"] = type_uri
  end

  ax_args["required"] = required.join(",") unless required.empty?
  ax_args["if_available"] = if_available.join(",") unless if_available.empty?
  ax_args
end

#get_required_attrsObject

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



172
173
174
175
176
177
178
179
180
# File 'lib/openid/extensions/ax.rb', line 172

def get_required_attrs
  @requested_attributes.inject([]) do |required, (type_uri, attribute)|
    if attribute.required
      required << type_uri
    else
      required
    end
  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



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 212

def parse_extension_args(ax_args)
  check_mode(ax_args)

  aliases = NamespaceMap.new

  ax_args.each do |k, v|
    next unless 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
        raise Error, "Invalid value for count #{count_key.inspect}: #{count_s.inspect}" if count <= 0
      end
    end
    add(AttrInfo.new(type_uri, name, false, count))
  end

  required = AX.to_type_uris(aliases, ax_args["required"])
  required.each do |type_uri|
    @requested_attributes[type_uri].required = true
  end
  if_available = AX.to_type_uris(aliases, ax_args["if_available"])
  all_type_uris = required + if_available

  aliases.namespace_uris.each do |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
  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