diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..bbfd55fa --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - 1.9.2 + - 1.9.3 +script: bundle exec rake test diff --git a/INSTALL b/INSTALL index 89f1b9bd..3c67d5cb 100644 --- a/INSTALL +++ b/INSTALL @@ -32,16 +32,16 @@ Or, if you installed via rubygems: $> irb irb$> require "rubygems" => true - irb$> require_gem "ruby-openid" + irb$> require_gem "ruby-openid" => true == Run the test suite -Go into the test directory and execute the *runtests.rb* script. +Go into the admin directory and execute the *runtests.rb* script. == Next steps -* Run consumer.rb in the examples directory. +* Run consumer.rb in the examples directory. * Get started writing your own consumer using OpenID::Consumer * Write your own server with OpenID::Server * Use the OpenIDLoginGenerator! Read example/README for more info. diff --git a/README b/README.md similarity index 62% rename from README rename to README.md index 2b19a07a..906d56bd 100644 --- a/README +++ b/README.md @@ -1,45 +1,46 @@ -=Ruby OpenID +# Ruby OpenID + +[![Build Status](https://secure.travis-ci.org/joe1chen/ruby-openid.png)](http://travis-ci.org/joe1chen/ruby-openid) A Ruby library for verifying and serving OpenID identities. -This is a fork by courtenay @ entp (http://entp.com) which merges in many pull requests, +This is a fork by courtenay @ entp (http://entp.com) and joe1chen which merges in many pull requests, fixes some tests and works on ruby 1.8.7 and ruby 1.9.2 (at least). This library seems otherwise abandoned by its authors. -==Features -* Easy to use API for verifying OpenID identites - OpenID::Consumer -* Support for serving OpenID identites - OpenID::Server -* Does not depend on underlying web framework -* Supports multiple storage mechanisms (Filesystem, ActiveRecord, Memory) -* Example code to help you get started, including: - * Ruby on Rails based consumer and server - * OpenIDLoginGenerator for quickly getting creating a rails app that uses - OpenID for authentication - * ActiveRecordOpenIDStore plugin -* Comprehensive test suite -* Supports both OpenID 1 and OpenID 2 transparently - -==Installing +## Features + * Easy to use API for verifying OpenID identites - OpenID::Consumer + * Support for serving OpenID identites - OpenID::Server + * Does not depend on underlying web framework + * Supports multiple storage mechanisms (Filesystem, ActiveRecord, Memory) + * Example code to help you get started, including: + * Ruby on Rails based consumer and server + * OpenIDLoginGenerator for quickly getting creating a rails app that uses OpenID for authentication + * ActiveRecordOpenIDStore plugin + * Comprehensive test suite + * Supports both OpenID 1 and OpenID 2 transparently + +## Installing Before running the examples or writing your own code you'll need to install the library. See the INSTALL file or use rubygems: - gem install ruby-openid + gem install ruby-openid Check the installation: - $ irb - irb> require 'rubygems' - irb> require_gem 'ruby-openid' - => true + $ irb + irb> require 'rubygems' + irb> require_gem 'ruby-openid' + => true The library is known to work with Ruby 1.8.4 on Unix, Max OSX and Win32. Examples have been tested with Rails 1.1 and 1.2, and 2.0. -==Getting Started +## Getting Started The best way to start is to look at the rails_openid example. You can run it with: - cd examples/rails_openid - script/server + cd examples/rails_openid + script/server If you are writing an OpenID Relying Party, a good place to start is: examples/rails_openid/app/controllers/consumer_controller.rb @@ -51,13 +52,13 @@ The library code is quite well documented, so don't be squeamish, and look at the library itself if there's anything you don't understand in the examples. -==Homepage +## Homepage http://github.com/openid/ruby-openid See also: http://openid.net/ -==Community +## Community Discussion regarding the Ruby OpenID library and other JanRain OpenID libraries takes place on the the OpenID mailing list on openid.net. @@ -74,12 +75,12 @@ contribute, see http://openidenabled.com/contribute/ -==Author +## Author Copyright 2006-2008, JanRain, Inc. Contact openid@janrain.com or visit the OpenID channel on pibb.com: http://pibb.com/go/openid -==License +## License Apache Software License. For more information see the LICENSE file. diff --git a/admin/library-name b/admin/library-name index cd1d7040..062173a3 100644 --- a/admin/library-name +++ b/admin/library-name @@ -1 +1 @@ -entp-ruby-openid \ No newline at end of file +ruby-openid \ No newline at end of file diff --git a/entp-ruby-openid.gemspec b/entp-ruby-openid.gemspec deleted file mode 100644 index b12b61db..00000000 --- a/entp-ruby-openid.gemspec +++ /dev/null @@ -1,21 +0,0 @@ -require 'rubygems' - -SPEC = Gem::Specification.new do |s| - s.name = `cat admin/library-name`.strip -# s.version = `darcs changes --tags= | awk '$1 == "tagged" { print $2 }' | head -n 1`.strip - s.version = '2.2' - s.author = 'ENTP' - s.email = 'courtenay@entp.com' - s.homepage = 'http://github.com/entp/ruby-openid' - s.platform = Gem::Platform::RUBY - s.summary = 'A library for consuming and serving OpenID identities. Forked from the unmaintained JanRain version.' - files = Dir.glob("{examples,lib,test}/**/*") - files << 'NOTICE' << 'CHANGELOG' - s.files = files.delete_if {|f| f.include?('_darcs') || f.include?('admin')} - s.require_path = 'lib' - s.autorequire = 'openid' - s.test_file = 'admin/runtests.rb' - s.has_rdoc = true - s.extra_rdoc_files = ['README','INSTALL','LICENSE','UPGRADE'] - s.rdoc_options << '--main' << 'README' -end diff --git a/examples/active_record_openid_store/README b/examples/active_record_openid_store/README index 11787298..9d15c62f 100644 --- a/examples/active_record_openid_store/README +++ b/examples/active_record_openid_store/README @@ -44,9 +44,11 @@ You may garbage collect unused nonces and expired associations using the gc instance method of ActiveRecordOpenIDStore. Hook it up to a task in your app's Rakefile like so: - desc 'GC OpenID store' + desc 'GC OpenID store, deleting expired nonces and associations' task :gc_openid_store => :environment do - ActiveRecordOpenIDStore.new.cleanup + require 'openid_ar_store' + nonces, associations = ActiveRecordStore.new.cleanup + puts "Deleted #{nonces} nonces, #{associations} associations" end Run it by typing: diff --git a/examples/active_record_openid_store/lib/openid_ar_store.rb b/examples/active_record_openid_store/lib/openid_ar_store.rb index 276569c5..c2436744 100644 --- a/examples/active_record_openid_store/lib/openid_ar_store.rb +++ b/examples/active_record_openid_store/lib/openid_ar_store.rb @@ -51,7 +51,7 @@ def cleanup_nonces def cleanup_associations now = Time.now.to_i - Association.delete_all(['issued + lifetime > ?',now]) + Association.delete_all(['issued + lifetime < ?',now]) end end diff --git a/lib/hmac/hmac.rb b/lib/hmac/hmac.rb deleted file mode 100644 index e8bfa42b..00000000 --- a/lib/hmac/hmac.rb +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright (C) 2001 Daiki Ueno -# This library is distributed under the terms of the Ruby license. - -# This module provides common interface to HMAC engines. -# HMAC standard is documented in RFC 2104: -# -# H. Krawczyk et al., "HMAC: Keyed-Hashing for Message Authentication", -# RFC 2104, February 1997 -# -# These APIs are inspired by JCE 1.2's javax.crypto.Mac interface. -# -# - -module HMAC - class Base - def initialize(algorithm, block_size, output_length, key) - @algorithm = algorithm - @block_size = block_size - @output_length = output_length - @status = STATUS_UNDEFINED - @key_xor_ipad = '' - @key_xor_opad = '' - set_key(key) unless key.nil? - end - - private - def check_status - unless @status == STATUS_INITIALIZED - raise RuntimeError, - "The underlying hash algorithm has not yet been initialized." - end - end - - public - def set_key(key) - # If key is longer than the block size, apply hash function - # to key and use the result as a real key. - key = @algorithm.digest(key) if key.size > @block_size - key_xor_ipad = "\x36" * @block_size - key_xor_opad = "\x5C" * @block_size - for i in 0 .. key.size - 1 - key_xor_ipad[i] ^= key[i] - key_xor_opad[i] ^= key[i] - end - @key_xor_ipad = key_xor_ipad - @key_xor_opad = key_xor_opad - @md = @algorithm.new - @status = STATUS_INITIALIZED - end - - def reset_key - @key_xor_ipad.gsub!(/./, '?') - @key_xor_opad.gsub!(/./, '?') - @key_xor_ipad[0..-1] = '' - @key_xor_opad[0..-1] = '' - @status = STATUS_UNDEFINED - end - - def update(text) - check_status - # perform inner H - md = @algorithm.new - md.update(@key_xor_ipad) - md.update(text) - str = md.digest - # perform outer H - md = @algorithm.new - md.update(@key_xor_opad) - md.update(str) - @md = md - end - alias << update - - def digest - check_status - @md.digest - end - - def hexdigest - check_status - @md.hexdigest - end - alias to_s hexdigest - - # These two class methods below are safer than using above - # instance methods combinatorially because an instance will have - # held a key even if it's no longer in use. - def Base.digest(key, text) - begin - hmac = self.new(key) - hmac.update(text) - hmac.digest - ensure - hmac.reset_key - end - end - - def Base.hexdigest(key, text) - begin - hmac = self.new(key) - hmac.update(text) - hmac.hexdigest - ensure - hmac.reset_key - end - end - - private_class_method :new, :digest, :hexdigest - end - - STATUS_UNDEFINED, STATUS_INITIALIZED = 0, 1 -end diff --git a/lib/hmac/sha1.rb b/lib/hmac/sha1.rb deleted file mode 100644 index d2f0088a..00000000 --- a/lib/hmac/sha1.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'hmac/hmac' -require 'digest/sha1' - -module HMAC - class SHA1 < Base - def initialize(key = nil) - super(Digest::SHA1, 64, 20, key) - end - public_class_method :new, :digest, :hexdigest - end -end diff --git a/lib/hmac/sha2.rb b/lib/hmac/sha2.rb deleted file mode 100644 index 0412ba40..00000000 --- a/lib/hmac/sha2.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'hmac/hmac' -require 'digest/sha2' - -module HMAC - class SHA256 < Base - def initialize(key = nil) - super(Digest::SHA256, 64, 32, key) - end - public_class_method :new, :digest, :hexdigest - end - - class SHA384 < Base - def initialize(key = nil) - super(Digest::SHA384, 128, 48, key) - end - public_class_method :new, :digest, :hexdigest - end - - class SHA512 < Base - def initialize(key = nil) - super(Digest::SHA512, 128, 64, key) - end - public_class_method :new, :digest, :hexdigest - end -end diff --git a/lib/openid.rb b/lib/openid.rb index ccb3ad91..2e1d03e5 100644 --- a/lib/openid.rb +++ b/lib/openid.rb @@ -14,9 +14,5 @@ require "openid/version" require 'openid/store' -require 'openid/yadis' require "openid/consumer" -require 'openid/server' - -module OpenID -end +require 'openid/server' \ No newline at end of file diff --git a/lib/openid/consumer/idres.rb b/lib/openid/consumer/idres.rb index 62d62643..8ef9e384 100644 --- a/lib/openid/consumer/idres.rb +++ b/lib/openid/consumer/idres.rb @@ -492,10 +492,10 @@ def verify_discovery_single(endpoint, to_match) "#{endpoint.claimed_id}") end - if to_match.claimed_id != endpoint.claimed_id - raise ProtocolError, ("claimed_id mismatch. Expected "\ - "#{to_match.claimed_id}, got "\ - "#{endpoint.claimed_id}") + if to_match.get_local_id != endpoint.get_local_id + raise ProtocolError, ("local_id mismatch. Expected "\ + "#{to_match.get_local_id}, got "\ + "#{endpoint.get_local_id}") end # If the server URL is nil, this must be an OpenID 1 @@ -512,7 +512,7 @@ def verify_discovery_single(endpoint, to_match) "`to_match' endpoint." end elsif to_match.server_url != endpoint.server_url - raise ProtocolError, ("OP Endpoint mismatch. Expected "\ + raise ProtocolError, ("OP Endpoint mismatch. Expected"\ "#{to_match.server_url}, got "\ "#{endpoint.server_url}") end diff --git a/lib/openid/cryptutil.rb b/lib/openid/cryptutil.rb old mode 100644 new mode 100755 index 2ae19e1d..7605b8c5 --- a/lib/openid/cryptutil.rb +++ b/lib/openid/cryptutil.rb @@ -1,19 +1,7 @@ require "openid/util" require "digest/sha1" require "digest/sha2" -begin - require "digest/hmac" -rescue LoadError - begin - # Try loading the ruby-hmac files if they exist - require "hmac-sha1" - require "hmac-sha2" - rescue LoadError - # Nothing exists use included hmac files - require "hmac/sha1" - require "hmac/sha2" - end -end +require "openssl" module OpenID # This module contains everything needed to perform low-level @@ -37,8 +25,10 @@ def CryptUtil.sha1(text) end def CryptUtil.hmac_sha1(key, text) - if Digest.const_defined? :HMAC - Digest::HMAC.new(key,Digest::SHA1).update(text).digest + if OpenSSL.const_defined? :HMAC + OpenSSL::HMAC.new(key, OpenSSL::Digest.new('sha1')).update(text).digest + elsif Digest.const_defined? :HMAC + Digest::HMAC.new(key, Digest::SHA1).update(text).digest else return HMAC::SHA1.digest(key, text) end @@ -49,8 +39,10 @@ def CryptUtil.sha256(text) end def CryptUtil.hmac_sha256(key, text) - if Digest.const_defined? :HMAC - Digest::HMAC.new(key,Digest::SHA256).update(text).digest + if OpenSSL.const_defined? :HMAC + OpenSSL::HMAC.new(key, OpenSSL::Digest.new('sha256')).update(text).digest + elsif Digest.const_defined? :HMAC + Digest::HMAC.new(key, Digest::SHA256).update(text).digest else return HMAC::SHA256.digest(key, text) end diff --git a/lib/openid/extensions/ui.rb b/lib/openid/extensions/ui.rb new file mode 100644 index 00000000..6c156d7f --- /dev/null +++ b/lib/openid/extensions/ui.rb @@ -0,0 +1,53 @@ +# An implementation of the OpenID User Interface Extension 1.0 - DRAFT 0.5 +# see: http://svn.openid.net/repos/specifications/user_interface/1.0/trunk/openid-user-interface-extension-1_0.html + +require 'openid/extension' + +module OpenID + + module UI + NS_URI = "http://specs.openid.net/extensions/ui/1.0" + + class Request < Extension + attr_accessor :lang, :icon, :mode, :ns_alias, :ns_uri + def initialize(mode = nil, icon = nil, lang = nil) + @ns_alias = 'ui' + @ns_uri = NS_URI + @lang = lang + @icon = icon + @mode = mode + end + + def get_extension_args + ns_args = {} + ns_args['lang'] = @lang if @lang + ns_args['icon'] = @icon if @icon + ns_args['mode'] = @mode if @mode + return ns_args + end + + # Instantiate a Request object from the arguments in a + # checkid_* OpenID message + # return nil if the extension was not requested. + def self.from_openid_request(oid_req) + oauth_req = new + args = oid_req.message.get_args(NS_URI) + if args == {} + return nil + end + oauth_req.parse_extension_args(args) + return oauth_req + end + + # Set UI extension parameters + def parse_extension_args(args) + @lang = args["lang"] + @icon = args["icon"] + @mode = args["mode"] + end + + end + + end + +end diff --git a/lib/openid/extras.rb b/lib/openid/extras.rb index 0d9560ab..5f4f93aa 100644 --- a/lib/openid/extras.rb +++ b/lib/openid/extras.rb @@ -1,11 +1,12 @@ class String - def starts_with?(other) + define_method :starts_with? do |other| head = self[0, other.length] head == other - end + end unless new.respond_to?(:starts_with?) - def ends_with?(other) + define_method :ends_with? do |other| tail = self[-1 * other.length, other.length] tail == other - end + end unless new.respond_to?(:ends_with?) end + diff --git a/lib/openid/fetchers.rb b/lib/openid/fetchers.rb index 26a0e936..d494e4be 100644 --- a/lib/openid/fetchers.rb +++ b/lib/openid/fetchers.rb @@ -1,5 +1,5 @@ require 'net/http' -require 'openid' +require 'openid/version' require 'openid/util' begin diff --git a/lib/openid/message.rb b/lib/openid/message.rb index c494f469..282497bd 100644 --- a/lib/openid/message.rb +++ b/lib/openid/message.rb @@ -278,6 +278,7 @@ def to_form_markup(action_url, form_tag_attrs=nil, submit_text='Continue') form_tag_attr_map['method'] = 'post' form_tag_attr_map['accept-charset'] = 'UTF-8' form_tag_attr_map['enctype'] = 'application/x-www-form-urlencoded' + form_tag_attr_map['id'] = OpenID::Util::HTML_FORM_ID markup = "
association.lifetime.seconds.to_i) + @cache_client.set(key, serialized, expiry(association.lifetime)) end end @@ -30,7 +30,7 @@ def store_association(server_url, association) # the one matching association is expired. (Is allowed to GC expired # associations when found.) def get_association(server_url, handle=nil) - serialized = @cache_client.read(assoc_key(server_url, handle)) + serialized = @cache_client.get(assoc_key(server_url, handle)) if serialized return deserialize(serialized) else @@ -62,9 +62,12 @@ def use_nonce(server_url, timestamp, salt) return false if (timestamp - Time.now.to_i).abs > Nonce.skew ts = timestamp.to_s # base 10 seconds since epoch nonce_key = key_prefix + 'N' + server_url + '|' + ts + '|' + salt - result = @cache_client.read(nonce_key) - @cache_client.write(nonce_key, nonce_key, :expires_in => (Nonce.skew() + 5)) - result.nil? + result = @cache_client.add(nonce_key, '', expiry(Nonce.skew + 5)) + if result.is_a? String + return !!(result =~ /^STORED/) + else + return result == true + end end def assoc_key(server_url, assoc_handle=nil) @@ -87,9 +90,12 @@ def cleanup_associations protected def delete(key) - # result = @cache_client.delete(key) # memcached delete seems to be broken - # return !!(result =~ /^DELETED/) - @cache_client.write(key, nil, :expires_in => 0) + result = @cache_client.delete(key) + if result.is_a? String + return !!(result =~ /^DELETED/) + else + return result == true + end end def serialize(assoc) diff --git a/lib/openid/util.rb b/lib/openid/util.rb index 3c2bdc03..40d84c7f 100644 --- a/lib/openid/util.rb +++ b/lib/openid/util.rb @@ -29,6 +29,8 @@ module Util [#{BASE64_CHARS}]{3}=)? \\Z", Regexp::EXTENDED) + HTML_FORM_ID = 'openid_transaction_in_progress' + def Util.assert(value, message=nil) if not value raise AssertionError, message or value @@ -74,39 +76,37 @@ def Util.append_args(url, args) url << Util.urlencode(args) end - @@logger = Logger.new(STDERR) - @@logger.progname = "OpenID" - def Util.logger=(logger) @@logger = logger end def Util.logger - @@logger + @@logger ||= Logger.new(STDERR, { :progname => 'OpenID' }) end # change the message below to do whatever you like for logging def Util.log(message) - logger.info(message) + Util.logger.info(message) end def Util.auto_submit_html(form, title='OpenID transaction in progress') - return " - - - #{title} - - -#{form} - - - -" + return " + + #{title} + + + + + #{form} + + " end ESCAPE_TABLE = { '&' => '&', '<' => '<', '>' => '>', '"' => '"', "'" => ''' } diff --git a/lib/openid/version.rb b/lib/openid/version.rb index fc57f357..8ea7c16f 100644 --- a/lib/openid/version.rb +++ b/lib/openid/version.rb @@ -1,3 +1,3 @@ module OpenID - VERSION = "2.1.9" + VERSION = "2.1.9.6" end diff --git a/lib/openid/yadis/xri.rb b/lib/openid/yadis/xri.rb index 89dd99af..d0c0ff4f 100644 --- a/lib/openid/yadis/xri.rb +++ b/lib/openid/yadis/xri.rb @@ -1,4 +1,3 @@ -require 'openid/yadis/xrds' require 'openid/fetchers' module OpenID diff --git a/ruby-openid.gemspec b/ruby-openid.gemspec new file mode 100644 index 00000000..b3222fea --- /dev/null +++ b/ruby-openid.gemspec @@ -0,0 +1,21 @@ +$:.push File.expand_path("../lib", __FILE__) +require 'openid/version' + +spec = Gem::Specification.new do |s| + s.name = 'ruby-openid' + s.version = OpenID::VERSION + s.author = 'JanRain, Inc' + s.email = 'openid@janrain.com' + s.homepage = 'http://github.com/openid/ruby-openid' + s.platform = Gem::Platform::RUBY + s.summary = 'A library for consuming and serving OpenID identities.' + files = Dir.glob("{examples,lib,test}/**/*") + files << 'NOTICE' << 'CHANGELOG' + s.files = files.delete_if {|f| f.include?('_darcs') || f.include?('admin')} + s.require_path = 'lib' + s.autorequire = 'openid' + s.test_file = 'admin/runtests.rb' + + s.add_development_dependency("rake") + s.add_development_dependency("test-unit") +end diff --git a/test/support/yadis_data/trustroot.txt b/test/support/yadis_data/trustroot.txt index 73681657..5947cea2 100644 --- a/test/support/yadis_data/trustroot.txt +++ b/test/support/yadis_data/trustroot.txt @@ -3,7 +3,7 @@ Trust root parsing checking ======================================== ---------------------------------------- -23: Does not parse +21: Does not parse ---------------------------------------- baz.org *.foo.com @@ -16,8 +16,6 @@ http://foo.*.com http://www.* http://*foo.com/ http://foo.com/invalid#fragment -http://..it/ -http://.it/ http://*:8081/ http://*:80 http://localhost:1900foo/ @@ -30,7 +28,7 @@ http://lambda.com/Λ 5 ---------------------------------------- -14: Insane +16: Insane ---------------------------------------- http:/// http://*/ @@ -46,6 +44,8 @@ http://*.museum/ https://*.museum/ http://www.schtuffcom/ http://it/ +http://..it/ +http://.it/ ---------------------------------------- 18: Sane diff --git a/test/test_discover.rb b/test/test_discover.rb index 00af984b..365d21f1 100644 --- a/test/test_discover.rb +++ b/test/test_discover.rb @@ -529,7 +529,7 @@ def fetch(url, body=nil, headers=nil, limit=nil) end begin - ctype, body = @documents.fetch(xri) + ctype, body = @documents.fetch(URI::unescape(xri)) rescue IndexError status = 404 ctype = 'text/plain' @@ -548,19 +548,21 @@ class TestXRIDiscovery < BaseTestDiscovery include TestDataMixin include TestUtil + TEST_INAME = "=smoker" + def initialize(*args) super(*args) @fetcher_class = MockFetcherForXRIProxy - @documents = {'=smoker' => ['application/xrds+xml', + @documents = {TEST_INAME => ['application/xrds+xml', read_data_file('test_discover/yadis_2entries_delegate.xml', false)], - '=smoker*bad' => ['application/xrds+xml', + "#{TEST_INAME}*bad" => ['application/xrds+xml', read_data_file('test_discover/yadis_another_delegate.xml', false)]} end def test_xri - user_xri, services = OpenID.discover_xri('=smoker') + user_xri, services = OpenID.discover_xri(TEST_INAME) _checkService(services[0], "http://www.myopenid.com/server", @@ -569,7 +571,7 @@ def test_xri Yadis::XRI.make_xri("=!1000"), ['1.0'], true, - '=smoker') + TEST_INAME) _checkService(services[1], "http://www.livejournal.com/openid/server.bml", @@ -578,11 +580,11 @@ def test_xri Yadis::XRI.make_xri("=!1000"), ['1.0'], true, - '=smoker') + TEST_INAME) end def test_xri_normalize - user_xri, services = OpenID.discover_xri('xri://=smoker') + user_xri, services = OpenID.discover_xri("xri://#{TEST_INAME}") _checkService(services[0], "http://www.myopenid.com/server", @@ -591,7 +593,7 @@ def test_xri_normalize Yadis::XRI.make_xri("=!1000"), ['1.0'], true, - '=smoker') + TEST_INAME) _checkService(services[1], "http://www.livejournal.com/openid/server.bml", @@ -600,12 +602,12 @@ def test_xri_normalize Yadis::XRI.make_xri("=!1000"), ['1.0'], true, - '=smoker') + TEST_INAME) end def test_xriNoCanonicalID silence_logging { - user_xri, services = OpenID.discover_xri('=smoker*bad') + user_xri, services = OpenID.discover_xri("#{TEST_INAME}*bad") assert(services.empty?) } end @@ -623,17 +625,19 @@ def test_useCanonicalID class TestXRIDiscoveryIDP < BaseTestDiscovery include TestDataMixin + TEST_INAME = "=smoker" + def initialize(*args) super(*args) @fetcher_class = MockFetcherForXRIProxy - @documents = {'=smoker' => ['application/xrds+xml', + @documents = {TEST_INAME => ['application/xrds+xml', read_data_file('test_discover/yadis_2entries_idp.xml', false)] } end def test_xri - user_xri, services = OpenID.discover_xri('=smoker') + user_xri, services = OpenID.discover_xri(TEST_INAME) assert(!services.empty?, "Expected services, got zero") assert_equal(services[0].server_url, "http://www.livejournal.com/openid/server.bml") diff --git a/test/test_message.rb b/test/test_message.rb index 412b8bf3..b84818ab 100644 --- a/test/test_message.rb +++ b/test/test_message.rb @@ -918,6 +918,7 @@ def setup 'accept-charset' => 'UTF-8', 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'post', + 'id' => Util::HTML_FORM_ID } end diff --git a/test/test_trustroot.rb b/test/test_trustroot.rb index 28e2b7b7..4b683d11 100644 --- a/test/test_trustroot.rb +++ b/test/test_trustroot.rb @@ -11,7 +11,7 @@ def _test_sanity(case_, sanity, desc) assert(tr.sane?) assert(OpenID::TrustRoot::TrustRoot.check_sanity(case_)) elsif sanity == 'insane' - assert(!tr.sane?) + assert(!tr || !tr.sane?) assert(!OpenID::TrustRoot::TrustRoot.check_sanity(case_)) else assert(tr.nil?, case_) diff --git a/test/test_ui.rb b/test/test_ui.rb new file mode 100644 index 00000000..e20227a4 --- /dev/null +++ b/test/test_ui.rb @@ -0,0 +1,93 @@ +require 'openid/extensions/ui' +require 'openid/message' +require 'openid/server' +require 'test/unit' + +module OpenID + module UITest + class UIRequestTestCase < Test::Unit::TestCase + + def setup + @req = UI::Request.new + end + + def test_construct + assert_nil @req.mode + assert_nil @req.icon + assert_nil @req.lang + assert_equal 'ui', @req.ns_alias + + req2 = UI::Request.new("popup", true, "ja-JP") + assert_equal "popup", req2.mode + assert_equal true, req2.icon + assert_equal "ja-JP", req2.lang + end + + def test_add_mode + @req.mode = "popup" + assert_equal "popup", @req.mode + end + + def test_add_icon + @req.icon = true + assert_equal true, @req.icon + end + + def test_add_lang + @req.lang = "ja-JP" + assert_equal "ja-JP", @req.lang + end + + def test_get_extension_args + assert_equal({}, @req.get_extension_args) + @req.mode = "popup" + assert_equal({'mode' => 'popup'}, @req.get_extension_args) + @req.icon = true + assert_equal({'mode' => 'popup', 'icon' => true}, @req.get_extension_args) + @req.lang = "ja-JP" + assert_equal({'mode' => 'popup', 'icon' => true, 'lang' => 'ja-JP'}, @req.get_extension_args) + end + + def test_parse_extension_args + args = {'mode' => 'popup', 'icon' => true, 'lang' => 'ja-JP'} + @req.parse_extension_args args + assert_equal "popup", @req.mode + assert_equal true, @req.icon + assert_equal "ja-JP", @req.lang + end + + def test_parse_extension_args_empty + @req.parse_extension_args({}) + assert_nil @req.mode + assert_nil @req.icon + assert_nil @req.lang + end + + def test_from_openid_request + openid_req_msg = Message.from_openid_args( + 'mode' => 'checkid_setup', + 'ns' => OPENID2_NS, + 'ns.ui' => UI::NS_URI, + 'ui.mode' => 'popup', + 'ui.icon' => true, + 'ui.lang' => 'ja-JP' + ) + oid_req = Server::OpenIDRequest.new + oid_req.message = openid_req_msg + req = UI::Request.from_openid_request oid_req + assert_equal "popup", req.mode + assert_equal true, req.icon + assert_equal "ja-JP", req.lang + end + + def test_from_openid_request_no_ui_params + message = Message.new + openid_req = Server::OpenIDRequest.new + openid_req.message = message + ui_req = UI::Request.from_openid_request openid_req + assert ui_req.nil? + end + + end + end +end diff --git a/test/test_util.rb b/test/test_util.rb index e19cf125..26565f75 100644 --- a/test/test_util.rb +++ b/test/test_util.rb @@ -140,6 +140,16 @@ def test_append_args() def test_parse_query assert_equal({'foo'=>'bar'}, Util.parse_query('foo=bar')) end + + def test_defines_html_form_id + assert Util::HTML_FORM_ID + end + + def test_auto_submit_html_looks_for_html_form_id_to_submit + auto_submit_html_output = Util.auto_submit_html('form_data') + assert auto_submit_html_output =~ Regexp.new(Regexp.escape("document.getElementById('#{Util::HTML_FORM_ID}')")) + assert auto_submit_html_output =~ Regexp.new(Regexp.escape("document.getElementById('#{Util::HTML_FORM_ID}').submit();")) + end end end