Example #1
0
    def get_next_action(self, userid, session=None):
        """
        Return next pending action for userid and session.
        If session is None, search actions with no session,
        otherwise search actions with either no session
        or with the specified session.
        If there is no pending action, return None

        :param userid: The id of the user with possible pending actions
        :param session: The IdP session for the user

        :type userid: str
        :type session: str

        :rtype: eduid_userdb.actions:Action or None
        """
        cachekey = self._update_cache(userid, session)
        action = None
        if cachekey in self._cache:
            try:
                action_doc = self._cache[cachekey].pop()
            except IndexError:
                self.clean_cache(userid, session)
            else:
                action = Action(data=action_doc)
        return action
Example #2
0
def get_actions():
    actions = get_next_action()
    if not actions['action']:
        return json.dumps({
            'action': False,
            'url': actions['idp_url'],
            'payload': {
                'csrf_token': session.new_csrf_token()
            }
        })
    action_type = session['current_plugin']
    plugin_obj = current_app.plugins[action_type]()
    action = Action(data=session['current_action'])
    current_app.logger.info('Starting pre-login action {} '
                            'for userid {}'.format(action.action_type,
                                                   session['userid']))
    try:
        url = plugin_obj.get_url_for_bundle(action)
        return json.dumps({
            'action': True,
            'url': url,
            'payload': {
                'csrf_token': session.new_csrf_token()
            }
        })
    except plugin_obj.ActionError as exc:
        _aborted(action, exc)
        abort(500)
Example #3
0
def _do_action():
    action_type = session.get('current_plugin')
    if not action_type:
        abort(403)

    plugin_obj = current_app.plugins[action_type]()
    old_format = 'user_oid' in session['current_action']
    action = Action(data=session['current_action'], old_format=old_format)
    try:
        data = plugin_obj.perform_step(action)
    except plugin_obj.ActionError as exc:
        return _aborted(action, exc)
    except plugin_obj.ValidationError as exc:
        errors = exc.args[0]
        current_app.logger.info(
            'Validation error {} for step {} of action {}'.format(
                errors, session['current_step'], action))
        session['current_step'] -= 1
        return error_response(payload={'errors': errors},
                              message=CommonMsg.form_errors)

    eppn = session.get('user_eppn')
    if session['total_steps'] == session['current_step']:
        current_app.logger.info(
            'Finished pre-login action {} for eppn {}'.format(
                action.action_type, eppn))
        return success_response(payload=dict(data=data),
                                message=ActionsMsg.action_completed)

    current_app.logger.info(
        'Performed step {} for action {} for eppn {}'.format(
            action.action_type, session['current_step'], eppn))
    session['current_step'] += 1
    return success_response(payload={'data': data}, message=None)
Example #4
0
def get_actions():
    user = current_app.central_userdb.get_user_by_eppn(
        session.get('user_eppn'))
    actions = get_next_action(user)
    if not actions['action']:
        return json.dumps({
            'action': False,
            'url': actions['idp_url'],
            'payload': {
                'csrf_token': session.new_csrf_token()
            }
        })
    action_type = session['current_plugin']
    plugin_obj = current_app.plugins[action_type]()
    old_format = 'user_oid' in session['current_action']
    action = Action(data=session['current_action'], old_format=old_format)
    current_app.logger.info('Starting pre-login action {} '
                            'for user {}'.format(action.action_type, user))
    try:
        url = plugin_obj.get_url_for_bundle(action)
        return json.dumps({
            'action': True,
            'url': url,
            'payload': {
                'csrf_token': session.new_csrf_token()
            }
        })
    except plugin_obj.ActionError as exc:
        _aborted(action, exc)
        abort(500)
Example #5
0
def get_config():
    try:
        action_type = session['current_plugin']
    except KeyError:
        abort(403)
    plugin_obj = current_app.plugins[action_type]()
    action = Action(data=session['current_action'])
    try:
        config = plugin_obj.get_config_for_bundle(action)
        config['csrf_token'] = session.new_csrf_token()
        return config
    except plugin_obj.ActionError as exc:
        return {'_status': 'error', 'message': exc.args[0]}
Example #6
0
    def add_action(self, userid=None, action_type=None, preference=100,
                   session=None, params=None, data=None):
        """
        Add an action to the DB.

        :param userid: The id of the user who has to perform the action
        :param action_type: the kind of action to be performed
        :param preference: preference to order actions
        :param session: The IdP session for the user
        :param params: Any params the action may need
        :param data: all the previous params together

        :type userid: bson.ObjectId
        :type action_type: str
        :type preference: int
        :type session: str
        :type params: dict
        :type data: dict

        :rtype: Action
        """
        if data is None:
            data = {'user_oid': userid,
                    'action': action_type,
                    'preference': preference,
                    }
            if session is not None:
                data['session'] = session
            if params is not None:
                data['params'] = params

        # XXX deal with exceptions here ?
        action = Action(data = data)
        result = self._coll.insert(action.to_dict())
        if result == action.action_id:
            return action
        logger.error("Failed inserting action {!r} into db".format(action))
        raise ActionDBError('Failed inserting action into db')
Example #7
0
def post_action():
    if not request.data or \
            session.get_csrf_token() != json.loads(request.data)['csrf_token']:
        abort(400)
    try:
        action_type = session['current_plugin']
    except KeyError:
        abort(403)
    plugin_obj = current_app.plugins[action_type]()
    action = Action(data=session['current_action'])
    errors = {}
    try:
        data = plugin_obj.perform_step(action)
    except plugin_obj.ActionError as exc:
        return _aborted(action, exc)

    except plugin_obj.ValidationError as exc:
        errors = exc.args[0]
        current_app.logger.info('Validation error {0} '
                                'for step {1} of action {2}'.format(
                                    str(errors), str(session['current_step']),
                                    str(action)))
        session['current_step'] -= 1
        return {
            '_status': 'error',
            'errors': errors,
            'csrf_token': session.new_csrf_token()
        }

    if session['total_steps'] == session['current_step']:
        current_app.logger.info('Finished pre-login action {0} '
                                'for userid {1}'.format(
                                    action.action_type, session['userid']))
        return {
            'message': 'actions.action-completed',
            'data': data,
            'csrf_token': session.new_csrf_token()
        }

    current_app.logger.info('Performed step {} for action {} '
                            'for userid {}'.format(action.action_type,
                                                   session['current_step'],
                                                   session['userid']))
    session['current_step'] += 1

    return {'data': data, 'csrf_token': session.new_csrf_token()}