Skip to content

Commit

Permalink
馃毀 Added basic code for 2FA authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
guyzmo committed Oct 12, 2016
1 parent 207330a commit 0c29743
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion git_repo/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def setup_service(service):
username = loop_input('username> ')
password = loop_input('password> ', method=getpass)

token = service.get_auth_token(username, password)
token = service.get_auth_token(username, password, prompt=loop_input)
print('Great! You\'ve been identified 馃嵒')

print('Do you want to give a custom name for this service\'s remote?')
Expand Down
2 changes: 1 addition & 1 deletion git_repo/services/ext/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def get_repository(self, user, repo):
#raise ResourceNotFoundError('Cannot retrieve repository: {}/{} does not exists.'.format(user, repo))

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

Expand Down
8 changes: 5 additions & 3 deletions git_repo/services/ext/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ def request_fetch(self, user, repo, request, pull=False):
raise err

@classmethod
def get_auth_token(cls, login, password):
def get_auth_token(cls, login, password, prompt=None):
import platform
auth = github3.GitHub().authorize(login, password,
gh = github3.GitHub()
gh.login(login, password, two_factor_callback=lambda: prompt('2FA code> '))
gh.authorize(login, password,
scopes=[ 'repo', 'delete_repo', 'gist' ],
note='git-repo token used on {}'.format(platform.node()),
note_url='https://github.com/guyzmo/git-repo')
return auth.token
return gh.token

@property
def user(self):
Expand Down
2 changes: 1 addition & 1 deletion git_repo/services/ext/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_repository(self, user, repo):
raise ResourceNotFoundError("Cannot delete: repository {}/{} does not exists.".format(user, repo)) from err

@classmethod
def get_auth_token(cls, login, password):
def get_auth_token(cls, login, password, prompt=None):
gl = gitlab.Gitlab(url='https://{}'.format(cls.fqdn), email=login, password=password)
gl.auth()
return gl.user.private_token
Expand Down

0 comments on commit 0c29743

Please sign in to comment.