10000 Rework revoke_authorization(s) with new methods · buiduyhieu1/github3.py@df7a6d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit df7a6d6

Browse files
committed
Rework revoke_authorization(s) with new methods
1 parent ee50f41 commit df7a6d6

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

github3/github.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,48 +1020,42 @@ def repository(self, owner, repository):
10201020
json = self._json(self._get(url), 200)
10211021
return Repository(json, self) if json else None
10221022

1023-
def _revoke_authorization(self, access_token=None):
1024-
"""Helper method for revoking application authorization keys.
1025-
1026-
:param str access_token: (optional), the access token to delete; if
1027-
not provided, revoke all access tokens for this application
1028-
1029-
"""
1030-
p = self._session.params
1031-
client_id = p.get('client_id')
1032-
client_secret = p.get('client_secret')
1033-
if client_id and client_secret:
1034-
auth = (client_id, client_secret)
1035-
url_parts = ['applications', str(auth[0]), 'tokens']
1036-
if access_token:
1037-
url_parts.append(access_token)
1038-
url = self._build_url(*url_parts)
1039-
return self._delete(url, auth=auth, params={
1040-
'client_id': None, 'client_secret': None
1041-
})
1042-
return False
1043-
10441023
@requires_app_credentials
10451024
def revoke_authorization(self, access_token):
10461025
"""Revoke specified authorization for an OAuth application.
10471026
1048-
Revoke all authorization tokens created by your application.
1027+
Revoke all authorization tokens created by your application. This will
1028+
only work if you have already called ``set_client_id``.
10491029
10501030
:param str access_token: (required), the access_token to revoke
10511031
:returns: bool -- True if successful, False otherwise
10521032
"""
1053-
self._revoke_authorization(access_token)
1033+
client_id, client_secret = self._session.retrieve_client_credentials()
1034+
url = self._build_url('applications', str(client_id), 'tokens',
1035+
access_token)
1036+
with self._session.temporary_basic_auth(client_id, client_secret):
1037+
response = self._delete(url, params={'client_id': None,
1038+
'client_secret': None})
1039+
1040+
return self._boolean(response, 204, 404)
10541041

10551042
@requires_app_credentials
10561043
def revoke_authorizations(self):
10571044
"""Revoke all authorizations for an OAuth application.
10581045
1059-
Revoke all authorization tokens created by your application.
1046+
Revoke all authorization tokens created by your application. This will
1047+
only work if you have already called ``set_client_id``.
10601048
10611049
:param str client_id: (required), the client_id of your application
10621050
:returns: bool -- True if successful, False otherwise
10631051
"""
1064-
self._revoke_authorization()
1052+
client_id, client_secret = self._session.retrieve_client_credentials()
1053+
url = self._build_url('applications', str(client_id), 'tokens')
1054+
with self._session.temporary_basic_auth(client_id, client_secret):
1055+
response = self._delete(url, params={'client_id': None,
1056+
'client_secret': None})
1057+
1058+
return self._boolean(response, 204, 404)
10651059

10661060
def search_code(self, query, sort=None, order=None, per_page=None,
10671061
text_match=False, number=-1, etag=None):

tests/unit/test_github.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@ def test_can_login_without_two_factor_callback(self):
1515
self.instance.login('username', 'password')
1616
self.instance.login(token='token')
1717

18+
19+
class TestGitHubAuthorizations(UnitHelper):
20+
described_class = GitHub
21+
example_data = None
22+
23+
def create_session_mock(self, *args):
24+
session = super(TestGitHubAuthorizations,
25+
self).create_session_mock(*args)
26+
session.retrieve_client_credentials.return_value = ('id', 'secret')
27+
return session
28+
1829
def test_revoke_authorization(self):
19-
self.instance.set_client_id('key', 'secret')
30+
self.instance.set_client_id('id', 'secret')
2031
self.instance.revoke_authorization('access_token')
2132
self.session.delete.assert_called_once_with(
22-
'https://api.github.com/applications/key/tokens/access_token',
23-
auth=('key', 'secret'),
33+
'https://api.github.com/applications/id/tokens/access_token',
2434
params={'client_id': None, 'client_secret': None}
2535
)
2636

2737
def test_revoke_authorizations(self):
28-
self.instance.set_client_id('key', 'secret')
38+
self.instance.set_client_id('id', 'secret')
2939
self.instance.revoke_authorizations()
3040
self.session.delete.assert_called_once_with(
31-
'https://api.github.com/applications/key/tokens',
32-
auth=('key', 'secret'),
41+
'https://api.github.com/applications/id/tokens',
3342
params={'client_id': None, 'client_secret': None}
3443
)

0 commit comments

Comments
 (0)
0