diff --git a/buckler.py b/buckler.py
index 19abc1b..05d142a 100644
--- a/buckler.py
+++ b/buckler.py
@@ -28,6 +28,23 @@ routes = web.RouteTableDef()
@auth.auth_required
async def index(request):
"""The index page."""
+ if request.method == 'POST':
+ data = await request.post()
+ form = data.get('form_name')
+
+ forms_ = {
+ 'invite_user': forms.invite_user,
+ 'change_password': forms.change_password,
+ 'delete_key': forms.delete_key,
+ 'delele_session': forms.delete_session,
+ 'new_app': forms.new_app,
+ }
+
+ if not forms_.get(form):
+ errors = {'main': "Unknown form id: {form}"}
+ else:
+ errors = await forms_[form](request)
+
async with request.app['pool'].acquire() as conn:
avail_sites = await conn.fetch(
"SELECT app_info.name, app_info.url "
@@ -58,22 +75,6 @@ async def index(request):
users[user_perm['username']][apps[index]['name']] = True
users_json = json.dumps(users)
- if request.method == 'POST':
- data = await request.post()
- form = data.get('form_name')
-
- forms_ = {
- 'invite_user': forms.invite_user,
- 'change_password': forms.change_password,
- 'delete_key': forms.delete_key,
- 'delele_session': forms.delete_session,
- }
-
- if not forms_[form]:
- errors = defaultdict['main'].append("Unknown form id: {form}")
- else:
- errors = await forms_[form](request)
-
return render_template("index.html", request, locals())
diff --git a/forms.py b/forms.py
index 728b192..033ea63 100644
--- a/forms.py
+++ b/forms.py
@@ -2,6 +2,8 @@
"""
Co-routines for handling various forms in Buckler.
"""
+from passlib.hash import argon2
+
import tools
@@ -86,3 +88,18 @@ async def delete_session(request):
"WHERE id = $1 AND user_id = $2",
session_id, request['session']['id'])
return {}
+
+
+async def new_app(request):
+ data = await request.post()
+ app_name = data.get('app_name')
+ app_url = data.get('app_url')
+ app_key = data.get('app_key')
+ key_hash = argon2.hash(app_key)
+
+ async with request.app['pool'].acquire() as conn:
+ await conn.execute(
+ "INSERT INTO app_info (name, url, key_hash) "
+ "VALUES ($1, $2, $3)",
+ app_name, app_url, key_hash)
+ return {}
diff --git a/templates/index.html b/templates/index.html
index b70e18e..c8a515b 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -74,6 +74,31 @@
+
+
{% endif %}