def test_login_user(self): nickname = 'Bob' password = '******' keep_login = True request = construct_dummy_request( json_body={ 'user': nickname, 'password': password, 'keep_login': keep_login, 'redirect_url': 'http://some.url' }) _tn = Translator('en') response = user_login(request) self.assertTrue(type(response) is HTTPFound) response = login_local_user(nickname, password, DummyMailer, lang=_tn) self.assertTrue(isinstance(response, dict)) self.assertNotIn('error', response) self.assertIn('user', response) response = login_local_user('definitelynotauser', r'¯\_(ツ)_/¯', DummyMailer, lang=_tn) self.assertTrue(isinstance(response, dict)) self.assertIn('error', response) self.assertNotIn('user', response)
def validate_credentials(request, **_kwargs) -> None: """ Parse credentials from POST request and validate it against DBA-S' database. :param request: :return: """ if request.errors: return nickname = request.validated['nickname'] password = request.validated['password'] del request.validated['password'] # Check in DB-AS' database, if the user's credentials are valid logged_in = login_local_user(nickname, password, request.mailer) db_user: User = logged_in.get('user') if db_user: request.validated['nickname']: str = db_user.nickname request.validated['user']: User = db_user request.validated['token'] = get_api_token(request, db_user) else: add_error(request, 'Could not login user', location="header", status_code=401)
def validate_credentials(request, **_kwargs) -> None: """ Parse credentials from POST request and validate it against DBA-S' database. :param request: :return: """ if request.errors: return secret = request.registry.settings['secret_key'] nickname = request.validated['nickname'] password = request.validated['password'] del request.validated['password'] # Check in DB-AS' database, if the user's credentials are valid logged_in = login_local_user(nickname, password, request.mailer) db_user: User = logged_in.get('user') if db_user: token = jwt.encode({ 'nickname': db_user.nickname, 'id': db_user.uid }, secret, algorithm='ES256') request.validated['nickname']: str = db_user.nickname request.validated['user']: User = db_user request.validated['token'] = token else: add_error(request, 'Could not login user', location="header", status_code=401)
def user_login(request): """ Will login the user by his nickname and password :param request: request of the web server :return: dict() with error """ LOG.debug("Login user with Nickname and Password") lang = get_language_from_cookie(request) nickname = request.validated.get('user') password = request.validated.get('password') keep_login = request.validated.get('keep_login') redirect_url = request.validated.get('redirect_url') login_data = login_local_user(nickname, password, request.mailer, lang) if not login_data.get('error'): headers, url = __refresh_headers_and_url(request, nickname, keep_login, redirect_url) sleep(0.5) return HTTPFound(location=url, headers=headers) return {'error': Translator(lang).get(_.userPasswordNotMatch)}