Skip to content

Instantly share code, notes, and snippets.

@nov

nov/gist:5915858

Last active Dec 19, 2015
Embed
What would you like to do?
module OpenIDConnect
module Discovery
class URINormalizer
attr_accessor :input
def initialize(input)
self.input = input
end
def normalized
normalized = case
when has_scheme?
input
when acct_scheme_assumed?
"acct:#{input}"
else
"https://#{input}"
end
normalized.split('#').first # strip fragment
end
def has_scheme?
if input.include?("://")
true
else
before_path = input.split(/[\/\?#]/).first
if before_path.include?(':')
scheme_or_host, host_or_port = before_path.split(':')
host_or_port !~ /^\d+$/
else
false
end
end
end
def acct_scheme_assumed?
if input.include?('@')
host = input.split('@').last
!(
host.include?(':') ||
host.include?('/') ||
host.include?('?')
) # allowing fragment here?
else
false
end
end
end
end
end
[
'example.com',
'example.com:8080',
'example.com/path',
'example.com?query',
'example.com#fragment',
'example.com:8080/path?query#fragment',
'http://example.com',
'http://example.com:8080',
'http://example.com/path',
'http://example.com?query',
'http://example.com#fragment',
'http://example.com:8080/path?query#fragment',
'nov@example.com',
'nov@example.com:8080',
'nov@example.com/path',
'nov@example.com?query',
'nov@example.com#fragment',
'nov@example.com:8080/path?query#fragment',
'acct:nov@matake.jp',
'acct:nov@example.com:8080',
'acct:nov@example.com/path',
'acct:nov@example.com?query',
'acct:nov@example.com#fragment',
'acct:nov@example.com:8080/path?query#fragment',
'mailto:nov@matake.jp',
'mailto:nov@example.com:8080',
'mailto:nov@example.com/path',
'mailto:nov@example.com?query',
'mailto:nov@example.com#fragment',
'mailto:nov@example.com:8080/path?query#fragment',
'localhost',
'localhost:8080',
'localhost/path',
'localhost?query',
'localhost#fragment',
'localhost/path?query#fragment',
'nov@localhost',
'nov@localhost:8080',
'nov@localhost/path',
'nov@localhost?query',
'nov@localhost#fragment',
'nov@localhost/path?query#fragment',
'tel:+810312345678',
'device:192.168.2.1',
'device:192.168.2.1:8080',
'device:192.168.2.1/path',
'device:192.168.2.1?query',
'device:192.168.2.1#fragment',
'device:192.168.2.1/path?query#fragment'
].each do |input|
normalized = OpenIDConnect::Discovery::URINormalizer.new(input).normalized
puts "#{input} => #{normalized}"
end
## INPUT => NORMALIZED
# example.com => https://example.com
# example.com:8080 => https://example.com:8080
# example.com/path => https://example.com/path
# example.com?query => https://example.com?query
# example.com#fragment => https://example.com
# example.com:8080/path?query#fragment => https://example.com:8080/path?query
# http://example.com => http://example.com
# http://example.com:8080 => http://example.com:8080
# http://example.com/path => http://example.com/path
# http://example.com?query => http://example.com?query
# http://example.com#fragment => http://example.com
# http://example.com:8080/path?query#fragment => http://example.com:8080/path?query
# nov@example.com => acct:nov@example.com
# nov@example.com:8080 => https://nov@example.com:8080
# nov@example.com/path => https://nov@example.com/path
# nov@example.com?query => https://nov@example.com?query
# nov@example.com#fragment => acct:nov@example.com
# nov@example.com:8080/path?query#fragment => https://nov@example.com:8080/path?query
# acct:nov@matake.jp => acct:nov@matake.jp
# acct:nov@example.com:8080 => acct:nov@example.com:8080
# acct:nov@example.com/path => acct:nov@example.com/path
# acct:nov@example.com?query => acct:nov@example.com?query
# acct:nov@example.com#fragment => acct:nov@example.com
# acct:nov@example.com:8080/path?query#fragment => acct:nov@example.com:8080/path?query
# mailto:nov@matake.jp => mailto:nov@matake.jp
# mailto:nov@example.com:8080 => mailto:nov@example.com:8080
# mailto:nov@example.com/path => mailto:nov@example.com/path
# mailto:nov@example.com?query => mailto:nov@example.com?query
# mailto:nov@example.com#fragment => mailto:nov@example.com
# mailto:nov@example.com:8080/path?query#fragment => mailto:nov@example.com:8080/path?query
# localhost => https://localhost
# localhost:8080 => https://localhost:8080
# localhost/path => https://localhost/path
# localhost?query => https://localhost?query
# localhost#fragment => https://localhost
# localhost/path?query#fragment => https://localhost/path?query
# nov@localhost => acct:nov@localhost
# nov@localhost:8080 => https://nov@localhost:8080
# nov@localhost/path => https://nov@localhost/path
# nov@localhost?query => https://nov@localhost?query
# nov@localhost#fragment => acct:nov@localhost
# nov@localhost/path?query#fragment => https://nov@localhost/path?query
# tel:+810312345678 => tel:+810312345678
# device:192.168.2.1 => device:192.168.2.1
# device:192.168.2.1:8080 => device:192.168.2.1:8080
# device:192.168.2.1/path => device:192.168.2.1/path
# device:192.168.2.1?query => device:192.168.2.1?query
# device:192.168.2.1#fragment => device:192.168.2.1
# device:192.168.2.1/path?query#fragment => device:192.168.2.1/path?query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment