-
Notifications
You must be signed in to change notification settings - Fork 7
Convert to Flask application #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| __pycache__ | ||
| envs.py |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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('/') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from init import db | ||
|
|
||
|
|
||
| class User(db.Model): | ||
| __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 not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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