Open Kilda Java Documentation
login.py
Go to the documentation of this file.
1 # Copyright 2017 Telstra Open Source
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 
16 from flask import Flask, flash, redirect, render_template, request, session, abort, url_for, Response
17 from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
18 
19 from app import application
20 from app import models, utils, db
21 
22 import sys, os
23 
24 login_manager = LoginManager()
25 login_manager.init_app(application)
26 login_manager.login_view = "login"
27 
28 @application.route('/')
29 @login_required
30 def index():
31  user = models.Users.query.filter(models.Users.username == 'admin').first()
32  return render_template('index.html', username=user.username)
33 
34 
35 @application.route("/login", methods=["GET", "POST"])
36 def login():
37  if request.method == 'POST':
38  username = request.form['username']
39  password = request.form['password']
40  otp = request.form['twofactor']
41  hashed_password = utils.hash_password(password)
42  db_user = models.Users.query.filter(models.Users.username == username).first()
43  try:
44  otp_result = utils.check_otp(otp, db_user.twofactor)
45  otp_result = True
46  except:
47  return render_template('login.html')
48 
49  if db_user and otp_result and hashed_password == str(db_user.password):
50  login_user(db_user)
51  return redirect(url_for('index'))
52  else:
53  return render_template('login.html')
54  else:
55  return render_template('login.html')
56 
57 
58 
59 @application.route("/logout")
60 @login_required
61 def logout():
62  logout_user()
63  return redirect(url_for('login'))
64 
65 @login_manager.user_loader
66 def user_loader(username):
67  try:
68  user = models.Users.query.filter(models.Users.username == username).first()
69  if user:
70  return user
71  return None
72 
73  except Exception as e:
74  return e
def user_loader(username)
Definition: login.py:66
def login()
Definition: login.py:36
def logout()
Definition: login.py:61
def index()
Definition: login.py:30