Skip to content

Commit

Permalink
Merge pull request #17 from SEL-Columbia/0.1.5-dev
Browse files Browse the repository at this point in the history
0.1.5
  • Loading branch information
vr2262 authored Feb 16, 2017
2 parents 17df983 + fef9a85 commit 6527d67
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 371 deletions.
1 change: 1 addition & 0 deletions minigrid/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self, reason, status_code, template_name, **template_kwargs):
self.template_name = template_name
self.log_message = None
self.template_kwargs = template_kwargs
template_kwargs['next_page'] = '/'


class LoginError(MinigridHTTPError):
Expand Down
30 changes: 24 additions & 6 deletions minigrid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from urllib.parse import urlencode
from uuid import uuid4

from asyncio_portier import get_verified_email

import redis

from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.exc import DataError, IntegrityError
from sqlalchemy.orm.exc import NoResultFound, UnmappedInstanceError
Expand All @@ -12,7 +16,10 @@
import minigrid.error
import minigrid.models as models
from minigrid.options import options
from minigrid.portier import get_verified_email, redis_kv


cache = redis.StrictRedis.from_url(options.redis_url)
broker_url = 'https://broker.portier.io'


class BaseHandler(tornado.web.RequestHandler):
Expand Down Expand Up @@ -65,12 +72,15 @@ def get(self):
self.render(
'index-minigrid-list.html', system=system, minigrids=minigrids)
return
self.render('index-logged-out.html')
self.render(
'index-logged-out.html', next_page=self.get_argument('next', '/'))

def post(self):
"""Send login information to the portier broker."""
nonce = uuid4().hex
redis_kv.setex(nonce, timedelta(minutes=15), '')
next_page = self.get_argument('next', '/')
expiration = timedelta(minutes=15)
cache.set('portier:nonce:{}'.format(nonce), next_page, expiration)
query_args = urlencode({
'login_hint': self.get_argument('email'),
'scope': 'openid email',
Expand All @@ -79,7 +89,7 @@ def post(self):
'response_mode': 'form_post',
'client_id': options.minigrid_website_url,
'redirect_uri': options.minigrid_website_url + '/verify'})
self.redirect('https://broker.portier.io/auth?' + query_args)
self.redirect(broker_url + '/auth?' + query_args)


class TariffsHandler(BaseHandler):
Expand Down Expand Up @@ -360,7 +370,15 @@ async def post(self):
raise minigrid.error.LoginError(
reason=f'Broker Error: {error}: {description}')
token = self.get_argument('id_token')
email = await get_verified_email(token)
try:
email, next_page = await get_verified_email(
broker_url,
token,
options.minigrid_website_url,
broker_url,
cache)
except ValueError as exc:
raise minigrid.error.LoginError(reason=str(exc))
try:
user = (
self.session
Expand All @@ -373,7 +391,7 @@ async def post(self):
self.set_secure_cookie(
'user', str(user.user_id),
httponly=True, secure=options.minigrid_https)
self.redirect(self.get_argument('next', '/'))
self.redirect(next_page)


class LogoutHandler(BaseHandler):
Expand Down
150 changes: 0 additions & 150 deletions minigrid/portier.py

This file was deleted.

2 changes: 1 addition & 1 deletion minigrid/templates/index-logged-out.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% if message is not None %}
<p><strong>Login unsuccessful: {{ message }}</strong></p>
{% end %}
<form action="/" method="POST">
<form action="/?next={{ next_page }}" method="POST">
{% module xsrf_form_html() %}
E-mail: <input type="email" name="email" />
<input type="submit" value="Log In" />
Expand Down
2 changes: 1 addition & 1 deletion prod/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
minigrid:
image: selcolumbia/minigrid-server:0.1.4
image: selcolumbia/minigrid-server:0.1.5
command: ./prod/run.sh --db_host=db --redis_url=redis://redis:6379/0 --minigrid-website-url=https://www.example.com
depends_on:
- redis
Expand Down
6 changes: 3 additions & 3 deletions prod/install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
# Minigrid Server installer for version 0.1.4
# Minigrid Server installer for version 0.1.5
set -e

# Do you have docker installed?
Expand Down Expand Up @@ -108,8 +108,8 @@ $SUDO openssl dhparam -out /etc/letsencrypt/live/$LETSENCRYPT_DIR/dhparam.pem 20
printf "========================================\n"
printf " Generating configuration \n"
printf "========================================\n"
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.4/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.4/prod/nginx.conf > nginx.conf
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.5/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.5/prod/nginx.conf > nginx.conf

sed -i s/www.example.com/$LETSENCRYPT_DIR/g docker-compose.yml
sed -i s/www.example.com/$LETSENCRYPT_DIR/g nginx.conf
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
asyncio-portier==0.1.0.post1
cryptography==1.5.3
psycopg2==2.6.2
PyJWT==1.4.2
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for minigrid-server."""
1 change: 1 addition & 0 deletions tests/python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Python tests for minigrid-server."""
2 changes: 1 addition & 1 deletion tests/python/coverage_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
set -e
bash -c "psql -d minigrid -c 'drop schema if exists minigrid_test cascade;' -U postgres 1&>/dev/null"
coverage erase
coverage run --source=minigrid,server.py --branch -m unittest ${@:-discover tests.python}
coverage run --source=minigrid,server.py --branch -m unittest
coverage html
coverage report -m
21 changes: 17 additions & 4 deletions tests/python/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tests.python.util import HTTPTest, CoroMock

from minigrid import models
from minigrid.portier import redis_kv
from minigrid.handlers import cache
from server import Application


Expand Down Expand Up @@ -796,12 +796,25 @@ def test_login_success(self):
self.assertResponseCode(response, 302)
query = parse_qs(urlparse(response.headers['Location']).query)
self.assertEqual(query['login_hint'][0], 'a@a.com')
self.assertIn(query['nonce'][0].encode(), redis_kv)
self.assertIn(('portier:nonce:' + query['nonce'][0]).encode(), cache)
self.assertTrue(query['redirect_uri'][0].endswith('/verify'))

@patch('minigrid.handlers.get_verified_email', new_callable=CoroMock)
def test_verify_value_error(self, get_verified_email):
get_verified_email.coro.side_effect = ValueError('error')
self.create_user()
with ExpectLog('tornado.access', '400'):
response = self.fetch(
'/verify?id_token=',
method='POST', body='', follow_redirects=False
)
self.assertResponseCode(response, 400)
self.assertEqual(
response.error.message, 'error')

@patch('minigrid.handlers.get_verified_email', new_callable=CoroMock)
def test_verify(self, get_verified_email):
get_verified_email.coro.return_value = 'a@a.com'
get_verified_email.coro.return_value = 'a@a.com', ''
self.create_user()
response = self.fetch(
'/verify?id_token=', method='POST', body='', follow_redirects=False
Expand All @@ -815,7 +828,7 @@ def test_verify(self, get_verified_email):

@patch('minigrid.handlers.get_verified_email', new_callable=CoroMock)
def test_verify_user_does_not_exist(self, get_verified_email):
get_verified_email.coro.return_value = 'a@a.com'
get_verified_email.coro.return_value = 'a@a.com', ''
with ExpectLog('tornado.access', '400'):
response = self.fetch('/verify?id_token=', method='POST', body='')
self.assertResponseCode(response, 400)
Expand Down
Loading

0 comments on commit 6527d67

Please sign in to comment.