Exemple #1
0
    def test_whitelist_add_duplicate_email(self):
        """Test that when a duplicate email is added to the whitelist it is
        rejected.
        """
        query_string_data_1 = {
            "email": "*****@*****.**",
            "user_type": "user"
        }
        query_string_data_2 = {
            "email": "*****@*****.**",
            "user_type": "editor"
        }
        self.request_with_role('/whitelist/add',
                               method='POST',
                               data=query_string_data_1,
                               follow_redirects=True)

        resp = self.request_with_role('/whitelist/add',
                                      method='POST',
                                      data=query_string_data_2,
                                      follow_redirects=True)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Whitelist.objects().count(), 1)
        wl = Whitelist.objects().get(email="*****@*****.**")
        self.assertEqual(wl.user_type, "user")
Exemple #2
0
def delete(user_id):
    """Delete the user with id ``user_id``

    If the ``revoke`` property is set to true,
    then the user will be removed from the whitelist, and not be
    allowed to make an account again.

    **Route:** ``/admin/users/delete/<user_id>``

    **Methods:** ``POST``
    """
    object_id = ObjectId(user_id)
    if User.objects(id=object_id).count() != 1:
        abort(401)
    user = User.objects().get(id=object_id)

    # Update whitelist
    try:
        wl = Whitelist.objects().get(email=user.email)
        wl.redeemed = False
        wl.save()
    except DoesNotExist:
        pass
    user.delete()

    # Log out if a user is attempting to delete themselves
    if "gplus_id" in session and user.gplus_id == session["gplus_id"]:
        flash("You deleted yourself successfully. Logging out.", MESSAGE_FLASH)
        return redirect(url_for(".logout"), 303)
    flash("User deleted successfully.", MESSAGE_FLASH)

    return redirect(url_for(".index"), code=303)
Exemple #3
0
 def test_whitelist_add_valid_input(self):
     """Test that when valid input is provided, a user is added to the
     whitelist.
     """
     query_string_data = {
         "email": "*****@*****.**",
         "user_type": "user"
     }
     resp = self.request_with_role('/whitelist/add',
                                   method='POST',
                                   data=query_string_data,
                                   follow_redirects=True)
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(Whitelist.objects().count(), 1)
     user = Whitelist.objects().get(email="*****@*****.**")
     self.assertEqual(user.user_type, "user")
Exemple #4
0
def delete(user_id):
    """Delete the user with id `user_id`

    If the `revoke` property is set to true,
    then the user will be removed from the whitelist, and not be
    allowed to make an account again.
    """
    object_id = ObjectId(user_id)
    if User.objects(id=object_id).count() != 1:
        abort(401)
    user = User.objects().get(id=object_id)

    # Update whitelist
    try:
        wl = Whitelist.objects().get(email=user.email)
        wl.redeemed = False
        wl.save()
    except DoesNotExist:
        pass
    user.delete()

    # Log out if a user is attempting to delete themselves
    if 'gplus_id' in session and user.gplus_id == session['gplus_id']:
        flash('You deleted yourself successfully. Logging out.')
        return redirect(url_for('.logout'), 303)
    flash('User deleted successfully.')

    return redirect(url_for('.index'), code=303)
Exemple #5
0
    def __call__(self, form, field):
        """Called internally by :mod:`wtforms` on validation of the field.

        :param form: The parent form
        :type form: :class:`Form`
        :param field: The field to validate
        :type field: :class:`Field`

        :raises: :class:`wtforms.validators.ValidationError`
        """
        if form.user_type.data != 'fake_user' and \
                Whitelist.objects(email=field.data).count():
            raise ValidationError(self.message)
