Skip to content

Commit 8077d35

Browse files
committed
Merge branch 'features/2FA_github' into devel
2 parents f053c85 + 9d18dff commit 8077d35

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

git_repo/repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def setup_service(service):
483483
username = loop_input('username> ')
484484
password = loop_input('password> ', method=getpass)
485485

486-
token = service.get_auth_token(username, password)
486+
token = service.get_auth_token(username, password, prompt=loop_input)
487487
print('Great! You\'ve been identified 🍻')
488488

489489
print('Do you want to give a custom name for this service\'s remote?')

git_repo/services/ext/bitbucket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def get_repository(self, user, repo):
146146
#raise ResourceNotFoundError('Cannot retrieve repository: {}/{} does not exists.'.format(user, repo))
147147

148148
@classmethod
149-
def get_auth_token(cls, login, password):
149+
def get_auth_token(cls, login, password, prompt=None):
150150
log.warn("/!\\ Due to API limitations, the bitbucket login/password is stored as plaintext in configuration.")
151151
return "{}:{}".format(login, password)
152152

git_repo/services/ext/github.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,21 @@ def request_fetch(self, user, repo, request, pull=False):
194194
raise err
195195

196196
@classmethod
197-
def get_auth_token(cls, login, password):
197+
def get_auth_token(cls, login, password, prompt=None):
198198
import platform
199-
auth = github3.GitHub().authorize(login, password,
200-
scopes=[ 'repo', 'delete_repo', 'gist' ],
201-
note='git-repo token used on {}'.format(platform.node()),
202-
note_url='https://github.com/guyzmo/git-repo')
203-
return auth.token
199+
gh = github3.GitHub()
200+
gh.login(login, password, two_factor_callback=lambda: prompt('2FA code> '))
201+
try:
202+
auth = gh.authorize(login, password,
203+
scopes=[ 'repo', 'delete_repo', 'gist' ],
204+
note='git-repo2 token used on {}'.format(platform.node()),
205+
note_url='https://github.com/guyzmo/git-repo')
206+
return auth.token
207+
except github3.models.GitHubError as err:
208+
if len(err.args) > 0 and 422 == err.args[0].status_code:
209+
raise ResourceExistsError("A token already exist for this machine on your github account.")
210+
else:
211+
raise err
204212

205213
@property
206214
def user(self):

git_repo/services/ext/gitlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def get_repository(self, user, repo):
7474
raise ResourceNotFoundError("Cannot delete: repository {}/{} does not exists.".format(user, repo)) from err
7575

7676
@classmethod
77-
def get_auth_token(cls, login, password):
77+
def get_auth_token(cls, login, password, prompt=None):
7878
gl = gitlab.Gitlab(url='https://{}'.format(cls.fqdn), email=login, password=password)
7979
gl.auth()
8080
return gl.user.private_token

git_repo/services/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def get_service(cls, repository, command):
122122
return cls._current
123123

124124
@classmethod
125-
def get_auth_token(cls, login, password):
125+
def get_auth_token(cls, login, password, prompt=None):
126126
raise NotImplementedError
127127

128128
def __init__(self, r=None, c=None):

tests/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def request_create(self, *args, **kwarg):
122122
return {'local': local, 'remote': remote, 'ref': 42}
123123

124124
@classmethod
125-
def get_auth_token(cls, login, password):
125+
def get_auth_token(cls, login, password, prompt=None):
126126
return '{}:{}'.format(login, password)
127127

128128
@property

0 commit comments

Comments
 (0)