Skip to content

Commit 987334e

Browse files
committed
Updated app.py with basic authorization
1 parent 4f4cc26 commit 987334e

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

app.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
from flask import Flask, render_template
2+
from flask_httpauth import HTTPBasicAuth
3+
from werkzeug.security import generate_password_hash, check_password_hash
24
import connexion
35
import requests
46
import myapi
57

68
#Create the application instance!
79
app = connexion.App(__name__, specification_dir='./')
10+
auth = HTTPBasicAuth()
11+
12+
13+
users = {
14+
"john": generate_password_hash("hello"),
15+
"susan": generate_password_hash("bye")
16+
}
17+
18+
@auth.verify_password
19+
def verify_password(username, password):
20+
if username in users and \
21+
check_password_hash(users.get(username), password):
22+
return username
23+
824

925
#Read the swagger.yml file to configure the endpoints
1026
app.add_api('swagger.yml')
1127

1228
#Create a URL route in our application for "/"
1329
@app.route('/', methods=["GET","POST"])
30+
@auth.login_required
1431
def home():
15-
return render_template('home.html')
32+
#return "Hello, {}!".format(auth.current_user())
33+
if(auth.current_user()):
34+
#uname = auth.current_user()
35+
return render_template('home.html', username=auth.username())
36+
else:
37+
print("ERROR")
1638

1739
if __name__ == '__main__':
18-
app.run(host='0.0.0.0', port=8080, debug=True)
40+
app.run(host='0.0.0.0', port=8080, debug=True)
41+
42+
43+

templates/home.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<div class="container">
99
<nav>
1010
<a class="logo" href="{{ url_for('home') }}">Welcome to your REST API!</a>
11+
<p>The user is {{ username }} </p>
12+
1113
</nav>
1214
</div>
13-
</html>
15+
</html>

0 commit comments

Comments
 (0)