Skip to content

Commit

Permalink
Creating OpenIdAuthentication.normalize method to replace OpenIdAuthe…
Browse files Browse the repository at this point in the history
…ntication.normalize_url

* Changed the name to reflect that not all OpenIDs are URLs.
* Normalization now follows the rules for OpenID 2.0, allowing XRI as well as URL based OpenIDs.
* normalize_url calls normalize to preserve backwards compatibility.
  • Loading branch information
peat authored and josh committed Dec 14, 2008
1 parent 00d8bc7 commit 13248aa
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions lib/open_id_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,39 @@ def message
end
end

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

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

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

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

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

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

end

return open_id
end

# deprecated for OpenID 2.0, where not all OpenIDs are URLs
def self.normalize_url(url)
uri = URI.parse(url.to_s.strip)
uri = URI.parse("http://#{uri}") unless uri.scheme
uri.scheme = uri.scheme.downcase # URI should do this
uri.normalize.to_s
rescue URI::InvalidURIError
raise InvalidOpenId.new("#{url} is not an OpenID URL")
self.normalize( url )
end

protected
Expand Down

0 comments on commit 13248aa

Please sign in to comment.