Exemple #1
0
def secret_key(key=None, filename='invenio.cfg', silent=True):
    """Generate and append SECRET_KEY to invenio.cfg.

    Useful for the installation process.
    """
    print(">>> Going to generate random SECRET_KEY...")
    try:
        d = get_instance_config_object(filename)
    except Exception as e:
        print("ERROR: ", str(e), file=sys.stderr)
        sys.exit(1)
    if len(d.__dict__.get('SECRET_KEY', '')) > 0:
        print("ERROR: SECRET_KEY is already filled.")
        sys.exit(1)
    from invenio.base.config import SECRET_KEY
    if current_app.config.get('SECRET_KEY') != SECRET_KEY:
        print("WARNING: custom config package already contains SECRET_KEY.",
              file=sys.stderr)
        print(">>> No need to generate secret key.")
    else:
        if key is None:
            key = generate_secret_key()
        with current_app.open_instance_resource(filename, 'a') as config_file:
            print('SECRET_KEY =', pformat(key), file=config_file)
            print(">>> SECRET_KEY appended to `%s`." % (config_file.name, ))
Exemple #2
0
def oauth_register(account_info, form_data=None):
    """Register user if possible."""
    from invenio.modules.accounts.models import User

    email = account_info.get("email")
    if form_data and form_data.get("email"):
        email = form_data.get("email")

    if email:
        if not User.query.filter_by(email=email).first():
            # Email does not already exists. so we can proceed to register
            # user.
            u = User(
                nickname=account_info.get('nickname', ''),
                email=email,
                password=generate_secret_key(),
                note='1',  # Activated - assumes email is validated
            )

            try:
                db.session.add(u)
                db.session.commit()
                return UserInfo(u.id)
            except Exception:
                current_app.logger.exception("Cannot create user")
    return None
Exemple #3
0
def secret_key(key=None, filename='invenio.cfg', silent=True):
    """Generate and append SECRET_KEY to invenio.cfg.

    Useful for the installation process.
    """
    print(">>> Going to generate random SECRET_KEY...")
    try:
        d = get_instance_config_object(filename)
    except Exception as e:
        print("ERROR: ", str(e), file=sys.stderr)
        sys.exit(1)
    if len(d.__dict__.get('SECRET_KEY', '')) > 0:
        print("ERROR: SECRET_KEY is already filled.")
        sys.exit(1)
    from invenio.base.config import SECRET_KEY
    if current_app.config.get('SECRET_KEY') != SECRET_KEY:
        print("WARNING: custom config package already contains SECRET_KEY.",
              file=sys.stderr)
        print(">>> No need to generate secret key.")
    else:
        if key is None:
            key = generate_secret_key()
        with current_app.open_instance_resource(filename, 'a') as config_file:
            print('SECRET_KEY =', pformat(key), file=config_file)
            print(">>> SECRET_KEY appended to `%s`." % (config_file.name, ))
Exemple #4
0
def oauth_register(account_info, form_data=None):
    """Register user if possible."""
    from invenio.modules.accounts.models import User

    email = account_info.get("email")
    if form_data and form_data.get("email"):
        email = form_data.get("email")

    if email:
        if not User.query.filter_by(email=email).first():
            # Email does not already exists. so we can proceed to register
            # user.
            u = User(
                nickname=account_info.get('nickname', ''),
                email=email,
                password=generate_secret_key(),
                note='1',  # Activated - assumes email is validated
            )

            try:
                db.session.add(u)
                db.session.commit()
                return UserInfo(u.id)
            except Exception:
                current_app.logger.exception("Cannot create user")
    return None
Exemple #5
0
def oauth_authenticate(client_id, email=None, access_token=None,
                       require_existing_link=True, auto_register=False):
    """
    Authenticate an oauth authorized callback
    """
    if email is None and access_token is None:
        return False

    # Authenticate via the access token
    if access_token:
        token = RemoteToken.get_by_token(client_id, access_token)

        if token:
            u = UserInfo(token.remote_account.user_id)
            if login_user(u):
                return True

    if email:
        if authenticate(email):
            if not require_existing_link:
                return True

            # Pre-existing link required so check
            account = RemoteAccount.get(current_user.get_id(), client_id)
            if account:
                return True

            # Account doesn't exists, and thus the user haven't linked
            # the accounts
            logout_user()
            return None
        elif auto_register:
            from invenio.modules.accounts.models import User
            if not User.query.filter_by(email=email).first():
                # Email doesn't exists so we can proceed to register user.
                u = User(
                    nickname="",
                    email=email,
                    password=generate_secret_key(),
                    note='1',  # Activated
                )

                try:
                    db.session.add(u)
                    db.session.commit()
                    login_user(UserInfo(u.id))
                    return True
                except Exception:
                    pass
    return False