示例#1
0
def create_web_app() -> Flask:
    """Initialize and configure the accounts application."""
    app = Flask('accounts')
    app.config.from_pyfile('config.py')

    SessionStore.init_app(app)
    legacy.init_app(app)
    users.init_app(app)

    app.register_blueprint(ui.blueprint)
    Base(app)    # Gives us access to the base UI templates and resources.
    auth.Auth(app)  # Handless sessions and authn/z.
    s3.init_app(app)

    middleware = [auth.middleware.AuthMiddleware]
    if app.config['VAULT_ENABLED']:
        middleware.insert(0, vault.middleware.VaultMiddleware)
    wrap(app, middleware)
    if app.config['VAULT_ENABLED']:
        app.middlewares['VaultMiddleware'].update_secrets({})

    if app.config['CREATE_DB']:
        with app.app_context():
            legacy.create_all()
            users.create_all()

    return app
示例#2
0
    def setUp(self):
        self.app = create_web_app()
        self.app.config['CLASSIC_COOKIE_NAME'] = 'foo_tapir_session'
        self.app.config['AUTH_SESSION_COOKIE_NAME'] = 'baz_session'
        self.app.config['AUTH_SESSION_COOKIE_SECURE'] = '0'
        self.app.config['SESSION_DURATION'] = self.expiry
        self.app.config['JWT_SECRET'] = self.secret
        self.app.config['CLASSIC_DATABASE_URI'] = f'sqlite:///{self.db}'
        self.app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{self.db}'
        self.app.config['REDIS_HOST'] = 'localhost'
        self.app.config['REDIS_PORT'] = '7000'
        self.app.config['REDIS_CLUSTER'] = '1'

        with self.app.app_context():
            legacy.drop_all()
            users.drop_all()
            legacy.create_all()
            users.create_all()

            with users.transaction() as session:
                # We have a good old-fashioned user.
                db_user = users.models.DBUser(
                    user_id=1,
                    first_name='first',
                    last_name='last',
                    suffix_name='iv',
                    email='*****@*****.**',
                    policy_class=2,
                    flag_edit_users=1,
                    flag_email_verified=1,
                    flag_edit_system=0,
                    flag_approved=1,
                    flag_deleted=0,
                    flag_banned=0,
                    tracking_cookie='foocookie',
                )
                db_nick = users.models.DBUserNickname(nick_id=1,
                                                      nickname='foouser',
                                                      user_id=1,
                                                      user_seq=1,
                                                      flag_valid=1,
                                                      role=0,
                                                      policy=0,
                                                      flag_primary=1)
                salt = b'fdoo'
                password = b'thepassword'
                hashed = hashlib.sha1(salt + b'-' + password).digest()
                encrypted = b64encode(salt + hashed)
                db_password = users.models.DBUserPassword(
                    user_id=1, password_storage=2, password_enc=encrypted)
                session.add(db_user)
                session.add(db_password)
                session.add(db_nick)
示例#3
0
def create_web_app() -> Flask:
    """Initialize and configure the accounts application."""
    app = Flask('accounts')
    app.config.from_pyfile('config.py')

    sessions.init_app(app)
    legacy.init_app(app)
    users.init_app(app)

    app.register_blueprint(ui.blueprint)
    Base(app)    # Gives us access to the base UI templates and resources.
    auth.Auth(app)  # Handless sessions and authn/z.
    wrap(app, [auth.middleware.AuthMiddleware])

    if app.config['CREATE_DB']:
        legacy.create_all()
        users.create_all()
    return app
示例#4
0
def create_user(username: str, email: str, password: str,
                first_name: str, last_name: str, suffix_name: str = '',
                affiliation: str = 'FSU', home_page: str = 'https://asdf.com'):
    """Create a new user. For dev/test purposes only."""
    app = create_web_app()
    with app.app_context():
        legacy.create_all()
        users.create_all()

    with util.transaction() as session:
        ip_addr = '127.0.0.1'
        joined_date = util.epoch(datetime.now().replace(tzinfo=EASTERN))
        db_user = models.DBUser(
            first_name=first_name,
            last_name=last_name,
            suffix_name=suffix_name,
            share_first_name=1,
            share_last_name=1,
            email=email,
            flag_approved=1,
            flag_deleted=0,
            flag_banned=0,
            flag_edit_users=0,
            flag_edit_system=0,
            flag_email_verified=1,
            share_email=8,
            email_bouncing=0,
            policy_class=2,  # Public user. TODO: consider admin.
            joined_date=joined_date,
            joined_ip_num=ip_addr,
            joined_remote_host=ip_addr
        )
        session.add(db_user)

        # Create a username.
        db_nick = models.DBUserNickname(
            user=db_user,
            nickname=username,
            flag_valid=1,
            flag_primary=1
        )

        # Create the user's profile.
        archive, subject_class = _random_category()
        db_profile = models.DBProfile(
            user=db_user,
            country='us',
            affiliation=affiliation,
            url=home_page,
            rank=random.randint(1, 5),
            archive=archive,
            subject_class=subject_class,
            original_subject_classes='',
            flag_group_math=1 if _prob(5) else 0,
            flag_group_cs=1 if _prob(5) else 0,
            flag_group_nlin=1 if _prob(5) else 0,
            flag_group_q_bio=1 if _prob(5) else 0,
            flag_group_q_fin=1 if _prob(5) else 0,
            flag_group_stat=1 if _prob(5) else 0
        )

        # Set the user's password.
        db_password = models.DBUserPassword(
            user=db_user,
            password_storage=2,
            password_enc=util.hash_password(password)
        )

        session.add(db_password)
        session.add(db_nick)
        session.add(db_profile)

        session.commit()
示例#5
0
"""Provides application for development purposes."""

from accounts.factory import create_web_app
from accounts.services import legacy, users

app = create_web_app()
legacy.create_all()