def createsuperuser(name): """Create a superuser admin""" try: app = create_app() app.app_context().push() with app.app_context(): user = User.query.filter(User.username == name).first() if user: raise Exception(f'The {user} already exists.') else: password_crypt = bcrypt.generate_password_hash( 'admin', 10).decode('utf-8') user = User(name, password_crypt, True, False, False, False) session.add(user) session.commit() serializer = UserSchema().dump(user) response = dict() response['user'] = serializer response[ 'password'] = '******' click.echo(Created(f'{response}')) except Exception as e: click.echo( Aborted(f'Create superuser produce the followings errors: {e}.'))
def unrevoke_token(token_id, user): """ Unrevokes the given token. Raises a TokenNotFound error if the token does not exist in the database """ try: token = TokenBlacklist.query.filter_by( id=token_id, user_identity=user['username']).one() token.revoked = False session.commit() except NoResultFound: raise TokenNotFound("Could not find the token {}".format(token_id))
def prune_database(): """ Delete tokens that have expired from the database. How (and if) you call this is entirely up you. You could expose it to an endpoint that only administrators could call, you could run it as a cron, set it up with flask cli, etc. """ now = datetime.now() expired = TokenBlacklist.query.filter(TokenBlacklist.expires < now).all() for token in expired: session.delete(token) session.commit()
def revoke_token(user, jti: str = None, token_id: int = None): """ Revokes the given token. Raises a TokenNotFound error if the token does not exist in the database """ try: if token_id: token = TokenBlacklist.query.filter_by( id=token_id, user_identity=user['username']).one() elif jti: token = TokenBlacklist.query.filter_by( jti=jti, user_identity=user['username']).one() token.revoked = True session.commit() except NoResultFound: raise TokenNotFound( user_err_msg=f"Could not find the token {token_id}")
def create(self, blueprint_name, id): serializer = self.pre_create() user = self.get_object(id) image_name = save_to_image( blueprint_name, image_file=serializer['thumbnail'], extension=serializer['extension'] ) user.thumbnail = image_name session.commit() deserializer = UserSchema().dump(user) return deserializer, HTTPStatus.OK
def update_auth( self, username: str, access_token: str, social: str, email: str, id: int, refresh_token=None ): query = session.query(Auth).filter(Auth.id == id) auth = query.first() if access_token: auth.access_token = access_token session.add(auth) session.commit() return auth
def create_auth( self, username: str, access_token: str, social: str, email: str, id: int, refresh_token=None ): auth = Auth( email=email, id=id, social=social, username=username, access_token=access_token, ) session.add(auth) session.commit() return auth
def add_token_to_database(encoded_token, identity_claim): """ Adds a new token to the database. It is not revoked when it is added. :param identity_claim: """ decoded_token = decode_token(encoded_token) jti = decoded_token['jti'] token_type = decoded_token['type'] user_identity = decoded_token[identity_claim] expires = _epoch_utc_to_datetime(decoded_token['exp']) revoked = False db_token = TokenBlacklist( jti=jti, token_type=token_type, user_identity=user_identity['username'], expires=expires, revoked=revoked, ) session.add(db_token) session.commit()