Exemple #1
0
def login():
    '''
    User Login
    '''
    post_data = request.get_json()
    try:
        result = mongo.db.users.find_one({'username': post_data["username"]})
        if result != None:
            if result["password"] == post_data["password"]:
                auth_token = User.encode_auth_token(result["_id"])
                if auth_token:
                    responseObject = {
                        'status': 'success',
                        'message': 'Logged in',
                        'auth_token': auth_token
                    }
                    return make_response(jsonify(responseObject)), 200
    except Exception as e:
        responseObject = {
            'status': 'fail',
            'message': 'login failed',
        }
        return make_response(jsonify(responseObject)), 400
    responseObject = {
        'status': 'fail',
        'message': 'login failed',
    }
    return make_response(jsonify(responseObject)), 400
Exemple #2
0
    def test_decode_auth_token(self):
        user = User(username='******', email='*****@*****.**', password='******')
        auth_token = user.encode_auth_token(1)
        self.assertTrue(isinstance(auth_token, bytes))

        self.assertTrue(
            User.decode_auth_token(auth_token.decode("utf-8")) == 1)
Exemple #3
0
def register():
    '''
    User registration
    '''
    post_data = request.get_json()
    result = mongo.db.users.find_one({'username': post_data["username"]})
    if result == None:
        try:
            # Create user
            uid = getNextSequence("userid")
            user = User(_id=uid,
                        username=post_data["username"],
                        email=post_data["email"],
                        password=post_data["password"])

            user_json = user.__dict__
            mongo.db.users.insert_one(user_json)
            auth_token = user.encode_auth_token(uid)

            # Copy file to Directory
            store_files(uid)

            responseObject = {
                'status': 'success',
                'message': 'Registered User',
                'auth_token': auth_token
            }
            return make_response(jsonify(responseObject)), 201
        except Exception as e:
            # TODO: logging error

            print e
            result = mongo.db.users.delete_one(user_json)
            print result.deleted_count
            responseObject = {
                'status': 'fail',
                'message': 'error registering, please try again'
            }
            return make_response(jsonify(responseObject)), 401
    else:
        responseObject = {'status': 'fail', 'message': 'user already exists'}
        return make_response(jsonify(responseObject)), 202
Exemple #4
0
    def post(self):
        # get the post data
        post_data = request.get_json()

        email = post_data.get('email') or post_data.get('username')
        password = post_data.get('password')
        phone = post_data.get('phone')
        referral_code = post_data.get('referral_code')

        if phone is not None:
            # this is a registration from a mobile device THUS a vendor or recipient.
            response_object, response_code = UserUtils.proccess_create_or_modify_user_request(
                post_data,
                is_self_sign_up=True,
            )

            if response_code == 200:
                db.session.commit()

            return make_response(jsonify(response_object)), response_code

        email_ok = False

        whitelisted_emails = EmailWhitelist.query\
            .filter_by(referral_code=referral_code, used=False) \
            .execution_options(show_all=True).all()

        selected_whitelist_item = None
        exact_match = False

        tier = None
        sempoadmin_emails = current_app.config['SEMPOADMIN_EMAILS']

        if sempoadmin_emails != [''] and email in sempoadmin_emails:
            email_ok = True
            tier = 'sempoadmin'

        for whitelisted in whitelisted_emails:
            if whitelisted.allow_partial_match and whitelisted.email in email:
                email_ok = True
                tier = whitelisted.tier
                selected_whitelist_item = whitelisted
                exact_match = False
                continue
            elif whitelisted.email == email:
                email_ok = True

                whitelisted.used = True
                tier = whitelisted.tier
                selected_whitelist_item = whitelisted
                exact_match = True
                continue

        if not email_ok:
            response_object = {
                'status': 'fail',
                'message': 'Invalid email domain.',
            }
            return make_response(jsonify(response_object)), 403

        if len(password) < 7:
            response_object = {
                'status': 'fail',
                'message': 'Password must be at least 6 characters long',
            }
            return make_response(jsonify(response_object)), 403

        # check if user already exists
        user = User.query.filter_by(email=email).execution_options(show_all=True).first()
        if user:
            response_object = {
                'status': 'fail',
                'message': 'User already exists. Please Log in.',
            }
            return make_response(jsonify(response_object)), 403

        if tier is None:
            tier = 'subadmin'

        if selected_whitelist_item:
            organisation = selected_whitelist_item.organisation
        else:
            organisation = Organisation.master_organisation()

        user = User(blockchain_address=organisation.primary_blockchain_address)

        user.create_admin_auth(email, password, tier, organisation)

        # insert the user
        db.session.add(user)

        db.session.flush()

        if exact_match:
            user.is_activated = True

            auth_token = user.encode_auth_token()

            # Possible Outcomes:
            # TFA required, but not set up
            # TFA not required

            tfa_response_oject = tfa_logic(user, tfa_token=None)
            if tfa_response_oject:
                tfa_response_oject['auth_token'] = auth_token.decode()

                db.session.commit()  # need this here to commit a created user to the db

                return make_response(jsonify(tfa_response_oject)), 401

            # Update the last_seen TS for this user
            user.update_last_seen_ts()

            response_object = create_user_response_object(user, auth_token, 'Successfully activated.')

            db.session.commit()

            return make_response(jsonify(response_object)), 201

        activation_token = user.encode_single_use_JWS('A')

        send_activation_email(activation_token, email)

        db.session.commit()

        # generate the auth token
        response_object = {
            'status': 'success',
            'message': 'Successfully registered. You must activate your email.',
        }

        return make_response(jsonify(response_object)), 201
Exemple #5
0
 def test_encode_auth_token(self):
     user = User(username='******', email='*****@*****.**', password='******')
     auth_token = user.encode_auth_token(1)
     self.assertTrue(isinstance(auth_token, bytes))
     print auth_token