|
| 1 | +import json |
| 2 | +from typing import Type |
| 3 | +from error_handling.error_handler import ErrorHandler |
| 4 | +import requests |
| 5 | +from github_oauth_client.constants import ( |
| 6 | + GITHUB_BASE_LOGIN_URL, |
| 7 | + GITHUB_OAUTH_ENDPOINT, |
| 8 | + GITHUB_VERIFICATION_CODES_ENDPOINT, |
| 9 | + GRANT_TYPE, |
| 10 | + HEADERS, |
| 11 | +) |
| 12 | +from github_oauth_client.models import ( |
| 13 | + AccessTokenResponseModel, |
| 14 | + VerificationCodesResponseModel, |
| 15 | +) |
| 16 | +from pephubclient.models import ClientData |
| 17 | +from pydantic import BaseModel, ValidationError |
| 18 | +from error_handling.exceptions import ResponseError |
| 19 | +from helpers import RequestManager |
| 20 | + |
| 21 | + |
| 22 | +class GitHubOAuthClient(RequestManager): |
| 23 | + """ |
| 24 | + Class responsible for authorization with GitHub. |
| 25 | + """ |
| 26 | + |
| 27 | + def get_access_token(self, client_data: ClientData): |
| 28 | + """ |
| 29 | + Requests user with specified ClientData.client_id to enter the verification code, and then |
| 30 | + responds with GitHub access token. |
| 31 | + """ |
| 32 | + device_code = self._get_device_verification_code(client_data) |
| 33 | + return self._request_github_for_access_token(device_code, client_data) |
| 34 | + |
| 35 | + def _get_device_verification_code(self, client_data: ClientData) -> str: |
| 36 | + """ |
| 37 | + Send the request for verification codes, parse the response and return device code. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Device code which is needed later to obtain the access code. |
| 41 | + """ |
| 42 | + resp = GitHubOAuthClient.send_request( |
| 43 | + method="POST", |
| 44 | + url=f"{GITHUB_BASE_LOGIN_URL}{GITHUB_VERIFICATION_CODES_ENDPOINT}", |
| 45 | + params={"client_id": client_data.client_id}, |
| 46 | + headers=HEADERS, |
| 47 | + ) |
| 48 | + verification_codes_response = self._handle_github_response( |
| 49 | + resp, VerificationCodesResponseModel |
| 50 | + ) |
| 51 | + print( |
| 52 | + f"User verification code: {verification_codes_response.user_code}, " |
| 53 | + f"please enter the code here: {verification_codes_response.verification_uri} and" |
| 54 | + f"hit enter when you are done with authentication on the website" |
| 55 | + ) |
| 56 | + input() |
| 57 | + |
| 58 | + return verification_codes_response.device_code |
| 59 | + |
| 60 | + def _request_github_for_access_token( |
| 61 | + self, device_code: str, client_data: ClientData |
| 62 | + ) -> str: |
| 63 | + """ |
| 64 | + Send the request for access token, parse the response and return access token. |
| 65 | +
|
| 66 | + Args: |
| 67 | + device_code: Device code from verification codes request. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + Access token. |
| 71 | + """ |
| 72 | + response = GitHubOAuthClient.send_request( |
| 73 | + method="POST", |
| 74 | + url=f"{GITHUB_BASE_LOGIN_URL}{GITHUB_OAUTH_ENDPOINT}", |
| 75 | + params={ |
| 76 | + "client_id": client_data.client_id, |
| 77 | + "device_code": device_code, |
| 78 | + "grant_type": GRANT_TYPE, |
| 79 | + }, |
| 80 | + headers=HEADERS, |
| 81 | + ) |
| 82 | + return self._handle_github_response( |
| 83 | + response, AccessTokenResponseModel |
| 84 | + ).access_token |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def _handle_github_response(response: requests.Response, model: Type[BaseModel]): |
| 88 | + """ |
| 89 | + Decode the response from GitHub and pack the returned data into appropriate model. |
| 90 | +
|
| 91 | + Args: |
| 92 | + response: Response from GitHub. |
| 93 | + model: Model that the data will be packed to. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + Response data as an instance of correct model. |
| 97 | + """ |
| 98 | + try: |
| 99 | + content = json.loads(GitHubOAuthClient.decode_response(response)) |
| 100 | + except json.JSONDecodeError: |
| 101 | + raise ResponseError("Something went wrong with GitHub response") |
| 102 | + |
| 103 | + try: |
| 104 | + return model(**content) |
| 105 | + except ValidationError: |
| 106 | + raise ErrorHandler.parse_github_response_error(content) or ResponseError() |
0 commit comments