/
auth.py
88 lines (75 loc) · 2.87 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (C) 2014-2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Author: Michael Simacek <msimacek@redhat.com>
# Author: Mikolaj Izdebski <mizdebsk@redhat.com>
from __future__ import print_function, absolute_import
import functools
import re
from flask import abort, request, session, redirect, url_for, g
import koschei.models as m
from koschei.config import get_config
from koschei.frontend.base import app, db
from koschei.frontend.util import flash_info, flash_ack
bypass_login = get_config('bypass_login', None)
user_re = get_config('frontend.auth.user_re')
user_env = get_config('frontend.auth.user_env')
user_re = re.compile('^{}$'.format(user_re))
@app.route('/login', methods=['GET', 'POST'])
def login():
if bypass_login:
identity = "none"
user_name = bypass_login
else:
identity = request.environ.get(user_env) or abort(501)
user_name = re.match(user_re, identity).group(1)
user = db.query(m.User).filter_by(name=user_name).first()
if not user:
user = m.User(name=user_name, admin=bool(bypass_login))
db.add(user)
db.commit()
flash_info('New user "{}" was registered.'.format(user_name))
session['user'] = user_name
flash_ack('Logged in as user "{}" with identity "{}".'
.format(user_name, identity))
if user.admin:
flash_info('You have admin privileges.')
next_url = request.values.get("next", url_for('frontpage'))
return redirect(next_url)
@app.before_request
def lookup_current_user():
if request.endpoint == 'static':
return
g.user = None
user_name = session.get('user', None)
if user_name:
g.user = db.query(m.User).filter_by(name=user_name).first()
@app.route('/logout')
def logout():
if session.pop('user', None):
flash_ack('Successfully logged out.')
else:
flash_info('You were not logged in.')
return redirect(url_for('frontpage'))
def login_required():
def decorator(func):
@functools.wraps(func)
def decorated(*args, **kwargs):
if g.user is None:
return redirect(url_for('login', next=request.url))
return func(*args, **kwargs)
return decorated
return decorator