def _try_login_with_password_ntimes(cls, login, ntimes): user = None for i in range(0, ntimes): password = DialogHelper.ask_for_password( prompt='Github Password for {username}:'.format(username=login)) # user pressed Ctrl + D if password is None: break gh = cls._gh_module.Github(login_or_token=login, password=password) user = gh.get_user() try: user.login break # if user.login doesn't raise, authentication has been successful except cls._gh_module.GithubException as e: user = None msg = 'Wrong Github username or password; message from Github: {0}\n'.\ format(e.data.get('message', 'Unknown authentication error')) msg += 'Try again or press {0} to abort.' if current_run.UI == 'cli': msg = msg.format('Ctrl + D') else: msg = msg.format('"Cancel"') logger.warning(msg) return user
def _get_github_user(cls, login, token, **kwargs): if not cls._user: try: # try logging with token gh = cls._gh_module.Github(login_or_token=token) cls._user = gh.get_user() # try if the authentication was successful cls._user.login except cls._gh_module.GithubException: # if the token was set, it was wrong, so make sure it's reset cls._token = None # login with username/password password = DialogHelper.ask_for_password( prompt='Github Password for {username}:'.format(username=login)) gh = cls._gh_module.Github(login_or_token=login, password=password) cls._user = gh.get_user() try: cls._user.login cls._github_create_auth(**kwargs) # create auth for future use except cls._gh_module.GithubException as e: msg = 'Wrong username or password\nGitHub exception: {0}'.format(e) logger.error(msg) # reset cls._user to None, so that we don't use it if calling this multiple times cls._user = None raise exceptions.RunException(msg) return cls._user
def _github_create_twofactor_authorization(cls, ui): """Create an authorization for a GitHub user using two-factor authentication. Unlike its non-two-factor counterpart, this method does not traverse the available authentications as they are not visible until the user logs in. Please note: cls._user's attributes are not accessible until the authorization is created due to the way (py)github works. """ try: try: # This is necessary to trigger sending a 2FA key to the user auth = cls._user.create_authorization() except cls._gh_exceptions.GithubException: onetime_pw = DialogHelper.ask_for_password( ui, prompt='Your one time password:'******'repo', 'user', 'admin:public_key'], note="DevAssistant", onetime_password=onetime_pw) cls._user = cls._gh_module.Github( login_or_token=auth.token).get_user() logger.debug( 'Two-factor authorization for user "{0}" created'.format( cls._user.login)) cls._github_store_authorization(cls._user, auth) logger.debug('Two-factor authorization token stored') except cls._gh_exceptions.GithubException as e: logger.warning( 'Creating two-factor authorization failed: {0}'.format(e))
def _get_github_user(cls, login): if not cls._user: try: # try logging with token token = cls._github_token(login) gh = cls._gh_module.Github(login_or_token=token) cls._user = gh.get_user() # try if the authentication was successful cls._user.login except cls._gh_module.GithubException: # if the token was set, it was wrong, so make sure it's reset cls._token = None # login with username/password password = DialogHelper.ask_for_password( prompt='Github Password for {username}:'.format(username=login)) gh = cls._gh_module.Github(login_or_token=login, password=password) cls._user = gh.get_user() try: cls._user.login cls._github_create_auth() # create auth for future use except cls._gh_module.GithubException as e: msg = 'Wrong username or password\nGitHub exception: {0}'.format(e) # reset cls._user to None, so that we don't use it if calling this multiple times cls._user = None raise exceptions.CommandException(msg) return cls._user
def run(cls, c): if c.input_res and not isinstance(c.input_res, dict): raise exceptions.CommandException('{0} needs a mapping as input!'.format(c.comm_type)) if c.comm_type == 'ask_password': res = DialogHelper.ask_for_password(**c.input_res) elif c.comm_type == 'ask_confirm': res = DialogHelper.ask_for_confirm_with_message(**c.input_res) else: raise exceptions.CommandException('Unknown command type {ct}.'.format(ct=c.comm_type)) return bool(res), res
def run(cls, c): var, args = cls.format_args(c) if c.comm_type == 'ask_password': result = [True, DialogHelper.ask_for_password(**args)] elif c.comm_type == 'ask_confirm': result = [True, DialogHelper.ask_for_confirm_with_message(**args)] else: raise exceptions.CommandException('Unknown command type {ct}.'.format(ct=c.comm_type)) c.kwargs[var] = result[1] return result
def _github_create_twofactor_authorization(cls, ui): """Create an authorization for a GitHub user using two-factor authentication. Unlike its non-two-factor counterpart, this method does not traverse the available authentications as they are not visible until the user logs in. Please note: cls._user's attributes are not accessible until the authorization is created due to the way (py)github works. """ try: try: # This is necessary to trigger sending a 2FA key to the user auth = cls._user.create_authorization() except cls._gh_exceptions.GithubException: onetime_pw = DialogHelper.ask_for_password(ui, prompt='Your one time password:'******'repo', 'user', 'admin:public_key'], note="DevAssistant", onetime_password=onetime_pw) cls._user = cls._gh_module.Github(login_or_token=auth.token).get_user() logger.debug('Two-factor authorization for user "{0}" created'.format(cls._user.login)) cls._github_store_authorization(cls._user, auth) logger.debug('Two-factor authorization token stored') except cls._gh_exceptions.GithubException as e: logger.warning('Creating two-factor authorization failed: {0}'.format(e))