Пример #1
0
    def user_register(self, username, password):

        hmac_key = Util.generateRandomBytes(32)
        encrypted_password = Util.getEncryptedPassword(hmac_key, password)
        user_hash = Util.generateUserHash(username)

        try:
            db.session.add(User(username, user_hash, email=None))
            db.session.add(
                Authentication(user_hash, encrypted_password, hmac_key))
            db.session.commit()
        except sqlalchemy.exc.IntegrityError as exc:
            current_app.logger.critical("user_register: Integrity error: %s" %
                                        exc)
            return None, ResultCode.FormatError
        except Exception as exc:
            current_app.logger.critical("user_register: Unknown error: %s" %
                                        exc)
            return None, ResultCode.DBError

        return "ok", ResultCode.Success
Пример #2
0
def create_app(DBURL=None):

    app = Flask(__name__)

    try:
        app.config.from_pyfile('../sensors.conf')
    except FileNotFoundError as exc:
        app.logger.critical("'../sensors.conf' is not found.")
        raise FileNotFoundError(exc)

    try:
        if DBURL is not None:
            dburl = DBURL
        else:
            dburl = app.config['DBURL']

        app.config['SQLALCHEMY_DATABASE_URI'] = dburl
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    except KeyError as exc:
        app.logger.critical(
            "DBURL is not set. please set dburl at sensors.conf!")
        raise KeyError(exc)

    app.config["SECRET_KEY"] = Util.generateRandomBytes(32)
    app.config['JSON_AS_ASCII'] = False

    Util.MaxUsernameLength = app.config["MAX_USERID_LENGTH"]
    Util.MaxUserPassLength = app.config["MAX_USERPASS_LENGTH"]

    csrf = CSRFProtect(app)
    csrf.init_app(app)

    # ビューの登録
    app.register_blueprint(webui, url_prefix='/')
    app.register_blueprint(api, url_prefix='/api/')

    # データベースの登録
    db.init_db(app)

    return app