|
1 | 1 | ############################ Copyrights and license ########################### |
2 | 2 | # # |
3 | 3 | # Copyright 2019 Rigas Papathanasopoulos <[email protected]> # |
| 4 | +# Copyright 2023 Enrico Minack <[email protected]> # |
4 | 5 | # # |
5 | 6 | # This file is part of PyGithub. # |
6 | 7 | # http://pygithub.readthedocs.io/ # |
@@ -96,21 +97,74 @@ def get_access_token(self, code, state=None): |
96 | 97 | if state is not None: |
97 | 98 | post_parameters["state"] = state |
98 | 99 |
|
99 | | - headers, data = self._requester.requestJsonAndCheck( |
100 | | - "POST", |
101 | | - "https://github.com/login/oauth/access_token", |
102 | | - headers={ |
103 | | - "Accept": "application/json", |
104 | | - "Content-Type": "application/json", |
105 | | - "User-Agent": "PyGithub/Python", |
106 | | - }, |
107 | | - input=post_parameters, |
| 100 | + headers, data = self._checkError( |
| 101 | + *self._requester.requestJsonAndCheck( |
| 102 | + "POST", |
| 103 | + "https://github.com/login/oauth/access_token", |
| 104 | + headers={"Accept": "application/json"}, |
| 105 | + input=post_parameters, |
| 106 | + ) |
108 | 107 | ) |
109 | 108 |
|
110 | 109 | return AccessToken( |
111 | 110 | requester=self._requester, |
112 | | - # not required, this is a NonCompletableGithubObject |
113 | | - headers={}, |
| 111 | + headers=headers, |
114 | 112 | attributes=data, |
115 | 113 | completed=False, |
116 | 114 | ) |
| 115 | + |
| 116 | + def get_app_user_auth(self, token): |
| 117 | + """ |
| 118 | + :param token: AccessToken |
| 119 | + """ |
| 120 | + # imported here to avoid circular import |
| 121 | + from github.Auth import AppUserAuth |
| 122 | + |
| 123 | + return AppUserAuth( |
| 124 | + client_id=self.client_id, |
| 125 | + client_secret=self.client_secret, |
| 126 | + token=token.token, |
| 127 | + token_type=token.type, |
| 128 | + expires_at=token.expires_at, |
| 129 | + refresh_token=token.refresh_token, |
| 130 | + refresh_expires_at=token.refresh_expires_at, |
| 131 | + requester=self._requester, |
| 132 | + ) |
| 133 | + |
| 134 | + def refresh_access_token(self, refresh_token): |
| 135 | + """ |
| 136 | + :calls: `POST /login/oauth/access_token <https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps>`_ |
| 137 | + :param refresh_token: string |
| 138 | + """ |
| 139 | + assert isinstance(refresh_token, str) |
| 140 | + post_parameters = { |
| 141 | + "client_id": self.client_id, |
| 142 | + "client_secret": self.client_secret, |
| 143 | + "grant_type": "refresh_token", |
| 144 | + "refresh_token": refresh_token, |
| 145 | + } |
| 146 | + |
| 147 | + headers, data = self._checkError( |
| 148 | + *self._requester.requestJsonAndCheck( |
| 149 | + "POST", |
| 150 | + "https://github.com/login/oauth/access_token", |
| 151 | + headers={"Accept": "application/json"}, |
| 152 | + input=post_parameters, |
| 153 | + ) |
| 154 | + ) |
| 155 | + |
| 156 | + return AccessToken( |
| 157 | + requester=self._requester, |
| 158 | + headers=headers, |
| 159 | + attributes=data, |
| 160 | + completed=False, |
| 161 | + ) |
| 162 | + |
| 163 | + @staticmethod |
| 164 | + def _checkError(headers, data): |
| 165 | + if isinstance(data, dict) and "error" in data: |
| 166 | + if data["error"] == "bad_verification_code": |
| 167 | + raise github.BadCredentialsException(200, data, headers) |
| 168 | + raise github.GithubException(200, data, headers) |
| 169 | + |
| 170 | + return headers, data |
0 commit comments