Skip to content

Commit

Permalink
Clean up previous commit: Use normalize_identifier, clean whitespace …
Browse files Browse the repository at this point in the history
…up and fix broken test.
  • Loading branch information
josh committed Dec 14, 2008
1 parent 13248aa commit 10764ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
31 changes: 15 additions & 16 deletions lib/open_id_authentication.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,43 +71,42 @@ def message
end end


# normalizes an OpenID according to http://openid.net/specs/openid-authentication-2_0.html#normalization # normalizes an OpenID according to http://openid.net/specs/openid-authentication-2_0.html#normalization
def self.normalize( open_id ) def self.normalize_identifier(identifier)
# clean up whitespace # clean up whitespace
open_id = open_id.to_s.strip identifier = identifier.to_s.strip

# if an XRI has a prefix, strip it. # if an XRI has a prefix, strip it.
open_id.gsub!(/xri:\/\//i, '') identifier.gsub!(/xri:\/\//i, '')

# dodge XRIs -- TODO: validate, don't just skip. # dodge XRIs -- TODO: validate, don't just skip.
unless ['=', '@', '+', '$', '!', '('].include? open_id[0].chr unless ['=', '@', '+', '$', '!', '('].include?(identifier.at(0))

# does it begin with http? if not, add it. # does it begin with http? if not, add it.
open_id = "http://#{open_id}" unless open_id =~ /^http/i identifier = "http://#{identifier}" unless identifier =~ /^http/i


# strip any fragments # strip any fragments
open_id.gsub!(/\#(.*)$/, '') identifier.gsub!(/\#(.*)$/, '')


begin begin
uri = URI.parse(open_id) uri = URI.parse(identifier)
uri.scheme = uri.scheme.downcase # URI should do this uri.scheme = uri.scheme.downcase # URI should do this
open_id = uri.normalize.to_s identifier = uri.normalize.to_s
rescue URI::InvalidURIError rescue URI::InvalidURIError
raise InvalidOpenId.new("#{url} is not an OpenID URL") raise InvalidOpenId.new("#{identifier} is not an OpenID identifier")
end end

end end


return open_id return identifier
end end


# deprecated for OpenID 2.0, where not all OpenIDs are URLs # deprecated for OpenID 2.0, where not all OpenIDs are URLs
def self.normalize_url(url) def self.normalize_url(url)
self.normalize( url ) ActiveSupport::Deprecation.warn "normalize_identifier has been deprecated, use normalize instead"
self.normalize_identifier(url)
end end


protected protected
def normalize_url(url) def normalize_url(url)
OpenIdAuthentication.normalize_url(url) OpenIdAuthentication.normalize_identifier(url)
end end


# The parameter name of "openid_identifier" is used rather than the Rails convention "open_id_identifier" # The parameter name of "openid_identifier" is used rather than the Rails convention "open_id_identifier"
Expand Down
6 changes: 3 additions & 3 deletions test/normalize_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class NormalizeTest < Test::Unit::TestCase
"http://loudthinking.com:8080" => "http://loudthinking.com:8080/", "http://loudthinking.com:8080" => "http://loudthinking.com:8080/",
"techno-weenie.net" => "http://techno-weenie.net/", "techno-weenie.net" => "http://techno-weenie.net/",
"http://techno-weenie.net" => "http://techno-weenie.net/", "http://techno-weenie.net" => "http://techno-weenie.net/",
"http://techno-weenie.net " => "http://techno-weenie.net/" "http://techno-weenie.net " => "http://techno-weenie.net/",
"=name" => "=name"
} }


def test_normalizations def test_normalizations
NORMALIZATIONS.each do |from, to| NORMALIZATIONS.each do |from, to|
assert_equal to, normalize_url(from) assert_equal to, normalize_url(from)
end end
end end

def test_broken_open_id def test_broken_open_id
assert_raises(InvalidOpenId) { normalize_url(nil) } assert_raises(InvalidOpenId) { normalize_url(nil) }
assert_raises(InvalidOpenId) { normalize_url("=name") }
end end
end end

0 comments on commit 10764ad

Please sign in to comment.