Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

refactor: adds random port and url parsing as part oauth refactor #228

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
Loading
from
Draft
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
27 changes: 19 additions & 8 deletions 27 google_auth_oauthlib/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import hashlib
import json
import logging
import random
from urllib.parse import urlparse, parse_qs
import warnings

try:
Expand All @@ -77,6 +79,13 @@
]


def parse_code(uri):
parsed_url = urlparse(uri)
query_args = parse_qs(parsed_url.query)
code = query_args["code"][0]
return code


class Flow(object):
"""OAuth 2.0 Authorization Flow

Expand Down Expand Up @@ -370,14 +379,16 @@ class InstalledAppFlow(Flow):
https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md
"""

_OOB_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"
# _OOB_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"
_PORT_NUMBER = random.randint(9000, 65000)
_OOB_REDIRECT_URI = f"http://localhost:{_PORT_NUMBER}"

_DEFAULT_AUTH_PROMPT_MESSAGE = (
"Please visit this URL to authorize this application: {url}"
"Please visit this URL to authorize this application: \n\n{url}\n"
)
"""str: The message to display when prompting the user for
authorization."""
_DEFAULT_AUTH_CODE_MESSAGE = "Enter the authorization code: "
_DEFAULT_AUTH_CODE_MESSAGE = "Upon authorizing, your browser will attempt to load a local host site but will be unsuccessful and will indicate 'This site can't be reached'. In your browser's address bar, copy that URL and enter it below:\n"
"""str: The message to display when prompting the user for the
authorization code. Used only by the console strategy."""

Expand All @@ -387,9 +398,9 @@ class InstalledAppFlow(Flow):

def run_console(
self,
authorization_prompt_message=_DEFAULT_AUTH_PROMPT_MESSAGE,
authorization_prompt_message="RC" + _DEFAULT_AUTH_PROMPT_MESSAGE,
authorization_code_message=_DEFAULT_AUTH_CODE_MESSAGE,
**kwargs
**kwargs,
):
"""Run the flow using the console strategy.

Expand Down Expand Up @@ -435,7 +446,7 @@ def run_console(
print(authorization_prompt_message.format(url=auth_url))

code = input(authorization_code_message)

code = parse_code(code)
self.fetch_token(code=code)

return self.credentials
Expand All @@ -445,11 +456,11 @@ def run_local_server(
host="localhost",
bind_addr=None,
port=8080,
authorization_prompt_message=_DEFAULT_AUTH_PROMPT_MESSAGE,
authorization_prompt_message="RL" + _DEFAULT_AUTH_PROMPT_MESSAGE,
success_message=_DEFAULT_WEB_SUCCESS_MESSAGE,
open_browser=True,
redirect_uri_trailing_slash=True,
**kwargs
**kwargs,
):
"""Run the flow using the server strategy.

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.