예제 #1
0
    def post(self):
        """
        Register user
        """
        data = request.json

        if Client.search().query(
                'match',
                client_id=data['client_id']).execute().hits.total != 0:
            abort(400, error='Client id already exist')

        if Client.search().query(
                'match', email=data['email']).execute().hits.total != 0:
            abort(400, error='Email already exist')

        if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
                        data['email']):
            abort(400, error='{0} not pass email regex.'.format(data['email']))

        user = Client(client_id=data['client_id'],
                      email=data['email'],
                      secret=data['client_secret'],
                      balance=0,
                      confirmed=False)

        try:
            serializer = URLSafeTimedSerializer(
                current_app.config['PRIVATE_KEY'])
            token = serializer.dumps(user.email,
                                     salt=current_app.config['SALT_KEY'])

            payload = {
                'confirm_token': token,
                'confirm_url': current_app.config['CONFIRM_BASE_URL'] + token,
            }

            msg = Message(recipients=[data['email']],
                          html=render_email('register.html', payload),
                          subject='Register')

            mail.send(msg)

            user.save()

            return 'Client successfully registered', 200

        except Exception as ex:
            current_app.logger.error(
                'Unable to register user --> {0}'.format(ex))
            abort(
                400,
                error='Unable to register user, please contact administrator')
예제 #2
0
def is_authorized_client(client_id, secret):
    """
    Verify if is authorized client

    :param client_id:
    :param secret:
    :return:
    """
    response = Client.search().query('match', client_id=client_id).execute()

    if response.hits.total == 0:
        return False

    u = response.hits[0]

    if u.check_secret(secret):
        g.user = u
        return True

    return False
예제 #3
0
    def get(self, token):
        """
        Confirm registration
        """
        serializer = URLSafeTimedSerializer(current_app.config['PRIVATE_KEY'])
        try:
            email = serializer.loads(token,
                                     salt=current_app.config['SALT_KEY'],
                                     max_age=3600)

            response = Client.search().query('match', email=email).execute()
            if response.hits.total == 0:
                abort(404, error='Token not found')

            user = response.hits[0]
            user.confirmed = True
            user.save()

            return 'Client successfully confirmed', 200

        except Exception as ex:
            current_app.logger.error(
                'Unable to confirm user --> {0}'.format(ex))
            abort(400, error='Invalid token')
예제 #4
0
import os
from app import create_app


basedir = os.path.abspath(os.path.dirname(__file__))

if __name__ == '__main__':
    flask_app = create_app(os.environ.get('APP_CONFIG', 'default'))

    with flask_app.test_request_context():
        from app.models import Client, Book, BookOffer

        Client.init()
        Book.init()
        BookOffer.init()

        response = Client.search().execute()
        if Client.search().execute().hits.total == 0:
            admin = Client(
                client_id='rastadev',
                secret='rastadev',
                email='*****@*****.**',
                confirmed=True,
                favorite_genders=['chill', 'science'],
                balance=999999999
            )
            admin.save()
            print('admin created')