Skip to content

Commit da05dda

Browse files
committed
🚧 Added basic code for 2FA authentication
1 parent f053c85 commit da05dda

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,15 @@ 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,
199+
gh = github3.GitHub()
200+
gh.login(login, password, two_factor_callback=lambda: prompt('2FA code> '))
201+
gh.authorize(login, password,
200202
scopes=[ 'repo', 'delete_repo', 'gist' ],
201203
note='git-repo token used on {}'.format(platform.node()),
202204
note_url='https://github.com/guyzmo/git-repo')
203-
return auth.token
205+
return gh.token
204206

205207
@property
206208
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

0 commit comments

Comments
 (0)