Check whether or not a Github OAuth token is valid. Args: access_token (str): The Github OAuth token. Returns: bool: Whether or not the provided OAuth token is valid.
(oauth_token=None, last_validated=None)
| 140 | |
| 141 | |
| 142 | def is_github_token_valid(oauth_token=None, last_validated=None): |
| 143 | """Check whether or not a Github OAuth token is valid. |
| 144 | |
| 145 | Args: |
| 146 | access_token (str): The Github OAuth token. |
| 147 | |
| 148 | Returns: |
| 149 | bool: Whether or not the provided OAuth token is valid. |
| 150 | |
| 151 | """ |
| 152 | # If no OAuth token was provided, no checks necessary. |
| 153 | if not oauth_token: |
| 154 | return False |
| 155 | |
| 156 | # If validation datetime string is passed, parse it to datetime. |
| 157 | if last_validated: |
| 158 | try: |
| 159 | last_validated = dateutil.parser.parse(last_validated) |
| 160 | except ValueError: |
| 161 | print('Validation of date failed.') |
| 162 | last_validated = None |
| 163 | |
| 164 | # Check whether or not the user's access token has been validated recently. |
| 165 | if oauth_token and last_validated: |
| 166 | if (timezone.now() - last_validated) < timedelta(hours=1): |
| 167 | return True |
| 168 | |
| 169 | _params = build_auth_dict(oauth_token) |
| 170 | _auth = (_params['client_id'], _params['client_secret']) |
| 171 | url = TOKEN_URL.format(**_params) |
| 172 | try: |
| 173 | response = requests.get(url, auth=_auth, headers=HEADERS) |
| 174 | except ConnectionError as e: |
| 175 | if not settings.ENV == 'local': |
| 176 | logger.error(e) |
| 177 | else: |
| 178 | print(e, '- No connection available. Unable to authenticate with Github.') |
| 179 | return False |
| 180 | |
| 181 | if response.status_code == 200: |
| 182 | return True |
| 183 | return False |
| 184 | |
| 185 | |
| 186 | def revoke_token(oauth_token): |