Example #1
0
 def get_current_user(self):
     try:
         return User(self.request.headers.get('Authorization')
                     or self.get_query_argument('token', default=None),
                     verify=False)
     except jwt.InvalidTokenError:
         return User(None)
Example #2
0
    def post(self, handler):
        if not self.template:
            raise exceptions.BadRequest(
                detail=
                'template must be provided via collection.plugins.template')

        if not self.from_email:
            raise exceptions.BadRequest(
                detail=
                'fromEmail must be provided via collection.plugins.fromEmail')

        if not handler.payload:
            raise exceptions.BadRequest()

        if not handler.payload.attributes.get('id'):
            raise exceptions.BadRequest(detail='Id must be provided')

        try:
            doc = self.collection.read(handler.payload.attributes['id'])
        except exceptions.NotFound:
            return handler.write({
                'data': {
                    'id': handler.payload.attributes.get('id'),
                    'type': 'reset',
                    'attributes': {
                        'status': 'success'
                    }
                }
            })

        email = doc.data.get(self.email_field, 'null')
        if re.match(EMAIL_RE, email) is None:
            raise exceptions.BadRequest(
                detail='"{}" at "{}" is not a valid email'.format(
                    email, self.email_field))

        namespace = handler._view._namespace  # A little hack never hurt anyone
        from jam.auth.providers.self import SelfAuthProvider
        user = User.create(SelfAuthProvider.type,
                           '{}:{}'.format(namespace.ref, self.collection.ref),
                           doc.ref,
                           exp=8)

        try:
            sg = sendgrid.SendGridClient(self.sendgrid_key, raise_errors=True)
            mail = sendgrid.Mail(to=email)

            mail.set_from(self.from_email)
            mail.add_substitution(':token', user.token.decode())
            mail.add_substitution(':user', user.id)
            mail.add_filter('templates', 'enable', 1)
            mail.add_filter('templates', 'template_id', self.template)
            # Sendgrid requires subject text and html to be set to a non falsey value
            # It is highly recommended that you overwrite these in your own templates
            mail.set_subject('JamDB password reset')
            mail.set_text('Your temporary token is :token')
            mail.set_html('Your temporary token is :token')

            logger.info(sg.send(mail))
        except sendgrid.SendGridError:
            logger.exception('Sendgrid Error:')
            raise exceptions.ServiceUnavailable(
                detail='Unable to submit request to sendgrid')

        return handler.write({
            'data': {
                'id': handler.payload.attributes.get('id'),
                'type': 'reset',
                'attributes': {
                    'status': 'success'
                }
            }
        })
Example #3
0
 async def authenticate(self, user, data):
     if user and user.provider == self.type and self.refreshable:
         return User.create(*await self._refresh(user))
     return User.create(*await self._authenticate(data))
Example #4
0
    def post(self, handler):
        if not self.template:
            raise exceptions.BadRequest(detail='template must be provided via collection.plugins.template')

        if not self.from_email:
            raise exceptions.BadRequest(detail='fromEmail must be provided via collection.plugins.fromEmail')

        if not handler.payload:
            raise exceptions.BadRequest()

        if not handler.payload.attributes.get('id'):
            raise exceptions.BadRequest(detail='Id must be provided')

        try:
            doc = self.collection.read(handler.payload.attributes['id'])
        except exceptions.NotFound:
            return handler.write({
                'data': {
                    'id': handler.payload.attributes.get('id'),
                    'type': 'reset',
                    'attributes': {
                        'status': 'success'
                    }
                }
            })

        email = doc.data.get(self.email_field, 'null')
        if re.match(EMAIL_RE, email) is None:
            raise exceptions.BadRequest(detail='"{}" at "{}" is not a valid email'.format(email, self.email_field))

        namespace = handler._view._namespace  # A little hack never hurt anyone
        from jam.auth.providers.self import SelfAuthProvider
        user = User.create(SelfAuthProvider.type, '{}:{}'.format(namespace.ref, self.collection.ref), doc.ref, exp=8)

        try:
            sg = sendgrid.SendGridClient(self.sendgrid_key, raise_errors=True)
            mail = sendgrid.Mail(to=email)

            mail.set_from(self.from_email)
            mail.add_substitution(':token', user.token.decode())
            mail.add_substitution(':user', user.id)
            mail.add_filter('templates', 'enable', 1)
            mail.add_filter('templates', 'template_id', self.template)
            # Sendgrid requires subject text and html to be set to a non falsey value
            # It is highly recommended that you overwrite these in your own templates
            mail.set_subject('JamDB password reset')
            mail.set_text('Your temporary token is :token')
            mail.set_html('Your temporary token is :token')

            logger.info(sg.send(mail))
        except sendgrid.SendGridError:
            logger.exception('Sendgrid Error:')
            raise exceptions.ServiceUnavailable(detail='Unable to submit request to sendgrid')

        return handler.write({
            'data': {
                'id': handler.payload.attributes.get('id'),
                'type': 'reset',
                'attributes': {
                    'status': 'success'
                }
            }
        })