Skip to content

Session handling and environment variables #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from flask import Flask
from flask_oidc import OpenIDConnect
from flask_swagger_ui import get_swaggerui_blueprint
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy

from config import config
from backend.utils import configure_logger
from backend.storage import SessionCredentialStore

app = None
oidc = OpenIDConnect()
Expand All @@ -16,6 +19,16 @@ def create_app():

# Load the configurations based on the 'FLASK_ENV' environment variable
app.config.from_object(config)

# setup session database
db = SQLAlchemy(app)
app.config["SESSION_SQLALCHEMY_TABLE"] = 'sessions'
app.config["SESSION_SQLALCHEMY"] = db
session = Session(app)
session.app.session_interface.db.create_all()
# Init the OpenIDConnect application instance
# oidc.init_app(app)
oidc = OpenIDConnect(app, SessionCredentialStore())

# Initialize logger
_logger = configure_logger(app)
Expand Down
23 changes: 23 additions & 0 deletions backend/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import session
from collections import UserDict


class SessionCredentialStore(UserDict):
def __init__(self):
super().__init__()
self.session = session

def __setitem__(self, key, value):
self.session[key] = value

def __getitem__(self, key):
return self.session[key]

def __delitem__(self, key):
return self.session.pop(key)

def __contains__(self, key):
return key in self.session

def __repr__(self):
return 'SessionStore: {}'.format(str(self.__class__))
11 changes: 11 additions & 0 deletions config/configurations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

PRODUCTION = 'production'
DEVELOPMENT = 'development'

Expand All @@ -15,6 +17,9 @@ class Config(object):
SWAGGER_SPEC = 'spec'
SWAGGER_NAME = 'Tech API - template'
HANDLER = "RotatingFileHandler"
SESSION_TYPE = 'sqlalchemy'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = 'sqlite://'


class ProductionConfig(Config):
Expand All @@ -23,6 +28,11 @@ class ProductionConfig(Config):
OIDC_ID_TOKEN_COOKIE_SECURE = False
HANDLER = "StreamHandler"

if "DATABASE_URL" in os.environ:
SQLALCHEMY_DATABASE_URI = os.environ["DATABASE_URL"]
if "FLASK_SECRET_KEY" in os.environ:
SECRET_KEY = os.environ["FLASK_SECRET_KEY"]


class DevelopmentConfig(Config):
DEBUG = True
Expand All @@ -31,3 +41,4 @@ class DevelopmentConfig(Config):
OIDC_CLIENT_SECRETS = 'config/client_secrets_dev.json'
OIDC_OPENID_REALM = 'flask-demo'
OIDC_ID_TOKEN_COOKIE_SECURE = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///sessions.db'
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ Flask==1.0.2
flask-oidc==1.4.0
Jinja2==2.10
flask-swagger-ui==3.18.0
flask-sqlalchemy==2.3.2
flask-session==0.3.1