def web(self, backend): auth_backend = auth_manager.get(backend) if auth_backend: try: auth_backend.update_config(self.parameters()) except AuthConfigError, err: return json_error('Failed to update configuration: %s' % err)
def action(admin, action_name, *args, **kwargs): """ Action wrapper for web context """ action_dict = available_action_dict(include_keyword=False) arguments = {} if not request.json and request.method == 'POST': return json_error('Not in json format') if request.method == 'POST': arguments = request.json try: action = action_dict[action_name]() action.phone_number = admin.phone_number action.email = admin.email ret = action.call_web_context(arguments, *args, **kwargs) except ActionError, err: return json_error('Failed to trigger action: %s' % err)
def web(self): if self.template_args(): template_args = json.loads(self.template_args()) else: template_args = {} success, message = self.send_email( subject=self.subject(), message=self.message(), to=self.to(), template=self.template(), template_args=template_args ) if success: return json_success(message) else: return json_error(message)
def login(): """ Get an authentication session Sample request to authenticate: .. code-block:: javascript POST /login HTTP/1.1 Host: janua.mydomain.com Content-Type: application/json { "username": "******", "password": "******", "language": "EN", } Sample response: .. code-block:: javascript HTTP/1.1 200 { "success": true, "message": "Successful authentication", "JanuaAuthToken": "abcdef123456789", } """ if not request.json: return make_response(json_error('Request format is not json')) if 'username' not in request.json: return make_response(json_error('Username is missing')) if 'password' not in request.json: return make_response(json_error('Password is missing')) if 'language' not in request.json: return make_response(json_error('Language is missing')) username = request.json['username'] password = request.json['password'] language = request.json['language'] admin = authenticate_admin(username, password) if admin: admin_token = serialize_token(admin.id, token_serializer) if not admin_token: return make_response(json_error('Failed to generate token')) session_lifetime = timedelta(hours=config.web.session_lifetime) expire = datetime.utcnow() + session_lifetime response = make_response( json_success('Authentication ok', JanuaAuthToken=admin_token)) response.set_cookie('role', get_role(admin), expires=expire) response.set_cookie('admin_id', str(admin.id), expires=expire) response.set_cookie('auth_token', admin_token, expires=expire) return response return make_response(json_error('Authentication failure'))
arguments = {} if not request.json and request.method == 'POST': return json_error('Not in json format') if request.method == 'POST': arguments = request.json try: action = action_dict[action_name]() action.phone_number = admin.phone_number action.email = admin.email ret = action.call_web_context(arguments, *args, **kwargs) except ActionError, err: return json_error('Failed to trigger action: %s' % err) except (ValueError, TypeError): return json_error('web context method for %s return an invalid value' % action_name) except ActionNotifyError, err: return json_error('Notify method failed: %s' % err) except Exception, err: return json_error('Bug in action %s: %s' % (action.get_name(), err)) error_msg = action.process_notify() del action if error_msg: return json_error(error_msg) return ret def urlconfig(url, role=['admin', 'supervisor']): """