Exemple #6
0
    def test_whitelist_revoke(self):
        """Test removing an email from the waitlist.

        Adds num_trials emails to the whitelist, deletes some of them,
        and checks to make sure the right ones were deleted."""
        num_trials = 20
        # Post `num_trials` new whitelist entries
        for i in range(num_trials):
            query_string_data = {
                "email": "*****@*****.**" % i,
                "user_type": "user"
            }
            resp = self.request_with_role('/whitelist/add',
                                          method='POST',
                                          data=query_string_data,
                                          follow_redirects=True)

            # No error pages
            self.assertEqual(resp.status_code, 200)

        # `num_trials` whitelist items added.
        self.assertEqual(Whitelist.objects().count(), num_trials)

        delete = [i for i in range(num_trials) if i % 3 == 0]
        for i in range(num_trials):
            if i in delete:
                resp = self.request_with_role(
                    '/whitelist/delete/email%[email protected]' % i,
                    method='POST')
                self.assertEqual(resp.status_code, 200)

        for i in range(num_trials):
            in_list = Whitelist.objects(
                email="*****@*****.**" % i).count() == 1
            if i in delete:
                self.assertFalse(in_list)
            else:
                self.assertTrue(in_list)
Exemple #7
0
def index():
    """View and manage users

    Whitelisted users are the only ones allowed to make user accounts.
    """
    upload_form = UploadImageForm()
    whitelist_form = AddToWhitelistForm()
    return render_template('admin/users/users.html',
                           whitelist_form=whitelist_form,
                           upload_form=upload_form,
                           whitelist=Whitelist.objects(redeemed=False),
                           users=User.objects(),
                           images=Image.objects(),
                           current_user=g.user)
Exemple #8
0
def create_profile():
    """Create a profile (filling in the form with openid data), and
    register it in the database.

    **Route:** ``/admin/create-profile``

    **Methods:** ``GET, POST``
    """
    if g.user is not None and 'gplus_id' in session:
        # use code=303 to avoid POSTing to the next page.
        return redirect(url_for('admin.index'), code=303)
    form = CreateProfileForm(request.form,
                             name=request.args['name'],
                             email=request.args['email'],
                             next=request.args['next'])
    if form.validate_on_submit():
        if User.objects(email=form.email.data).count() != 0:
            # A user with this email already exists.  Override it.
            user = User.objects.get(email=form.email.data)
            user.openid = session['openid']
            user.name = form.name.data
            flash('Account with this email already exists.  Overridden.',
                  MESSAGE_FLASH)
            user.register_login()
            user.save()
        else:
            # Retreive their user type from the whitelist then remove them.
            wl = Whitelist.objects().get(email=form.email.data)
            user_type = wl.user_type
            wl.redeemed = True
            wl.save()
            # Create a brand new user
            user = User(email=form.email.data,
                        name=form.name.data,
                        gplus_id=session['gplus_id'],
                        user_type=user_type,
                        image_url=request.args.get('image_url'))
            flash('Account created successfully.', MESSAGE_FLASH)
            user.register_login()
            user.save()

        # redirect to the next url or the root of the application ('/')
        if form.next.data:
            # use code=303 to avoid POSTing to the next page.
            return redirect(form.next.data, code=303)
        # use code=303 to avoid POSTing to the next page.
        return redirect('/', code=303)

    return render_template('admin/auth/create_profile.html',
                           image_url=request.args.get('image_url'), form=form)
Exemple #9
0
 def test_whitelist_add_no_user_type(self):
     """Test that when no user type is provided, then no user is added to
     the whitelist.
     """
     query_string_data = {
         "email": "*****@*****.**",
         "user_type": None
     }
     resp = self.request_with_role('/whitelist/add',
                                   method='POST',
                                   query_string=query_string_data,
                                   follow_redirects=True)
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(Whitelist.objects().count(), 0)
Exemple #10
0
 def test_whitelist_add_invalid_email(self):
     """Test that when an invalid email is provided, no user is added to the
     whitelist.
     """
     query_string_data = {
         "email": "notanemail",
         "user_type": "user"
     }
     resp = self.request_with_role('/whitelist/add',
                                   method='POST',
                                   query_string=query_string_data,
                                   follow_redirects=True)
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(Whitelist.objects().count(), 0)
Exemple #11
0
def index():
    """View and manage users

    Whitelisted users are the only ones allowed to make user accounts.
    """

    upload_form = UploadImageForm()
    whitelist_form = AddToWhitelistForm()
    return render_template('admin/users/users.html',
                           whitelist_form=whitelist_form,
                           upload_form=upload_form,
                           whitelist=Whitelist.objects(redeemed=False),
                           users=User.objects(),
                           images=Image.objects(),
                           current_user=g.user)
