73 lines
1.6 KiB
Python
73 lines
1.6 KiB
Python
import logging
|
|
|
|
from flask import (Flask, make_response, request, abort, send_file,
|
|
redirect, url_for)
|
|
from flask.ext.principal import Principal
|
|
from flask.ext.login import login_user, LoginManager, UserMixin
|
|
|
|
import model
|
|
|
|
app = Flask(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
Principal(app, use_sessions=False)
|
|
|
|
app.secret_key = '1cb18882-6d12-440d-a4cc-b7430fb5f884'
|
|
|
|
login_manager = LoginManager()
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'signin'
|
|
|
|
|
|
class _LoginWrappedDBUser(UserMixin):
|
|
def __init__(self, db_user):
|
|
self.db_user = db_user
|
|
|
|
def is_active(self):
|
|
return self.db_user.verified
|
|
|
|
def get_id(self):
|
|
return unicode(self.db_user.username)
|
|
|
|
|
|
@login_manager.user_loader
|
|
def load_user(username):
|
|
db_user = model.get_user(username)
|
|
if db_user:
|
|
return _LoginWrappedDBUser(db_user)
|
|
else:
|
|
return None
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def index():
|
|
return send_file('templates/index.html')
|
|
|
|
|
|
@app.route('/signin', methods=['POST'])
|
|
def signin():
|
|
username = request.form['username']
|
|
password = request.form['password']
|
|
|
|
#TODO Allow email login
|
|
verified = model.verify_user(username, password)
|
|
if verified:
|
|
logger.debug('Successfully signed in as: %s' % username)
|
|
|
|
login_user(_LoginWrappedDBUser(verified))
|
|
return redirect(request.args.get('next') or url_for('index'))
|
|
|
|
abort(403)
|
|
|
|
|
|
@app.route('/signin', methods=['GET'])
|
|
def render_signin_page():
|
|
return send_file('templates/signin.html')
|
|
|
|
|
|
@app.route('/_ping')
|
|
@app.route('/v1/_ping')
|
|
def ping():
|
|
response = make_response('true', 200)
|
|
response.headers['X-Docker-Registry-Version'] = '0.6.0'
|
|
return response
|