|
3 | 3 | ====================
|
4 | 4 | """
|
5 | 5 |
|
6 |
| -# Methods for setting up and accessing the GitHub API. See |
7 |
| -# git.py for command line git operations. |
| 6 | +# Methods for accessing the GitHub API. See auth.py for methods |
| 7 | +# related to setting up authorization and git.py |
| 8 | +# for command line git operations. |
8 | 9 |
|
9 |
| -import requests |
10 |
| - |
11 |
| -import os.path as op |
12 |
| -from ruamel.yaml import YAML |
13 | 10 | import github3 as gh3
|
14 | 11 |
|
15 |
| -from .utils import get_request |
16 |
| - |
17 |
| - |
18 |
| -def get_github_auth(): |
19 |
| - """ |
20 |
| - Check to see if there is an existing github authentication |
21 |
| - and load the authentication. |
22 |
| -
|
23 |
| - Returns |
24 |
| - ------- |
25 |
| - ruamel.yaml.comments.CommentedMap |
26 |
| - Yaml object that contains the token and id for a github session. |
27 |
| - If yaml doesn't exists, return an empty dictionary. |
28 |
| - """ |
29 |
| - yaml = YAML() |
30 |
| - try: |
31 |
| - with open(op.expanduser("~/.abc-classroom.tokens.yml")) as f: |
32 |
| - config = yaml.load(f) |
33 |
| - return config["github"] |
34 |
| - |
35 |
| - except FileNotFoundError: |
36 |
| - return {} |
37 |
| - |
38 |
| - |
39 |
| -def set_github_auth(auth_info): |
40 |
| - """ |
41 |
| - Set the github authentication information. Put the token and id |
42 |
| - authentication information into a yaml file if it doesn't already exist. |
43 |
| -
|
44 |
| - Parameters |
45 |
| - ---------- |
46 |
| - auth_info : dictionary |
47 |
| - The token and id authentication information from github stored in a |
48 |
| - dictionary object. |
49 |
| - """ |
50 |
| - yaml = YAML() |
51 |
| - config = {} |
52 |
| - if get_github_auth(): |
53 |
| - with open(op.expanduser("~/.abc-classroom.tokens.yml")) as f: |
54 |
| - config = yaml.load(f) |
55 |
| - |
56 |
| - config["github"] = auth_info |
57 |
| - |
58 |
| - with open(op.expanduser("~/.abc-classroom.tokens.yml"), "w") as f: |
59 |
| - yaml.dump(config, f) |
60 |
| - |
61 |
| - |
62 |
| -def get_access_token(): |
63 |
| - """Get a GitHub access token for the API |
64 |
| -
|
65 |
| - First tries to read from local token file. If token does not exist, |
66 |
| - or is not valid, generates a new token using the OAuth Device Flow. |
67 |
| - https://docs.github.com/en/free-pro-team@latest/developers/apps/ |
68 |
| - identifying-and-authorizing-users-for-github-apps#device-flow |
69 |
| -
|
70 |
| - Returns an access token (string). |
71 |
| - """ |
72 |
| - # first, we see if we have a saved token |
73 |
| - auth_info = get_github_auth() |
74 |
| - if auth_info: |
75 |
| - try: |
76 |
| - access_token = auth_info["access_token"] |
77 |
| - # if so, is it valid? |
78 |
| - user = _get_authenticated_user(access_token) |
79 |
| - if user is not None: |
80 |
| - print( |
81 |
| - "Access token is present and valid; successfully " |
82 |
| - "authenticated as user {}".format(user) |
83 |
| - ) |
84 |
| - return access_token |
85 |
| - except KeyError: |
86 |
| - pass |
87 |
| - |
88 |
| - # otherwise, generate a new token |
89 |
| - print("Generating new access token") |
90 |
| - # client id for the abc-classroom-bot GitHub App |
91 |
| - client_id = "Iv1.8df72ad9560c774c" |
92 |
| - |
93 |
| - # TODO need to handle cases where the device call fails - wrong client_id, |
94 |
| - # the user could ^C or the internet could be out, or some other |
95 |
| - # unanticipated woe) |
96 |
| - device_code = _get_login_code(client_id) |
97 |
| - access_token = _poll_for_status(client_id, device_code) |
98 |
| - |
99 |
| - # test the new access token |
100 |
| - user = _get_authenticated_user(access_token) |
101 |
| - if user is not None: |
102 |
| - print("""Successfully authenticated as user {}""".format(user)) |
103 |
| - return access_token |
104 |
| - |
105 |
| - |
106 |
| -def _get_authenticated_user(token): |
107 |
| - """Test the validity of an access token. |
108 |
| -
|
109 |
| - Given a github access token, test that it is valid by making an |
110 |
| - API call to get the authenticated user. |
111 |
| -
|
112 |
| - Returns the GitHub username of the authenticated user if token valid, |
113 |
| - otherwise returns None. |
114 |
| - """ |
115 |
| - |
116 |
| - url = "https://api.github.com/user" |
117 |
| - (status, body) = get_request(url, token) |
118 |
| - try: |
119 |
| - user = body["login"] |
120 |
| - return user |
121 |
| - except KeyError: |
122 |
| - return None |
123 |
| - |
124 |
| - |
125 |
| -def _get_login_code(client_id): |
126 |
| - """Prompts the user to authorize abc-classroom-bot. |
127 |
| -
|
128 |
| - First part of the Device Flow workflow. Asks user to visit a URL and |
129 |
| - enter the provided code. Waits for user to hit RETURN to continue. |
130 |
| - Returns the device code. |
131 |
| - """ |
132 |
| - |
133 |
| - # make the device call |
134 |
| - header = {"Content-Type": "application/json", "Accept": "application/json"} |
135 |
| - payload = {"client_id": client_id} |
136 |
| - link = "https://github.com/login/device/code" |
137 |
| - r = requests.post(link, headers=header, json=payload) |
138 |
| - |
139 |
| - # process the response |
140 |
| - data = r.json() |
141 |
| - status = r.status_code |
142 |
| - if status != 200: |
143 |
| - # print the response if the call failed |
144 |
| - print(r.json()) |
145 |
| - return None |
146 |
| - |
147 |
| - device_code = data["device_code"] |
148 |
| - uri = data["verification_uri"] |
149 |
| - user_code = data["user_code"] |
150 |
| - |
151 |
| - # prompt the user to enter the code |
152 |
| - print( |
153 |
| - "To authorize this app, go to {} and enter the code {}".format( |
154 |
| - uri, user_code |
155 |
| - ) |
156 |
| - ) |
157 |
| - input("\nPress RETURN to continue after inputting the code successfully") |
158 |
| - |
159 |
| - return device_code |
160 |
| - |
161 |
| - |
162 |
| -def _poll_for_status(client_id, device_code): |
163 |
| - """Polls API to see if user entered the device code |
164 |
| -
|
165 |
| - This is the second step of the Device Flow. Returns an access token, and |
166 |
| - also writes the token to a file in the user's home directory. |
167 |
| - """ |
168 |
| - |
169 |
| - header = {"Content-Type": "application/json", "Accept": "application/json"} |
170 |
| - payload = { |
171 |
| - "client_id": client_id, |
172 |
| - "device_code": device_code, |
173 |
| - "grant_type": "urn:ietf:params:oauth:grant-type:device_code", |
174 |
| - } |
175 |
| - r = requests.post( |
176 |
| - "https://github.com/login/oauth/access_token", |
177 |
| - headers=header, |
178 |
| - json=payload, |
179 |
| - ) |
180 |
| - |
181 |
| - data = r.json() |
182 |
| - access_token = data["access_token"] |
183 |
| - set_github_auth({"access_token": access_token}) |
184 |
| - return access_token |
185 |
| - |
186 | 12 |
|
187 | 13 | def remote_repo_exists(org, repository, token=None):
|
188 | 14 | """Check if the remote repository exists for the organization."""
|
|
0 commit comments