Exemple #12
0
def delete(email):
    """Delete ``email`` from the whitelist.

    **Route:** ``/admin/whitelist/delete/<email>``

    **Methods:** ``POST``

    :param str email: The email address to delete from the whitelist.
    """
    if Whitelist.objects(email=email).count() > 0:
        Whitelist.objects.get(email=email).delete()
        flash("Whitelist entry revoked successfully.", MESSAGE_FLASH)
        return redirect(url_for('users.index'))
    flash('No such user in the database.', ERROR_FLASH)
    return redirect(url_for('users.index'))
Exemple #13
0
def create_profile():
    """Create a profile (filling in the form with openid data), and
    register it in the database.
    """
    if g.user is not None and 'gplus_id' in session:
        # use code=303 to avoid POSTing to the next page.
        return redirect(url_for('admin.index'), code=303)
    form = CreateProfileForm(request.form,
                             name=request.args['name'],
                             email=request.args['email'],
                             next=request.args['next'])
    if form.validate_on_submit():
        if User.objects(email=form.email.data).count() != 0:
            # A user with this email already exists.  Override it.
            user = User.objects.get(email=form.email.data)
            user.openid = session['openid']
            user.name = form.name.data
            flash('Account with this email already exists.  Overridden.')
            user.register_login()
            user.save()
        else:
            # Retreive their user type from the whitelist then remove them.
            wl = Whitelist.objects().get(email=form.email.data)
            user_type = wl.user_type
            wl.redeemed = True
            wl.save()
            # Create a brand new user
            user = User(email=form.email.data,
                        name=form.name.data,
                        gplus_id=session['gplus_id'],
                        user_type=user_type,
                        image_url=request.args.get('image_url'))
            flash('Account created successfully.')
            user.register_login()
            user.save()

        # redirect to the next url or the root of the application ('/')
        if form.next.data:
            # use code=303 to avoid POSTing to the next page.
            return redirect(form.next.data, code=303)
        # use code=303 to avoid POSTing to the next page.
        return redirect('/', code=303)

    return render_template('admin/auth/create_profile.html',
                           image_url=request.args.get('image_url'),
                           form=form)
Exemple #14
0
def index():
    """View and manage users.

    Whitelisted users are the only ones allowed to make user accounts.

    **Route:** ``/admin/users``

    **Methods:** ``GET``
    """

    upload_form = UploadImageForm()
    whitelist_form = AddToWhitelistForm()
    return render_template(
        "admin/users/users.html",
        whitelist_form=whitelist_form,
        upload_form=upload_form,
        whitelist=Whitelist.objects(redeemed=False),
        users=User.objects(),
        images=Image.objects(),
        current_user=g.user,
    )
Exemple #15
0
    def test_whitelist_add_email_of_existing_user(self):
        """Test that when a email is added to the whitelist that belongs to
        an existing user, it should be rejected.
        """
        # Add a user to the database
        user = User(name="Test User", email="*****@*****.**",
                    gplus_id="test123", user_type="admin")
        user.save()
        self.assertEqual(User.objects(email="*****@*****.**").count(), 1)

        # Post a new whitelist entry
        query_string_data = {
            "email": "*****@*****.**",
            "user_type": "user"
        }
        resp = self.request_with_role('/whitelist/add',
                                      method='POST',
                                      data=query_string_data,
                                      follow_redirects=True)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Whitelist.objects().count(), 0)
