| Conditions | 2 |
| Total Lines | 59 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import hashlib |
||
| 77 | def get_token_manual( |
||
| 78 | issuer_uri, |
||
| 79 | client_id, |
||
| 80 | redirect_uri=None, |
||
| 81 | scopes=None, |
||
| 82 | login_hint=None, |
||
| 83 | domain_hint=None, |
||
| 84 | browser_name=None |
||
| 85 | ): |
||
| 86 | state = str(randrange(1000000000)) # "InfoOfWhereTheUserWantedToGo" |
||
| 87 | nonce = str(randrange(1000000000)) |
||
| 88 | |||
| 89 | # Your first step is to generate_key_pair a code verifier and challenge: |
||
| 90 | # Code verifier: Random URL-safe string with a minimum length of 43 characters. |
||
| 91 | # Code challenge: Base64 URL-encoded SHA-256 hash of the code verifier. |
||
| 92 | code_verifier = secrets.token_urlsafe(43) |
||
| 93 | m = hashlib.sha256() |
||
| 94 | m.update(code_verifier.encode()) |
||
| 95 | code_challenge = urlsafe_b64encode(m.digest()).decode().rstrip("=") |
||
| 96 | |||
| 97 | print({ |
||
| 98 | "code_verifier": code_verifier, |
||
| 99 | "code_challenge": code_challenge |
||
| 100 | }) |
||
| 101 | |||
| 102 | res = requests.get( |
||
| 103 | url=posixpath.join(issuer_uri, "oauth2/v2.0/authorize"), |
||
| 104 | params={ |
||
| 105 | "client_id": client_id, |
||
| 106 | "response_type": "code", |
||
| 107 | "scope": " ".join(scopes), |
||
| 108 | "redirect_uri": redirect_uri, |
||
| 109 | "state": state, |
||
| 110 | "nonce": nonce, # Optional |
||
| 111 | "code_challenge_method": "S256", |
||
| 112 | "code_challenge": code_challenge, |
||
| 113 | # "client_info": "1", |
||
| 114 | # "prompt": "select_account", |
||
| 115 | # "login_hint": login_hint or "", |
||
| 116 | # "domain_hint": domain_hint or "" |
||
| 117 | } |
||
| 118 | ) |
||
| 119 | assert res.status_code == 200 |
||
| 120 | |||
| 121 | if browser_name: |
||
| 122 | webbrowser.get(browser_name).open(res.request.url) |
||
| 123 | else: |
||
| 124 | webbrowser.open(res.request.url) |
||
| 125 | |||
| 126 | response = input('TYPE YOUR RETURN URI:') |
||
| 127 | o = urlparse(response.rstrip()) |
||
| 128 | q = parse_qs(o.query) |
||
| 129 | |||
| 130 | return _exchange_code( |
||
| 131 | client_id=client_id, |
||
| 132 | issuer_uri=issuer_uri, |
||
| 133 | code_verifier=code_verifier, |
||
| 134 | redirect_url=redirect_uri, |
||
| 135 | code=q["code"] |
||
| 136 | ) |
||
| 186 |