Skip to content

Normalize the url when it contains default HTTP/HTTPS ports #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ def generate_verifier(length=8):
return ''.join([str(random.randint(0, 9)) for i in range(length)])


def normalize_url(value):
if value:
scheme, netloc, path, params, query, fragment = urlparse.urlparse(value)

# Exclude default port numbers.
if scheme == 'http' and netloc[-3:] == ':80':
netloc = netloc[:-3]
elif scheme == 'https' and netloc[-4:] == ':443':
netloc = netloc[:-4]
if scheme not in ('http', 'https'):
raise ValueError("Unsupported URL %s (%s)." % (value, scheme))

# Normalized URL excludes params, query, and fragment.
normalized_url = urlparse.urlunparse((scheme, netloc, path, None, None, None))
return normalized_url
else:
return None


class Consumer(object):
"""A consumer of OAuth-protected services.

Expand Down Expand Up @@ -542,6 +561,9 @@ def from_request(cls, http_method, http_url, headers=None, parameters=None,
url_params = cls._split_url_string(param_str)
parameters.update(url_params)

# Simply normalize URL: remove default port numbers if existing
http_url = normalize_url(http_url)

if parameters:
return cls(http_method, http_url, parameters)

Expand Down