Exemple #16
0
def store_token():
    """Do the oauth flow for Google plus sign in, storing the access token
    in the session, and redircting to create an account if appropriate.

    Because this method will be called from a $.ajax() request in JavaScript,
    we can't return redirect(), so instead this method returns the URL that
    the user should be redirected to, and the redirect happens in JavaScript:
        success: function(response) {
            window.location.href = response;
        }
    """
    if request.args.get('state', '') != session.get('state'):
        return json_response('Invalid state parameter.', 401)

    del session['state']
    code = request.data

    try:
        # Upgrade the authorization code into a credentials object
        oauth_flow = flow_from_clientsecrets(
            'config/client_secrets.json', scope='')
        oauth_flow.redirect_uri = 'postmessage'
        credentials = oauth_flow.step2_exchange(code)
    except FlowExchangeError:
        return json_response('Failed to upgrade the authorization code.',
                                  401)

    gplus_id = credentials.id_token['sub']

    # Store the access token in the session for later use.
    session['credentials'] = credentials.access_token
    session['gplus_id'] = gplus_id

    if User.objects(gplus_id=gplus_id).count() == 0:
        # A new user model must be made

        # Get the user's name and email to populate the form
        http = httplib2.Http()
        http = credentials.authorize(http)
        people_document = gplus_service.people().get(
            userId='me').execute(http=http)

        # The user must be whitelisted in order to create an account.
        email = people_document['emails'][0]['value']
        if Whitelist.objects(email=email).count() != 1:
            return json_response({
                'code': WHITELIST_CODE,
                'title': 'User has not been whitelisted.',
                'email': email
                }, 401)

        return json_response(url_for(
            '.create_profile',
            next=request.args.get('next'),
            name=people_document['displayName'],
            email=email,
            image_url=people_document['image']['url']), 200)

    user = User.objects().get(gplus_id=gplus_id)
    user.register_login()
    user.save()

    # The user already exists.  Redirect to the next url or
    # the root of the application ('/')
    if request.args.get('next'):
        return json_response(request.args.get('next'), 200)
    return json_response(request.url_root, 200)
Exemple #17
0
 def setUp(self):
     for w in Whitelist.objects():
         w.delete()
     super(TestAuth, self).setUp()
 def __call__(self, form, field):
     if form.user_type.data != 'fake_user' and \
             Whitelist.objects(email=field.data).count() != 0:
         raise ValidationError(self.message)
Exemple #19
0
def store_token():
    """Do the oauth flow for Google plus sign in, storing the access token
    in the session, and redircting to create an account if appropriate.

    Because this method will be called from a ``$.ajax()`` request in
    JavaScript, we can't return ``redirect()``, so instead this method returns
    the URL that the user should be redirected to, and the redirect happens in
    html:

    .. code:: javascript

        success: function(response) {
            window.location.href = response.data.redirect_url;
        }

    **Route:** ``/admin/store-token``

    **Methods:** ``POST``
    """
    if request.args.get('state', '') != session.get('state'):
        return json_error_message('Invalid state parameter.', 401)

    del session['state']
    code = request.data

    try:
        # Upgrade the authorization code into a credentials object
        oauth_flow = flow_from_clientsecrets(config['CLIENT_SECRETS_PATH'],
                                             scope='')
        oauth_flow.redirect_uri = 'postmessage'
        credentials = oauth_flow.step2_exchange(code)
    except FlowExchangeError:
        return json_error_message('Failed to upgrade the authorization code.',
                                  401)

    gplus_id = credentials.id_token['sub']

    # Store the access token in the session for later use.
    session['credentials'] = credentials.access_token
    session['gplus_id'] = gplus_id

    if User.objects(gplus_id=gplus_id).count() == 0:
        # A new user model must be made

        # Get the user's name and email to populate the form
        http = httplib2.Http()
        http = credentials.authorize(http)
        people_document = gplus_service.people().get(userId='me').execute(
            http=http)

        # The user must be whitelisted in order to create an account.
        email = people_document['emails'][0]['value']
        if Whitelist.objects(email=email).count() != 1:
            return json_error_message('User has not been whitelisted.', 401, {
                'whitelisted': False,
                'email': email
            })

        return json_success({
            'redirect_url':
            url_for('.create_profile',
                    next=request.args.get('next'),
                    name=people_document['displayName'],
                    email=email,
                    image_url=people_document['image']['url'])
        })

    user = User.objects().get(gplus_id=gplus_id)
    user.register_login()
    user.save()

    # The user already exists.  Redirect to the next url or
    # the root of the application ('/')
    if request.args.get('next'):
        return json_success({'redirect_url': request.args.get('next')})
    return json_success({'redirect_url': request.url_root})
Exemple #20
0
 def __call__(self, form, field):
     if form.user_type.data != 'fake_user' and \
             Whitelist.objects(email=field.data).count() != 0:
         raise ValidationError(self.message)