Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
envs.py
17 changes: 0 additions & 17 deletions index.html

This file was deleted.

11 changes: 11 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import flask
from flask_login import login_required, current_user
from auth import app

@app.route('/')
# @login_required
def homepage():
return flask.render_template('home.html')

if __name__ == '__main__':
app.run()
72 changes: 72 additions & 0 deletions src/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from init import app, auth, db
from models import User
from flask_login import login_user, logout_user, LoginManager
import flask

from functools import wraps


def csh_user_auth(func):
@wraps(func)
def wrapped_function(*args, **kwargs):
uid = str(flask.session["userinfo"].get("preferred_username", ""))
last = str(flask.session["userinfo"].get("family_name", ""))
first = str(flask.session["userinfo"].get("given_name", ""))
picture = "https://profiles.csh.rit.edu/image/" + uid
groups = flask.session["userinfo"].get("groups", [])
is_eboard = "eboard" in groups
is_rtp = "rtp" in groups
auth_dict = {
"uid": uid,
"first": first,
"last": last,
"picture": picture,
"admin": is_eboard or is_rtp or uid == "cinnamon"
}
kwargs["auth_dict"] = auth_dict
return func(*args, **kwargs)
return wrapped_function

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'csh_auth'


@login_manager.user_loader
def load_user(user_id):
q = User.query.get(user_id)
if q:
return q
return None


@app.route("/logout")
@auth.oidc_logout
def _logout():
logout_user()
return flask.redirect("/", 302)


@app.route('/csh_auth')
@app.route('/login')
@auth.oidc_auth('default')
@csh_user_auth
def csh_auth(auth_dict=None):
"""
Gets new logger inner data
"""
if auth_dict is None:
return flask.redirect("/csh_auth")
user = User.query.get(auth_dict['uid'])
if user is not None:
user.firstname = auth_dict['first']
user.lastname = auth_dict['last']
user.picture = auth_dict['picture']
user.admin = auth_dict['admin']
else:
user = User(auth_dict['uid'], auth_dict['first'],
auth_dict['last'], auth_dict['picture'], auth_dict['admin'])
db.session.add(user)
db.session.commit()
login_user(user)
return flask.redirect('/')
22 changes: 22 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from os import environ as env


# automatically updates some dev envs.
try:
__import__('envs.py')
except ImportError:
pass

# Flask config
IP = env.get('IP', '0.0.0.0')
PORT = env.get('PORT', 8080)
SERVER_NAME = env.get('SERVER_NAME', '127.0.0.1:5000')
PREFERRED_URL_SCHEME = env.get('PREFERRED_URL_SCHEME', 'https')

SQLALCHEMY_DATABASE_URI = env.get('SQLALCHEMY_DATABASE_URI', "sqlite:///users.sqlite3")
SQLALCHEMY_TRACK_MODIFICATIONS = 'False'

# OpenID Connect SSO config CSH
OIDC_ISSUER = env.get('OIDC_ISSUER', 'https://sso.csh.rit.edu/auth/realms/csh')
OIDC_CLIENT_ID = env.get('OIDC_CLIENT_ID', 'devcade')
OIDC_CLIENT_SECRET = env.get('OIDC_CLIENT_SECRET', 'NOT-A-SECRET')
32 changes: 32 additions & 0 deletions src/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import pytz
import flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from flask_pyoidc.provider_configuration import ProviderConfiguration, ClientMetadata


app = flask.Flask(__name__)
try:
app.config.from_pyfile(os.path.join(os.getcwd(), "config.py"))
except:
app.config.from_pyfile("config.py")

# time setup for the server side time
eastern = pytz.timezone('America/New_York')

# OIDC Authentication
CSH_AUTH = ProviderConfiguration(issuer=app.config["OIDC_ISSUER"],
client_metadata=ClientMetadata(
app.config["OIDC_CLIENT_ID"],
app.config["OIDC_CLIENT_SECRET"]))
auth = OIDCAuthentication({'default': CSH_AUTH},
app)

auth.init_app(app)
app.secret_key = os.urandom(16)

# DB
db = SQLAlchemy(app)
migrate = Migrate(app, db)
43 changes: 43 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from init import db


class User(db.Model):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reccomendation: use alembic or some other system to keep database revisions. They'll make it easier to replicate iterative changes to the db schema in the future

__tablename__ = 'user'

id = db.Column(db.String, primary_key=True)
firstname = db.Column(db.String, nullable=False)
lastname = db.Column(db.String, nullable=False)
picture = db.Column(db.String, nullable=False)
admin = db.Column(db.Boolean, nullable=False)

def init(self, uid, firstname, lastname, picture, admin):
self.id = uid
self.firstname = firstname
self.lastname = lastname
self.picture = picture
self.admin = admin

def __repr__(self):
return '<id {}>'.format(self.id)

def to_json(self):
return {"uid": self.uid,
"first": self.firstname,
"last": self.lastname,
"picture": self.picture}

def get_id(self):
return self.id

@staticmethod
def is_authenticated():
return True

@staticmethod
def is_active():
return True

@staticmethod
def is_anonymous():
return False

File renamed without changes.
Binary file added src/static/favicon.ico
Binary file not shown.
20 changes: 20 additions & 0 deletions src/templates/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% block header %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='css/devcade.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<title>Devcade</title>
</head>
<body>
<div id="nav"><h1>DEVCADE</h1></div>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>

{% endblock %}
4 changes: 4 additions & 0 deletions src/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "header.html" %}
{% block content %}
<p id="description">WElcum To doofcad</p>
{% endblock %}