Class: OpenID::Message
- Inherits:
-
Object
- Object
- OpenID::Message
- Defined in:
- lib/openid/kvpost.rb,
lib/openid/message.rb
Defined Under Namespace
Classes: KeyNotFound
Constant Summary collapse
- @@registered_aliases =
Namespace / alias registration map. See register_namespace_alias.
{}
- @@allowed_openid_namespaces =
[OPENID1_NS, OPENID2_NS, OPENID11_NS]
Instance Attribute Summary collapse
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
Class Method Summary collapse
- .from_http_response(response, server_url) ⇒ Object
-
.from_kvform(kvform_string) ⇒ Object
Create a message from a KVForm string.
-
.from_openid_args(openid_args) ⇒ Object
Construct a Message from a parsed KVForm message.
-
.from_post_args(args) ⇒ Object
Construct a Message containing a set of POST arguments.
-
.register_namespace_alias(namespace_uri, alias_) ⇒ Object
Registers a (namespace URI, alias) mapping in a global namespace alias map.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#_fix_ns(namespace) ⇒ Object
Convert an input value into the internally used values of this obejct.
-
#_from_openid_args(openid_args) ⇒ Object
Raises InvalidNamespaceError if you try to instantiate a Message with a namespace not in the above allowed list.
- #_get_default_namespace(mystery_alias) ⇒ Object
- #copy ⇒ Object
-
#del_arg(namespace, key) ⇒ Object
Remove a single argument from this namespace.
- #get_aliased_arg(aliased_key, default = nil) ⇒ Object
-
#get_arg(namespace, key, default = nil) ⇒ Object
Get a value for a namespaced key.
-
#get_args(namespace) ⇒ Object
Get the arguments that are defined for this namespace URI.
-
#get_key(namespace, ns_key) ⇒ Object
Get the key for a particular namespaced argument.
- #get_openid_namespace ⇒ Object
- #has_key?(namespace, ns_key) ⇒ Boolean
-
#initialize(openid_namespace = nil) ⇒ Message
constructor
Raises InvalidNamespaceError if you try to instantiate a Message with a namespace not in the above allowed list.
- #is_openid1 ⇒ Object
- #is_openid2 ⇒ Object
-
#set_arg(namespace, key, value) ⇒ Object
Set a single argument in this namespace.
- #set_openid_namespace(openid_ns_uri, implicit) ⇒ Object
-
#to_args ⇒ Object
Return all namespaced arguments, failing if any non-namespaced arguments exist.
-
#to_form_markup(action_url, form_tag_attrs = nil, submit_text = "Continue") ⇒ Object
Generate HTML form markup that contains the values in this message, to be HTTP POSTed as x-www-form-urlencoded UTF-8.
-
#to_kvform ⇒ Object
Generate a KVForm string that contains the parameters in this message.
-
#to_post_args ⇒ Object
Return all arguments with “openid.” in from of namespaced arguments.
-
#to_url(base_url) ⇒ Object
Generate a GET URL with the paramters in this message attacked as query parameters.
-
#to_url_encoded ⇒ Object
Generate an x-www-urlencoded string.
-
#update_args(namespace, updates) ⇒ Object
Set multiple key/value pairs in one call.
Constructor Details
#initialize(openid_namespace = nil) ⇒ Message
Raises InvalidNamespaceError if you try to instantiate a Message with a namespace not in the above allowed list
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openid/message.rb', line 111 def initialize(openid_namespace = nil) @args = {} @namespaces = NamespaceMap.new if openid_namespace implicit = OPENID1_NAMESPACES.member?(openid_namespace) set_openid_namespace(openid_namespace, implicit) else @openid_ns_uri = nil end end |
Instance Attribute Details
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
77 78 79 |
# File 'lib/openid/message.rb', line 77 def namespaces @namespaces end |
Class Method Details
.from_http_response(response, server_url) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/openid/kvpost.rb', line 35 def self.from_http_response(response, server_url) msg = from_kvform(response.body) case response.code.to_i when 200 msg when 206 msg when 400 raise ServerError.(msg) else = "bad status code from server #{server_url}: " \ "#{response.code}" raise HTTPStatusError.new() end end |
.from_kvform(kvform_string) ⇒ Object
Create a message from a KVForm string
225 226 227 |
# File 'lib/openid/message.rb', line 225 def self.from_kvform(kvform_string) Message.from_openid_args(Util.kv_to_dict(kvform_string)) end |
.from_openid_args(openid_args) ⇒ Object
Construct a Message from a parsed KVForm message. Raises InvalidNamespaceError if you try to instantiate a Message with a namespace not in the above allowed list
150 151 152 153 154 |
# File 'lib/openid/message.rb', line 150 def self.from_openid_args(openid_args) m = Message.new m._from_openid_args(openid_args) m end |
.from_post_args(args) ⇒ Object
Construct a Message containing a set of POST arguments. Raises InvalidNamespaceError if you try to instantiate a Message with a namespace not in the above allowed list
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/openid/message.rb', line 125 def self.from_post_args(args) m = Message.new openid_args = {} args.each do |key, value| if value.is_a?(Array) raise ArgumentError, "Query dict must have one value for each key, " + "not lists of values. Query is #{args.inspect}" end prefix, rest = key.split(".", 2) if prefix != "openid" or rest.nil? m.set_arg(BARE_NS, key, value) else openid_args[rest] = value end end m._from_openid_args(openid_args) m end |
.register_namespace_alias(namespace_uri, alias_) ⇒ Object
Registers a (namespace URI, alias) mapping in a global namespace alias map. Raises NamespaceAliasRegistrationError if either the namespace URI or alias has already been registered with a different value. This function is required if you want to use a namespace with an OpenID 1 message.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/openid/message.rb', line 91 def self.register_namespace_alias(namespace_uri, alias_) return if @@registered_aliases[alias_] == namespace_uri if @@registered_aliases.values.include?(namespace_uri) raise NamespaceAliasRegistrationError, 'Namespace uri #{namespace_uri} already registered' end if @@registered_aliases.member?(alias_) raise NamespaceAliasRegistrationError, 'Alias #{alias_} already registered' end @@registered_aliases[alias_] = namespace_uri end |
Instance Method Details
#==(other) ⇒ Object
421 422 423 |
# File 'lib/openid/message.rb', line 421 def ==(other) other.is_a?(self.class) && @args == other.instance_eval { @args } end |
#_fix_ns(namespace) ⇒ Object
Convert an input value into the internally used values of this obejct.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/openid/message.rb', line 326 def _fix_ns(namespace) if namespace == OPENID_NS raise UndefinedOpenIDNamespace, "OpenID namespace not set" unless @openid_ns_uri namespace = @openid_ns_uri end return namespace if namespace == BARE_NS unless namespace.is_a?(String) raise ArgumentError, "Namespace must be BARE_NS, OPENID_NS or " \ "a string. Got #{namespace.inspect}" end if namespace.index(":").nil? msg = "OpenID 2.0 namespace identifiers SHOULD be URIs. " \ "Got #{namespace.inspect}" Util.log(msg) if namespace == "sreg" msg = "Using #{SREG_URI} instead of \"sreg\" as namespace" Util.log(msg) return SREG_URI end end namespace end |
#_from_openid_args(openid_args) ⇒ Object
Raises InvalidNamespaceError if you try to instantiate a Message with a namespace not in the above allowed list
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/openid/message.rb', line 158 def _from_openid_args(openid_args) ns_args = [] # resolve namespaces openid_args.each do |rest, value| ns_alias, ns_key = rest.split(".", 2) if ns_key.nil? ns_alias = NULL_NAMESPACE ns_key = rest end if ns_alias == "ns" @namespaces.add_alias(value, ns_key) elsif ns_alias == NULL_NAMESPACE and ns_key == "ns" set_openid_namespace(value, false) else ns_args << [ns_alias, ns_key, value] end end # implicitly set an OpenID 1 namespace set_openid_namespace(OPENID1_NS, true) unless get_openid_namespace # put the pairs into the appropriate namespaces ns_args.each do |ns_alias, ns_key, value| ns_uri = @namespaces.get_namespace_uri(ns_alias) unless ns_uri ns_uri = _get_default_namespace(ns_alias) if ns_uri @namespaces.add_alias(ns_uri, ns_alias, true) else ns_uri = get_openid_namespace ns_key = "#{ns_alias}.#{ns_key}" end end set_arg(ns_uri, ns_key, value) end end |
#_get_default_namespace(mystery_alias) ⇒ Object
197 198 199 200 201 |
# File 'lib/openid/message.rb', line 197 def _get_default_namespace(mystery_alias) # only try to map an alias to a default if it's an # OpenID 1.x namespace @@registered_aliases[mystery_alias] if is_openid1 end |
#copy ⇒ Object
229 230 231 |
# File 'lib/openid/message.rb', line 229 def copy Marshal.load(Marshal.dump(self)) end |
#del_arg(namespace, key) ⇒ Object
Remove a single argument from this namespace.
415 416 417 418 419 |
# File 'lib/openid/message.rb', line 415 def del_arg(namespace, key) namespace = _fix_ns(namespace) _key = [namespace, key] @args.delete(_key) end |
#get_aliased_arg(aliased_key, default = nil) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/openid/message.rb', line 425 def get_aliased_arg(aliased_key, default = nil) return get_openid_namespace if aliased_key == "ns" ns_alias, key = aliased_key.split(".", 2) if ns_alias == "ns" uri = @namespaces.get_namespace_uri(key) return (uri.nil? ? default : uri) unless uri.nil? and default == NO_DEFAULT raise KeyNotFound, "Namespace #{key} not defined when looking " \ "for #{aliased_key}" end if key.nil? key = aliased_key ns = nil else ns = @namespaces.get_namespace_uri(ns_alias) end if ns.nil? key = aliased_key ns = get_openid_namespace end get_arg(ns, key, default) end |
#get_arg(namespace, key, default = nil) ⇒ Object
Get a value for a namespaced key.
381 382 383 384 385 386 387 388 |
# File 'lib/openid/message.rb', line 381 def get_arg(namespace, key, default = nil) namespace = _fix_ns(namespace) @args.fetch([namespace, key]) do raise KeyNotFound, "<#{namespace}>#{key} not in this message" if default == NO_DEFAULT default end end |
#get_args(namespace) ⇒ Object
Get the arguments that are defined for this namespace URI.
391 392 393 394 395 396 397 398 399 |
# File 'lib/openid/message.rb', line 391 def get_args(namespace) namespace = _fix_ns(namespace) args = {} @args.each do |k, v| pair_ns, ns_key = k args[ns_key] = v if pair_ns == namespace end args end |
#get_key(namespace, ns_key) ⇒ Object
Get the key for a particular namespaced argument
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/openid/message.rb', line 362 def get_key(namespace, ns_key) namespace = _fix_ns(namespace) return ns_key if namespace == BARE_NS ns_alias = @namespaces.get_alias(namespace) # no alias is defined, so no key can exist return if ns_alias.nil? tail = if ns_alias == NULL_NAMESPACE ns_key else "#{ns_alias}.#{ns_key}" end "openid." + tail end |
#get_openid_namespace ⇒ Object
212 213 214 |
# File 'lib/openid/message.rb', line 212 def get_openid_namespace @openid_ns_uri end |
#has_key?(namespace, ns_key) ⇒ Boolean
356 357 358 359 |
# File 'lib/openid/message.rb', line 356 def has_key?(namespace, ns_key) namespace = _fix_ns(namespace) @args.member?([namespace, ns_key]) end |
#is_openid1 ⇒ Object
216 217 218 |
# File 'lib/openid/message.rb', line 216 def is_openid1 OPENID1_NAMESPACES.member?(@openid_ns_uri) end |
#is_openid2 ⇒ Object
220 221 222 |
# File 'lib/openid/message.rb', line 220 def is_openid2 @openid_ns_uri == OPENID2_NS end |
#set_arg(namespace, key, value) ⇒ Object
Set a single argument in this namespace
408 409 410 411 412 |
# File 'lib/openid/message.rb', line 408 def set_arg(namespace, key, value) namespace = _fix_ns(namespace) @args[[namespace, key].freeze] = value @namespaces.add(namespace) if namespace != BARE_NS end |
#set_openid_namespace(openid_ns_uri, implicit) ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/openid/message.rb', line 203 def set_openid_namespace(openid_ns_uri, implicit) unless @@allowed_openid_namespaces.include?(openid_ns_uri) raise InvalidOpenIDNamespace, "Invalid null namespace: #{openid_ns_uri}" end @namespaces.add_alias(openid_ns_uri, NULL_NAMESPACE, implicit) @openid_ns_uri = openid_ns_uri end |
#to_args ⇒ Object
Return all namespaced arguments, failing if any non-namespaced arguments exist.
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/openid/message.rb', line 260 def to_args post_args = to_post_args kvargs = {} post_args.each do |k, v| if !k.start_with?("openid.") raise ArgumentError, "This message can only be encoded as a POST, because it contains arguments that are not prefixed with 'openid.'" else kvargs[k[7..-1]] = v end end kvargs end |
#to_form_markup(action_url, form_tag_attrs = nil, submit_text = "Continue") ⇒ Object
Generate HTML form markup that contains the values in this message, to be HTTP POSTed as x-www-form-urlencoded UTF-8.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/openid/message.rb', line 276 def to_form_markup(action_url, form_tag_attrs = nil, submit_text = "Continue") form_tag_attr_map = {} if form_tag_attrs form_tag_attrs.each do |name, attr| form_tag_attr_map[name] = attr end end form_tag_attr_map["action"] = action_url form_tag_attr_map["method"] = "post" form_tag_attr_map["accept-charset"] = "UTF-8" form_tag_attr_map["enctype"] = "application/x-www-form-urlencoded" markup = "<form " form_tag_attr_map.each do |k, v| markup += " #{k}=\"#{v}\"" end markup += ">\n" to_post_args.each do |k, v| markup += "<input type='hidden' name='#{k}' value='#{OpenID::Util.html_encode(v)}' />\n" end markup += "<input type='submit' value='#{submit_text}' />\n" markup += "\n</form>" markup end |
#to_kvform ⇒ Object
Generate a KVForm string that contains the parameters in this message. This will fail is the message contains arguments outside of the “openid.” prefix.
315 316 317 |
# File 'lib/openid/message.rb', line 315 def to_kvform Util.dict_to_kv(to_args) end |
#to_post_args ⇒ Object
Return all arguments with “openid.” in from of namespaced arguments.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/openid/message.rb', line 234 def to_post_args args = {} # add namespace defs to the output @namespaces.each do |ns_uri, ns_alias| next if @namespaces.implicit?(ns_uri) ns_key = if ns_alias == NULL_NAMESPACE "openid.ns" else "openid.ns." + ns_alias end args[ns_key] = ns_uri end @args.each do |k, value| ns_uri, ns_key = k key = get_key(ns_uri, ns_key) args[key] = value end args end |
#to_url(base_url) ⇒ Object
Generate a GET URL with the paramters in this message attacked as query parameters.
308 309 310 |
# File 'lib/openid/message.rb', line 308 def to_url(base_url) Util.append_args(base_url, to_post_args) end |
#to_url_encoded ⇒ Object
Generate an x-www-urlencoded string.
320 321 322 323 |
# File 'lib/openid/message.rb', line 320 def to_url_encoded args = to_post_args.map.sort Util.urlencode(args) end |
#update_args(namespace, updates) ⇒ Object
Set multiple key/value pairs in one call.
402 403 404 405 |
# File 'lib/openid/message.rb', line 402 def update_args(namespace, updates) namespace = _fix_ns(namespace) updates.each { |k, v| set_arg(namespace, k, v) } end |