Skip to content

Commit 9199f6a

Browse files
committed
Merge pull request sigmavirus24#33 from seveas/requires_basic_auth
Better error messages for authorization methods
2 parents bfa988e + e3e6c38 commit 9199f6a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

github3/auths.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
"""
88

9+
from github3.decorators import requires_basic_auth
910
from github3.models import GitHubCore
1011
from json import dumps
1112

@@ -44,10 +45,12 @@ def __repr__(self):
4445
def _update_(self, auth):
4546
self.__init__(auth, self._session)
4647

48+
@requires_basic_auth
4749
def delete(self):
4850
"""delete this authorization"""
4951
return self._boolean(self._delete(self._api), 204, 404)
5052

53+
@requires_basic_auth
5154
def update(self, scopes=[], add_scopes=[], rm_scopes=[], note='',
5255
note_url=''):
5356
"""Update this authorization.

github3/decorators.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,33 @@ def auth_wrapper(self, *args, **kwargs):
5050
r.raw = StringIO('{"message": "Requires authentication"}'.encode())
5151
raise GitHubError(r)
5252
return auth_wrapper
53+
54+
def requires_basic_auth(func):
55+
"""Decorator to note which object methods require username/password
56+
authorization and won't work with token based authorization.
57+
58+
.. note::
59+
This decorator causes the wrapped methods to lose their proper
60+
signature. Please refer to the documentation for each of those.
61+
"""
62+
#note = """.. note::
63+
#The signature of this function may not appear correctly in
64+
#documentation. Please adhere to the defined parameters and their
65+
#types.
66+
#"""
67+
## Append the above note to each function this is applied to
68+
#func.__doc__ = '\n\n'.join([func.__doc__, note])
69+
70+
@wraps(func)
71+
def auth_wrapper(self, *args, **kwargs):
72+
if hasattr(self, '_session') and self._session.auth:
73+
return func(self, *args, **kwargs)
74+
else:
75+
from github3.models import GitHubError
76+
# Mock a 401 response
77+
r = Response()
78+
r.status_code = 401
79+
r.encoding = 'utf-8'
80+
r.raw = StringIO('{"message": "Requires username/password authentication"}'.encode())
81+
raise GitHubError(r)
82+
return auth_wrapper

github3/github.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from github3.orgs import Organization
1818
from github3.repos import Repository
1919
from github3.users import User, Key
20-
from github3.decorators import requires_auth
20+
from github3.decorators import requires_auth, requires_basic_auth
2121

2222

2323
class GitHub(GitHubCore):
@@ -40,7 +40,7 @@ def _iter_follow(self, which, number):
4040
url = self._build_url('user', which)
4141
return self._iter(number, url, User)
4242

43-
@requires_auth
43+
@requires_basic_auth
4444
def authorization(self, id_num):
4545
"""Get information about authorization ``id``.
4646
@@ -320,7 +320,7 @@ def issue(self, owner, repository, number):
320320
return repo.issue(number)
321321
return None
322322

323-
@requires_auth
323+
@requires_basic_auth
324324
def list_authorizations(self):
325325
"""List authorizations for the authenticated user.
326326
@@ -330,7 +330,7 @@ def list_authorizations(self):
330330
json = self._json(self._get(url), 200)
331331
return [Authorization(a, self) for a in json]
332332

333-
@requires_auth
333+
@requires_basic_auth
334334
def iter_authorizations(self, number=-1):
335335
"""Iterate over authorizations for the authenticated user.
336336

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy