Clean up local login

Use generate_and_store_token and get_local_user directly instead of the /make-token endpoint.
This commit is contained in:
Francesco Siddi 2017-07-14 21:41:40 +02:00
parent e752a5dc87
commit 502e494083
2 changed files with 25 additions and 27 deletions

View File

@ -37,17 +37,7 @@ def create_local_user(email, password):
return r['_id']
@blueprint.route('/make-token', methods=['POST'])
def make_token():
"""Direct login for a user, without OAuth, using local database. Generates
a token that is passed back to Pillar Web and used in subsequent
transactions.
:return: a token string
"""
username = request.form['username']
password = request.form['password']
def get_local_user(username, password):
# Look up user in db
users_collection = current_app.data.driver.db['users']
user = users_collection.find_one({'username': username})
@ -62,6 +52,21 @@ def make_token():
hashed_password = hash_password(password, salt)
if hashed_password != credentials['token']:
return abort(403)
return user
@blueprint.route('/make-token', methods=['POST'])
def make_token():
"""Direct login for a user, without OAuth, using local database. Generates
a token that is passed back to Pillar Web and used in subsequent
transactions.
:return: a token string
"""
username = request.form['username']
password = request.form['password']
user = get_local_user(username, password)
token = generate_and_store_token(user['_id'])
return jsonify(token=token['token'])

View File

@ -2,8 +2,8 @@ import json
import logging
import requests
from flask import (abort, Blueprint, current_app, flash, redirect,
render_template, request, session, url_for)
from flask import abort, Blueprint, current_app, flash, redirect, render_template, request, session,\
url_for
from flask_login import login_required, logout_user, current_user
from flask_oauthlib.client import OAuthException
from werkzeug import exceptions as wz_exceptions
@ -11,6 +11,7 @@ from werkzeug import exceptions as wz_exceptions
import pillar.api.blender_cloud.subscription
import pillar.auth
from pillar.web import system_util
from pillar.api.local_auth import generate_and_store_token, get_local_user
from . import forms
@ -78,23 +79,15 @@ def blender_id_authorized():
@blueprint.route('/login/local', methods=['GET', 'POST'])
def login_local():
"""Login with a local account, skipping OAuth. This provides access only
to the web application and is meant for limited access (for example in
the case of a shared account)."""
"""Login with a local account, as an alternative to OAuth.
This provides access only to the web application."""
form = forms.UserLoginForm()
# Forward credentials to server
if form.validate_on_submit():
payload = {
'username': form.username.data,
'password': form.password.data
}
r = requests.post("{0}auth/make-token".format(
system_util.pillar_server_endpoint()), data=payload)
if r.status_code != 200:
return abort(r.status_code)
res = r.json()
# If correct, receive token and log in the user
pillar.auth.login_user(res['token'])
user = get_local_user(form.username.data, form.password.data)
token = generate_and_store_token(user['_id'])
pillar.auth.login_user(token['token'])
return redirect(url_for('main.homepage'))
return render_template('users/login.html', form=form)