def get_token(login: hug.types.text, password: hug.types.text): try: user = User.get(login=login, password=hashlib.sha1(password.encode()).hexdigest()) return {'token': user.token.token} except User.DoesNotExist: raise HTTPError(HTTP_404)
def create_user(username: hug.types.text, password: hug.types.text, response): print(password, type(password)) session = Session() existing_user = session.query(User).filter( User.username.is_(username)).first() if existing_user: response.status = HTTP_400 return {'success': False, 'errors': ['User with this username already exists']} password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) session.add(User(username, password)) session.commit() response.status = HTTP_201 return {'success': True}
def registration(login: hug.types.text, password: hug.types.text): if len(password) >= 6: password = hashlib.sha1(password.encode()).hexdigest() else: raise HTTPError(HTTP_422) token = uuid.uuid4().hex token = Token.create(token=token) try: User.create(**locals()) except peewee.IntegrityError: token.delete() raise HTTPError(HTTP_409) return {'status': 'ok'}
def create_api_key(username: hug.types.text, password: hug.types.text, response): session = Session() user = session.query(User).filter(User.username.is_(username)).first() if not user: response.status = HTTP_404 return password_is_correct = bcrypt.checkpw(password.encode('utf-8'), user.password) if not password_is_correct: response.status = 401 return api_key = ApiKey(owner_id=user.id) session.add(api_key) session.commit() session.status = HTTP_201 return {'success': True, 'key': api_key.serialize()}