(cookies)
| 79 | |
| 80 | |
| 81 | def FetchRequestToken(cookies): |
| 82 | logging.info("Loading OAuth2 auth page ...") |
| 83 | |
| 84 | query = urllib.parse.urlencode({ |
| 85 | "client_id": OAuthClientId, |
| 86 | "redirect_uri": OAuthRedirectUri, |
| 87 | "response_type": OAuthResponseType, |
| 88 | "scope": OAuthScope |
| 89 | }) |
| 90 | |
| 91 | headers = { |
| 92 | "Cookie": encodeCookies(cookies), |
| 93 | } |
| 94 | |
| 95 | conn = http.client.HTTPSConnection(osmHost) |
| 96 | conn.request("GET", f"/oauth2/authorize?{query}", headers=headers) |
| 97 | |
| 98 | res = conn.getresponse() |
| 99 | |
| 100 | if res.status >= 400: |
| 101 | print(res.status) |
| 102 | print(res.headers) |
| 103 | print(res.read().decode('utf-8')) |
| 104 | raise Exception("Can't load OAuth2 page") |
| 105 | |
| 106 | elif res.status == 302: |
| 107 | logging.info("User already accepted OAuth2 request!") |
| 108 | redirectUri = res.headers['Location'] |
| 109 | logging.info(f"Accepted OAuth2: {redirectUri}") |
| 110 | oauthCode = FindOauthCode(redirectUri) |
| 111 | |
| 112 | if not oauthCode: |
| 113 | raise Exception(f"Can't find 'code' in redirect URI: {redirectUri}") |
| 114 | |
| 115 | return oauthCode |
| 116 | else: |
| 117 | respBody = res.read().decode("utf-8") |
| 118 | authToken = FindAuthenticityToken("/oauth2/authorize", respBody) |
| 119 | if not authToken: |
| 120 | print(res.status) |
| 121 | print(res.headers) |
| 122 | print(respBody) |
| 123 | raise Exception("Invalid authToken '{authToken}'") |
| 124 | logging.info(f"Parsed authToken = {authToken}") |
| 125 | |
| 126 | return SendAuthRequest(authToken, cookies) |
| 127 | |
| 128 | def SendAuthRequest(authToken, cookies): |
| 129 | logging.info("Accepting OAuth2 ...") |
no test coverage detected