Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a1b4d8

Browse files
committed
Some more work on adding two-factor features
1 parent3fdc27a commit9a1b4d8

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

‎github3/github.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,8 @@ def key(self, id_num):
821821
json=self._json(self._get(url),200)
822822
returnKey(json,self)ifjsonelseNone
823823

824-
deflogin(self,username=None,password=None,token=None):
824+
deflogin(self,username=None,password=None,token=None,
825+
two_factor_callback=None):
825826
"""Logs the user into GitHub for protected API calls.
826827
827828
:param str username: (optional)
@@ -833,6 +834,9 @@ def login(self, username=None, password=None, token=None):
833834
eliftoken:
834835
self._session.token_auth(token)
835836

837+
# The Session method handles None for free.
838+
self._session.two_factor_auth_callback(two_factor_callback)
839+
836840
defmarkdown(self,text,mode='',context='',raw=False):
837841
"""Render an arbitrary markdown document.
838842

‎github3/session.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
importrequests
22

3+
fromcollectionsimportCallable
34
fromgithub3import__version__
45
fromloggingimportgetLogger
56

67
__url_cache__= {}
78
__logs__=getLogger(__package__)
89

910

11+
defrequires_2fa(response):
12+
if (response.status_code==401and'X-GitHub-OTP'inresponse.headers
13+
and'required'inresponse.headers['X-GitHub-OTP']):
14+
returnTrue
15+
returnFalse
16+
17+
1018
classGitHubSession(requests.Session):
1119
def__init__(self):
1220
super(GitHubSession,self).__init__()
@@ -21,6 +29,7 @@ def __init__(self):
2129
'User-Agent':'github3.py/{0}'.format(__version__),
2230
})
2331
self.base_url='https://api.github.com'
32+
self.two_factor_auth_cb=None
2433

2534
defbasic_auth(self,username,password):
2635
"""Set the Basic Auth credentials on this Session.
@@ -45,6 +54,36 @@ def build_url(self, *args, **kwargs):
4554
__url_cache__[key]='/'.join(parts)
4655
return__url_cache__[key]
4756

57+
defoauth2_auth(self,client_id,client_secret):
58+
"""Use OAuth2 for authentication.
59+
60+
It is suggested you install requests-oauthlib to use this.
61+
62+
:param str client_id: Client ID retrieved from GitHub
63+
:param str client_secret: Client secret retrieved from GitHub
64+
"""
65+
raiseNotImplementedError('These features are not implemented yet')
66+
67+
defrequest(self,*args,**kwargs):
68+
response=super(GitHubSession,self).request(*args,**kwargs)
69+
ifrequires_2fa(response)andself.two_factor_auth_cb:
70+
headers=kwargs.pop('headers', {})
71+
headers.update({
72+
'X-GitHub-OTP':self.two_factor_auth_cb()
73+
})
74+
kwargs.update(headers=headers)
75+
response=super(GitHubSession,self).request(*args,**kwargs)
76+
returnresponse
77+
78+
deftwo_factor_auth_callback(self,callback):
79+
ifnotcallback:
80+
return
81+
82+
ifnotisinstance(callback,Callable):
83+
raiseValueError('Your callback should be callable')
84+
85+
self.two_factor_auth_cb=callback
86+
4887
deftoken_auth(self,token):
4988
"""Use an application token for authentication.
5089
@@ -57,13 +96,3 @@ def token_auth(self, token):
5796
self.headers.update({
5897
'Authorization':'token {0}'.format(token)
5998
})
60-
61-
defoauth2_auth(self,client_id,client_secret):
62-
"""Use OAuth2 for authentication.
63-
64-
It is suggested you install requests-oauthlib to use this.
65-
66-
:param str client_id: Client ID retrieved from GitHub
67-
:param str client_secret: Client secret retrieved from GitHub
68-
"""
69-
raiseNotImplementedError('These features are not implemented yet')

‎tests/unit/test_github.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ class TestGitHub(UnitHelper):
1010
deftest_two_factor_login(self):
1111
self.instance.login('username','password',
1212
two_factor_callback=lambda*args:'foo')
13+
14+
deftest_can_login_without_two_factor_callback(self):
15+
self.instance.login('username','password')
16+
self.instance.login(token='token')

‎tests/unit/test_github_session.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ def test_token_auth_does_not_use_falsey_values(self):
8484
s.token_auth(token)
8585
assert'Authorization'notins.headers
8686

87+
deftest_two_factor_auth_callback_handles_None(self):
88+
s=self.build_session()
89+
asserts.two_factor_auth_cbisNone
90+
s.two_factor_auth_callback(None)
91+
asserts.two_factor_auth_cbisNone
92+
93+
deftest_two_factor_auth_callback_checks_for_Callable(self):
94+
s=self.build_session()
95+
asserts.two_factor_auth_cbisNone
96+
withpytest.raises(ValueError):
97+
s.two_factor_auth_callback(1)
98+
99+
deftest_two_factor_auth_callback_accepts_a_Callable(self):
100+
s=self.build_session()
101+
asserts.two_factor_auth_cbisNone
102+
# You have to have a sense of humor ;)
103+
not_so_anonymous=lambda*args:'foo'
104+
s.two_factor_auth_callback(not_so_anonymous)
105+
asserts.two_factor_auth_cbisnot_so_anonymous
106+
87107
deftest_oauth2_auth(self):
88108
"""Test that oauth2 authentication works
89109

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp