Skip to content

Commit

Permalink
Add: SSO implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasata committed Dec 23, 2019
1 parent 10f5b98 commit 1e97a56
Show file tree
Hide file tree
Showing 21 changed files with 699 additions and 496 deletions.
3 changes: 2 additions & 1 deletion APPVERSION
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# APPVERSION - Jani Tammi <jasata@utu.fi>
#
# 0.1.0 2019.12.07 Initial version.
# 0.2.0 2019.12.23 December release.
#
# This file may contain ONLY one single non-comment line.
# That row is expected to contain a semantic version string "x.y.z".
Expand All @@ -13,4 +14,4 @@
#
#

0.1.0
0.2.0
16 changes: 13 additions & 3 deletions api/File.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# 0.1.0 2019.12.07 Initial version.
#
#
# TODO: remove _* -columns from result sets.
#
import time
import logging
Expand Down Expand Up @@ -40,11 +41,20 @@ def __init__(self, request):
super().__init__(self.cursor, 'file')


def search(self, type: str = None):
"""If we would support any arguments, __init__() would have processed and stored them for us...."""
def search(self, type: str = None, include_restricted: bool = False):
"""Argument 'type' as per column file.type, 'restricted' as column file.restricted."""
app.logger.debug(
f"Query will {('not ', '')[int(include_restricted)]}include restricted {type} images"
)
self.sql = f"SELECT * FROM {self.table_name}"
where = []
if type is not None:
self.sql += f" WHERE type = '{type}'"
where.append(f" type = '{type}' ")
if not include_restricted:
where.append(f" _restricted = 'no' ")
if where:
self.sql += " WHERE " + " and ".join(where)
app.logger.debug("SQL: " + self.sql)
try:
self.cursor.execute(self.sql)
except sqlite3.Error as e:
Expand Down
52 changes: 35 additions & 17 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#
# application.py - Jani Tammi <jasata@utu.fi>
#
# 0.1.0 2019.12.07 Initial version.
# 0.1.0 2019-12-07 Initial version.
# 0.2.0 2019-12-23 Add SSO object creation and .update()
# @app.before_request
#
#
# Code in this file gets executed ONLY ONCE, when the uWSGI is started.
Expand Down Expand Up @@ -40,11 +42,17 @@
import time
import logging
import sqlite3
import datetime

from logging.handlers import RotatingFileHandler
from logging import Formatter
from flask import Flask
from flask import g
from flask import session
from flask import request

# Local module(s)
from sso import SSO


# For some reason, if Flask() is given 'debug=True',
Expand Down Expand Up @@ -138,6 +146,16 @@



#
# Create Single Sign-On object
#
sso = SSO(
app.config.get('SSO_COOKIE'),
app.config.get('SSO_SESSION_API')
)



#
# This logging happens only once, when uWSGI daemon starts
#
Expand All @@ -152,22 +170,6 @@
)


#
# Check certain important configuration values
#
if not app.config.get('COMMAND_TIMEOUT', None):
app.logger.warning(
"COMMAND_TIMEOUT not defined in application.conf! Defaulting to 1 second..."
)
app.config['COMMAND_TIMEOUT'] = 1.0

if not app.config.get('COMMAND_POLL_INTERVAL', None):
app.logger.warning(
"COMMAND_POLL_INTERVAL not defined in application.conf! Defaulting to 0.2 seconds..."
)
app.config['COMMAND_POLL_INTERVAL'] = 1.0




###############################################################################
Expand All @@ -193,6 +195,7 @@ def before_request():
g.t_cpu_start = time.process_time()
app.logger.debug("@app.before_request")


#
# Ensure database connection
#
Expand All @@ -203,13 +206,28 @@ def before_request():
cursor = g.db.cursor()
cursor.execute("PRAGMA foreign_keys = 1")


#
# Refresh session expiration
#
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(
minutes = app.config.get('SESSION_LIFETIME', 60)
)
#
# Update SSO object with request and session from this request
#
sso.update(request, session)

return


#
# Routes in 'routes.py'
#
import routes


#
# Executed each time application context tears down
# (request ends)
Expand Down
4 changes: 3 additions & 1 deletion create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ CREATE TABLE file
dtap TEXT NOT NULL DEFAULT 'production',
created INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),
_owner TEXT NOT NULL,
_restricted TEXT NOT NULL DEFAULT 'no',
FOREIGN KEY (_owner) REFERENCES teacher (uid),
UNIQUE(label, version, type),
CHECK (type IN ('usb', 'vm')),
CHECK (dtap IN ('development', 'testing', 'acceptance', 'production'))
CHECK (dtap IN ('development', 'testing', 'acceptance', 'production')),
CHECK (_restricted IN ('no', 'yes'))
);
CREATE TABLE teacher
(
Expand Down
30 changes: 16 additions & 14 deletions html/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@

<title>Turku university - Course Virtual Machines</title>

<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"
rel="stylesheet">
<link rel="stylesheet"
href="css/bootstrap.min.css">
<link rel="stylesheet"
href="css/common.css">
<link rel="stylesheet"
href="css/subpage.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/subpage.css">
<link rel="stylesheet" href="css/sso.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/sso.js"></script>
<script>
// Initialize SSO element
$(document).ready(function() {
$("#sso").sso();
});
$('.navbar-nav a').on('click', function() {
$('.navbar-collapse').collapse('hide');
});
</script>

</head>

Expand All @@ -47,14 +54,9 @@
<a class="nav-link p-3" href="#sec3">Exporting</a>
</ul>
</div>
<div id="sso"></div>
</div>
</nav>
<script>
$('.navbar-nav a').on('click', function()
{
$('.navbar-collapse').collapse('hide');
});
</script>



Expand Down
85 changes: 85 additions & 0 deletions html/css/sso.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
:root {
--sso-font-size: 14px;
}
#sso
{
padding: 6px;
width: 80px;
height: auto;
background-color: White; /* Pink; for visual */
text-align: center;
}
#sso > div
{
padding: 4px;
border-radius: 7%;
border: 1px solid Silver;
background-color: White;
-webkit-transition: 0.3s ease-out;
-moz-transition: 0.3s ease-out;
-o-transition: 0.3s ease-out;
transition: 0.3s ease-out;
}
#sso > div:hover
{
box-shadow: 0 5px 17px rgba(0,0,0,0.25),
0 3px 4px rgba(0,0,0,0.22);
background-color: #f4f4f4;
}
#sso a
{
font-size: var(--sso-font-size);
}
#sso a:link
{
color: var(--link-color);
text-decoration: none;
}
#sso a:visited
{
color: var(--link-color);
text-decoration: none;
}
#sso a:hover
{
color: var(--text-color);
text-decoration: none;
}
#sso a:active
{
color: var(--text-color);
text-decoration: none;
}
#sso a::before
{
display: block;
font-family: FontAwesome;
font-size: calc(var(--sso-font-size) + 6px);
}
#sso a::after
{
display: block;
font-family: FontAwesome;
font-size: calc(var(--sso-font-size) + 6px);
}
#sso.anonymous a::before
{
content: '\f090'; /* sign-in */
}
#sso.student a::before
{
content: '\f19d'; /* graduation cap */
}
#sso.teacher a::before
{
content: '\f19c'; /* university */
}
#sso.deactivated a
{
color: Silver !important;
}
#sso.deactivated > div:hover
{
box-shadow: none !important;
background-color: White !important;
}
19 changes: 13 additions & 6 deletions html/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/subpage.css">
<link rel="stylesheet" href="css/sso.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/sso.js"></script>
<script>
// Initialize SSO element
$(document).ready(function() {
$("#sso").sso();
});
$('.navbar-nav a').on('click', function() {
$('.navbar-collapse').collapse('hide');
});
</script>


</head>

Expand All @@ -46,14 +58,9 @@
-->
</ul>
</div>
<div id="sso"></div>
</div>
</nav>
<script>
$('.navbar-nav a').on('click', function()
{
$('.navbar-collapse').collapse('hide');
});
</script>



Expand Down
13 changes: 13 additions & 0 deletions html/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
charset="utf8"
src="DataTables/datatables.js">
</script>
<link rel="stylesheet"
type="text/css"
href="css/sso.css">
<script type="text/javascript"
charset="utf8"
src="js/sso.js">
</script>

<!-- Site Specific -->
<link rel="stylesheet"
type="text/css"
Expand Down Expand Up @@ -84,6 +92,10 @@
$(document).ready(function() {

/******************************************************************
/* Single Sign-On Element
*/
$("#sso").sso();
/******************************************************************
/* CardTab building
*/
$('.tabgroup-vm').cardTabs({theme: 'utu'});
Expand Down Expand Up @@ -226,6 +238,7 @@
<a class="nav-link p-3" Href="#usb">USB Disk Images</a>
</ul>
</div>
<div id="sso"></div>
</div>
</nav>

Expand Down
Loading

0 comments on commit 1e97a56

Please sign in to comment.