Skip to content

Commit 476c8aa

Browse files
committed
Hero och Napster har kokat galet
1 parent fdcf91b commit 476c8aa

File tree

13 files changed

+521
-8
lines changed

13 files changed

+521
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# SummIT
22
(in development) The new, very cool, meeting helper. This service is supposed to incorporate aspects of current tools and completely replace secretary.
33

4-
54
Något roligt :D

main.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

project/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import Flask
2+
from dotenv import load_dotenv
3+
#from flask_sqlalchemy import SQLAlchemy
4+
5+
# init SQLAlchemy so we can use it later in our models
6+
#db = SQLAlchemy()
7+
8+
def create_app():
9+
load_dotenv()
10+
app = Flask(__name__)
11+
12+
app.config['SECRET_KEY'] = 'secret-key-goes-here'
13+
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
14+
15+
# db.init_app(app)
16+
17+
# blueprint for auth routes in our app
18+
from .auth import auth as auth_blueprint
19+
app.register_blueprint(auth_blueprint)
20+
21+
# blueprint for non-auth parts of app
22+
from .main import main as main_blueprint
23+
app.register_blueprint(main_blueprint)
24+
25+
return app

project/auth.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from flask import Blueprint, render_template, redirect, url_for, session, request, jsonify
2+
from urllib.parse import urlencode
3+
import secrets
4+
import requests
5+
import os
6+
#from . import db
7+
8+
app_secret=''
9+
10+
client_id=os.getenv('GAMMA_CLIENT_ID', '')
11+
client_secret=os.getenv('GAMMA_CLIENT_SECRET', '')
12+
redirect_uri=os.getenv('GAMMA_REDIRECT_URI', 'http://localhost:5000/api/auth/callbacks/gamma')
13+
auth_uri=os.getenv('GAMMA_AUTH_URL', 'https://auth.chalmers.it/oauth2/authorize')
14+
token_uri=os.getenv('GAMMA_TOKEN_URL', 'https://auth.chalmers.it/oauth2/token')
15+
user_info_uri=os.getenv('GAMMA_USER_INFO_URL', 'https://auth.chalmers.it/oauth2/userinfo')
16+
17+
auth = Blueprint('auth', __name__)
18+
19+
@auth.route('/login')
20+
def login():
21+
return render_template('login.html')
22+
23+
@auth.route('/authorize')
24+
def authorize():
25+
# Generate and store state parameter for CSRF protection
26+
state = secrets.token_urlsafe(32)
27+
session['oauth2_state'] = state
28+
29+
qs = {
30+
'response_type': 'code',
31+
'client_id': client_id,
32+
'scope': 'openid', #profile
33+
'redirect_uri': redirect_uri,
34+
'state':state,
35+
}
36+
37+
return redirect(f"{auth_uri}?{urlencode(qs)}")
38+
39+
@auth.route('/api/auth/callbacks/gamma')
40+
def callback():
41+
args_dict = dict(request.args)
42+
print(args_dict)
43+
44+
if 'code' not in args_dict:
45+
return "Error: Missing authorization code parameter", 400
46+
47+
if 'state' not in args_dict:
48+
return "Error: Missing state parameter", 400
49+
50+
received_state = args_dict['state']
51+
stored_state = session.get('oauth2_state')
52+
53+
if not stored_state or received_state != stored_state:
54+
return "Error: Invalid state parameter", 400
55+
56+
session.pop('oauth2_state', None)
57+
58+
code = args_dict['code']
59+
return code
60+
61+
62+
63+
# @auth.route('/signup')
64+
# def signup():
65+
# return 'Signup'
66+
67+
@auth.route('/logout')
68+
def logout():
69+
return render_template('logout.html')

project/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from flask import Blueprint, render_template
2+
#from . import db
3+
4+
main = Blueprint('main', __name__)
5+
6+
@main.route('/')
7+
def index():
8+
return render_template('index.html')
9+
10+
@main.route('/profile')
11+
def profile():
12+
return render_template('profile.html')

project/templates/base.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<title>Flask Auth Example</title>
9+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" />
10+
</head>
11+
12+
<body>
13+
<section class="hero is-primary is-fullheight">
14+
15+
<div class="hero-head">
16+
<nav class="navbar">
17+
<div class="container">
18+
<div id="navbarMenuHeroA" class="navbar-menu">
19+
<div class="navbar-end">
20+
<a href="{{ url_for('main.index') }}" class="navbar-item">
21+
Home
22+
</a>
23+
<a href="{{ url_for('main.profile') }}" class="navbar-item">
24+
Profile
25+
</a>
26+
<a href="{{ url_for('auth.login') }}" class="navbar-item">
27+
Login
28+
</a>
29+
<a href="{{ url_for('auth.logout') }}" class="navbar-item">
30+
Logout
31+
</a>
32+
</div>
33+
</div>
34+
</div>
35+
</nav>
36+
</div>
37+
38+
<div class="hero-body">
39+
<div class="container has-text-centered">
40+
{% block content %}
41+
{% endblock %}
42+
</div>
43+
</div>
44+
</section>
45+
</body>
46+
47+
</html>

project/templates/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<h1 class="title">
5+
Flask Login Example
6+
</h1>
7+
<h2 class="subtitle">
8+
Easy authentication and authorization in Flask.
9+
</h2>
10+
{% endblock %}

project/templates/login.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<div class="column is-4 is-offset-4">
5+
<h3 class="title">Login</h3>
6+
<div class="box">
7+
<a href="{{ url_for('auth.authorize') }}" class="button is-block is-info is-large is-fullwidth">Login</a>
8+
</div>
9+
</div>
10+
{% endblock %}

project/templates/logout.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
logout
5+
{% endblock %}

project/templates/profile.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<h1 class="title">
5+
Welcome, Anthony!
6+
</h1>
7+
{% endblock %}

0 commit comments

Comments
 (0)