refactor(api): black
parent
cb187f6bef
commit
6c07d1dd1b
|
@ -1,65 +1,53 @@
|
||||||
#!flask/bin/python
|
#!flask/bin/python
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
import os
|
|
||||||
import logging as log
|
import logging as log
|
||||||
|
import os
|
||||||
|
|
||||||
from flask import Flask, send_from_directory, render_template
|
from flask import Flask, render_template, send_from_directory
|
||||||
app = Flask(__name__, static_url_path='')
|
|
||||||
app = Flask(__name__, template_folder='static/templates')
|
app = Flask(__name__, static_url_path="")
|
||||||
|
app = Flask(__name__, template_folder="static/templates")
|
||||||
app.url_map.strict_slashes = False
|
app.url_map.strict_slashes = False
|
||||||
|
|
||||||
'''
|
"""
|
||||||
App secret key for encrypting cookies
|
App secret key for encrypting cookies
|
||||||
You can generate one with:
|
You can generate one with:
|
||||||
import os
|
import os
|
||||||
os.urandom(24)
|
os.urandom(24)
|
||||||
And paste it here.
|
And paste it here.
|
||||||
'''
|
"""
|
||||||
app.secret_key = "Change this key!//\xf7\x83\xbe\x17\xfa\xa3zT\n\\]m\xa6\x8bF\xdd\r\xf7\x9e\x1d\x1f\x14'"
|
app.secret_key = "Change this key!//\xf7\x83\xbe\x17\xfa\xa3zT\n\\]m\xa6\x8bF\xdd\r\xf7\x9e\x1d\x1f\x14'"
|
||||||
|
|
||||||
print('Starting isard-sso api...')
|
print("Starting isard-sso api...")
|
||||||
|
|
||||||
from api.lib.load_config import loadConfig
|
from api.lib.load_config import loadConfig
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loadConfig(app)
|
loadConfig(app)
|
||||||
except:
|
except:
|
||||||
print('Could not get environment variables...')
|
print("Could not get environment variables...")
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
'''
|
|
||||||
Debug should be removed on production!
|
Debug should be removed on production!
|
||||||
'''
|
"""
|
||||||
if app.debug:
|
if app.debug:
|
||||||
log.warning('Debug mode: {}'.format(app.debug))
|
log.warning("Debug mode: {}".format(app.debug))
|
||||||
else:
|
else:
|
||||||
log.info('Debug mode: {}'.format(app.debug))
|
log.info("Debug mode: {}".format(app.debug))
|
||||||
|
|
||||||
'''
|
"""
|
||||||
Serve static files
|
Serve static files
|
||||||
'''
|
"""
|
||||||
@app.route('/templates/<path:path>')
|
|
||||||
|
|
||||||
|
@app.route("/templates/<path:path>")
|
||||||
def send_templates(path):
|
def send_templates(path):
|
||||||
return send_from_directory(os.path.join(app.root_path, 'static/templates'), path)
|
return send_from_directory(os.path.join(app.root_path, "static/templates"), path)
|
||||||
|
|
||||||
# @app.route('/static/<path:path>')
|
|
||||||
# def send_static_js(path):
|
|
||||||
# return send_from_directory(os.path.join(app.root_path, 'static'), path)
|
|
||||||
|
|
||||||
# @app.errorhandler(404)
|
"""
|
||||||
# def not_found_error(error):
|
|
||||||
# return render_template('page_404.html'), 404
|
|
||||||
|
|
||||||
# @app.errorhandler(500)
|
|
||||||
# def internal_error(error):
|
|
||||||
# return render_template('page_500.html'), 500
|
|
||||||
|
|
||||||
'''
|
|
||||||
Import all views
|
Import all views
|
||||||
'''
|
"""
|
||||||
from .views import MenuViews
|
from .views import AvatarsViews, MenuViews
|
||||||
from .views import AvatarsViews
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
import time
|
import json
|
||||||
from api import app as application
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
import pprint
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
|
||||||
import yaml, json
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import pprint
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import yaml
|
||||||
from keycloak import KeycloakAdmin
|
from keycloak import KeycloakAdmin
|
||||||
|
|
||||||
|
from api import app as application
|
||||||
|
|
||||||
|
|
||||||
class Avatars():
|
class Avatars:
|
||||||
def __init__(self,
|
def __init__(
|
||||||
|
self,
|
||||||
url="http://isard-sso-keycloak:8080/auth/",
|
url="http://isard-sso-keycloak:8080/auth/",
|
||||||
username=os.environ['KEYCLOAK_USER'],
|
username=os.environ["KEYCLOAK_USER"],
|
||||||
password=os.environ['KEYCLOAK_PASSWORD'],
|
password=os.environ["KEYCLOAK_PASSWORD"],
|
||||||
realm='master',
|
realm="master",
|
||||||
verify=True):
|
verify=True,
|
||||||
|
):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
|
@ -28,20 +30,19 @@ class Avatars():
|
||||||
self.verify = verify
|
self.verify = verify
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self.keycloak_admin = KeycloakAdmin(server_url=self.url,
|
self.keycloak_admin = KeycloakAdmin(
|
||||||
|
server_url=self.url,
|
||||||
username=self.username,
|
username=self.username,
|
||||||
password=self.password,
|
password=self.password,
|
||||||
realm_name=self.realm,
|
realm_name=self.realm,
|
||||||
verify=self.verify)
|
verify=self.verify,
|
||||||
|
)
|
||||||
|
|
||||||
def get_user_avatar(self, username):
|
def get_user_avatar(self, username):
|
||||||
self.connect()
|
self.connect()
|
||||||
return self.keycloak_admin.get_user_id(username)
|
return self.keycloak_admin.get_user_id(username)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# # Add user
|
# # Add user
|
||||||
# new_user = keycloak_admin.create_user({"email": "example@example.com",
|
# new_user = keycloak_admin.create_user({"email": "example@example.com",
|
||||||
# "username": "example@example.com",
|
# "username": "example@example.com",
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
from api import app
|
|
||||||
|
|
||||||
import os, sys
|
|
||||||
import logging as log
|
import logging as log
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
class loadConfig():
|
from api import app
|
||||||
|
|
||||||
|
|
||||||
|
class loadConfig:
|
||||||
def __init__(self, app=None):
|
def __init__(self, app=None):
|
||||||
try:
|
try:
|
||||||
app.config.setdefault('DOMAIN', os.environ['DOMAIN'])
|
app.config.setdefault("DOMAIN", os.environ["DOMAIN"])
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(traceback.format_exc())
|
log.error(traceback.format_exc())
|
||||||
|
|
|
@ -4,22 +4,23 @@
|
||||||
# Josep Maria Viñolas Auquer
|
# Josep Maria Viñolas Auquer
|
||||||
# Alberto Larraz Dalmases
|
# Alberto Larraz Dalmases
|
||||||
# License: AGPLv3
|
# License: AGPLv3
|
||||||
import time
|
import json
|
||||||
from api import app as application
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
import pprint
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import pprint
|
||||||
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import yaml, json
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import yaml
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
|
from api import app as application
|
||||||
|
|
||||||
|
|
||||||
def write_css():
|
def write_css():
|
||||||
env = Environment(loader=FileSystemLoader('api/static/_templates'))
|
env = Environment(loader=FileSystemLoader("api/static/_templates"))
|
||||||
css_template = env.get_template('dd.css')
|
css_template = env.get_template("dd.css")
|
||||||
with open('menu/custom.yaml', 'r') as menu_custom_file:
|
with open("menu/custom.yaml", "r") as menu_custom_file:
|
||||||
menu_custom_yaml = menu_custom_file.read()
|
menu_custom_yaml = menu_custom_file.read()
|
||||||
menu_custom = yaml.full_load(menu_custom_yaml)
|
menu_custom = yaml.full_load(menu_custom_yaml)
|
||||||
css = css_template.render(data=menu_custom)
|
css = css_template.render(data=menu_custom)
|
||||||
|
@ -27,7 +28,7 @@ def write_css():
|
||||||
css_file.write(css)
|
css_file.write(css)
|
||||||
|
|
||||||
|
|
||||||
class Menu():
|
class Menu:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# self.user_menudict=self.gen_user_menu()
|
# self.user_menudict=self.gen_user_menu()
|
||||||
# pprint.pprint(self.user_menudict)
|
# pprint.pprint(self.user_menudict)
|
||||||
|
@ -38,40 +39,63 @@ class Menu():
|
||||||
self.write_headers()
|
self.write_headers()
|
||||||
write_css()
|
write_css()
|
||||||
|
|
||||||
''' HEADER & APP MENU '''
|
""" HEADER & APP MENU """
|
||||||
|
|
||||||
def gen_header(self):
|
def gen_header(self):
|
||||||
with open(r'menu/system.yaml') as yml:
|
with open(r"menu/system.yaml") as yml:
|
||||||
system = yaml.load(yml, Loader=yaml.FullLoader)
|
system = yaml.load(yml, Loader=yaml.FullLoader)
|
||||||
|
|
||||||
user_menu = []
|
user_menu = []
|
||||||
for item in system['user_menu']:
|
for item in system["user_menu"]:
|
||||||
item['href']='https://'+item['subdomain']+'.'+application.config['DOMAIN']+item['href']
|
item["href"] = (
|
||||||
del item['subdomain']
|
"https://"
|
||||||
|
+ item["subdomain"]
|
||||||
|
+ "."
|
||||||
|
+ application.config["DOMAIN"]
|
||||||
|
+ item["href"]
|
||||||
|
)
|
||||||
|
del item["subdomain"]
|
||||||
user_menu.append(item)
|
user_menu.append(item)
|
||||||
user_menu_dict={'user_menu':user_menu,
|
user_menu_dict = {
|
||||||
'user_avatar':'https://sso.'+application.config['DOMAIN']+'/auth/realms/master/avatar-provider'}
|
"user_menu": user_menu,
|
||||||
|
"user_avatar": "https://sso."
|
||||||
|
+ application.config["DOMAIN"]
|
||||||
|
+ "/auth/realms/master/avatar-provider",
|
||||||
|
}
|
||||||
|
|
||||||
apps_internal = []
|
apps_internal = []
|
||||||
for app in system['apps_internal']:
|
for app in system["apps_internal"]:
|
||||||
app['href']='https://'+app['subdomain']+'.'+application.config['DOMAIN']+app['href']
|
app["href"] = (
|
||||||
del app['subdomain']
|
"https://"
|
||||||
|
+ app["subdomain"]
|
||||||
|
+ "."
|
||||||
|
+ application.config["DOMAIN"]
|
||||||
|
+ app["href"]
|
||||||
|
)
|
||||||
|
del app["subdomain"]
|
||||||
apps_internal.append(app)
|
apps_internal.append(app)
|
||||||
|
|
||||||
with open(r'menu/custom.yaml') as yml:
|
with open(r"menu/custom.yaml") as yml:
|
||||||
custom = yaml.load(yml, Loader=yaml.FullLoader)
|
custom = yaml.load(yml, Loader=yaml.FullLoader)
|
||||||
custom['background_login']='https://api.'+application.config['DOMAIN']+custom['background_login']
|
custom["background_login"] = (
|
||||||
custom['logo']='https://api.'+application.config['DOMAIN']+custom['logo']
|
"https://api." + application.config["DOMAIN"] + custom["background_login"]
|
||||||
|
)
|
||||||
|
custom["logo"] = "https://api." + application.config["DOMAIN"] + custom["logo"]
|
||||||
|
|
||||||
menudict={**custom,**{'apps_internal':apps_internal,**user_menu_dict}}
|
menudict = {**custom, **{"apps_internal": apps_internal, **user_menu_dict}}
|
||||||
menudict['user']={}
|
menudict["user"] = {}
|
||||||
menudict['user']['account']='https://sso.'+application.config['DOMAIN']+system['user']['account']
|
menudict["user"]["account"] = (
|
||||||
menudict['user']['avatar']='https://sso.'+application.config['DOMAIN']+system['user']['avatar']
|
"https://sso." + application.config["DOMAIN"] + system["user"]["account"]
|
||||||
|
)
|
||||||
|
menudict["user"]["avatar"] = (
|
||||||
|
"https://sso." + application.config["DOMAIN"] + system["user"]["avatar"]
|
||||||
|
)
|
||||||
return menudict
|
return menudict
|
||||||
|
|
||||||
def write_headers(self):
|
def write_headers(self):
|
||||||
env = Environment(loader=FileSystemLoader('api/static/_templates'))
|
env = Environment(loader=FileSystemLoader("api/static/_templates"))
|
||||||
|
|
||||||
template = env.get_template('user_menu.html')
|
template = env.get_template("user_menu.html")
|
||||||
output_from_parsed_template = template.render(data=self.menudict)
|
output_from_parsed_template = template.render(data=self.menudict)
|
||||||
print(output_from_parsed_template)
|
print(output_from_parsed_template)
|
||||||
with open("api/static/templates/user_menu_header.html", "w") as fh:
|
with open("api/static/templates/user_menu_header.html", "w") as fh:
|
||||||
|
@ -80,7 +104,7 @@ class Menu():
|
||||||
# with open("api/static/templates/user_menu_header.json", "w") as fh:
|
# with open("api/static/templates/user_menu_header.json", "w") as fh:
|
||||||
# fh.write(json.dumps(self.menudict))
|
# fh.write(json.dumps(self.menudict))
|
||||||
|
|
||||||
template = env.get_template('apps_menu.html')
|
template = env.get_template("apps_menu.html")
|
||||||
output_from_parsed_template = template.render(data=self.menudict)
|
output_from_parsed_template = template.render(data=self.menudict)
|
||||||
print(output_from_parsed_template)
|
print(output_from_parsed_template)
|
||||||
with open("api/static/templates/header.html", "w") as fh:
|
with open("api/static/templates/header.html", "w") as fh:
|
||||||
|
|
|
@ -1,19 +1,34 @@
|
||||||
#!flask/bin/python
|
#!flask/bin/python
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
from api import app
|
import json
|
||||||
import logging as log
|
import logging as log
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import time,json
|
|
||||||
import sys,os
|
from flask import (
|
||||||
from flask import render_template, Response, request, redirect, url_for, jsonify, send_from_directory
|
Response,
|
||||||
|
jsonify,
|
||||||
|
redirect,
|
||||||
|
render_template,
|
||||||
|
request,
|
||||||
|
send_from_directory,
|
||||||
|
url_for,
|
||||||
|
)
|
||||||
|
|
||||||
|
from api import app
|
||||||
|
|
||||||
from ..lib.avatars import Avatars
|
from ..lib.avatars import Avatars
|
||||||
|
|
||||||
avatars = Avatars()
|
avatars = Avatars()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/avatar/<username>", methods=["GET"])
|
@app.route("/avatar/<username>", methods=["GET"])
|
||||||
def avatar(username):
|
def avatar(username):
|
||||||
print(app.root_path)
|
return send_from_directory(
|
||||||
log.error(app.root_path)
|
os.path.join(app.root_path, "../avatars/master-avatars/"),
|
||||||
return send_from_directory(os.path.join(app.root_path, '../avatars/master-avatars/'), avatars.get_user_avatar(username), mimetype='image/jpg')
|
avatars.get_user_avatar(username),
|
||||||
|
mimetype="image/jpg",
|
||||||
|
)
|
||||||
|
|
|
@ -1,31 +1,41 @@
|
||||||
#!flask/bin/python
|
#!flask/bin/python
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
from api import app
|
import json
|
||||||
import logging as log
|
import logging as log
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import time,json
|
|
||||||
import sys,os
|
from flask import Response, jsonify, redirect, render_template, request, url_for
|
||||||
from flask import render_template, Response, request, redirect, url_for, jsonify
|
|
||||||
|
from api import app
|
||||||
|
|
||||||
from ..lib.menu import Menu
|
from ..lib.menu import Menu
|
||||||
|
|
||||||
menu = Menu()
|
menu = Menu()
|
||||||
|
|
||||||
@app.route('/header/<format>', methods=['GET'])
|
|
||||||
@app.route('/header/<format>/<application>', methods=['GET'])
|
@app.route("/header/<format>", methods=["GET"])
|
||||||
|
@app.route("/header/<format>/<application>", methods=["GET"])
|
||||||
def api_v2_header(format, application=False):
|
def api_v2_header(format, application=False):
|
||||||
if application == False:
|
if application == False:
|
||||||
if format == 'json':
|
if format == "json":
|
||||||
if application == False:
|
if application == False:
|
||||||
return json.dumps(menu.get_header()), 200, {'Content-Type': 'application/json'}
|
return (
|
||||||
if format == 'html':
|
json.dumps(menu.get_header()),
|
||||||
|
200,
|
||||||
|
{"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
if format == "html":
|
||||||
if application == False:
|
if application == False:
|
||||||
return render_template('header.html')
|
return render_template("header.html")
|
||||||
if application == 'nextcloud':
|
if application == "nextcloud":
|
||||||
return render_template('header_nextcloud.html')
|
return render_template("header_nextcloud.html")
|
||||||
if application == 'wordpress':
|
if application == "wordpress":
|
||||||
return render_template('header_wordpress.html')
|
return render_template("header_wordpress.html")
|
||||||
|
|
||||||
|
|
||||||
# @app.route('/user_menu/<format>', methods=['GET'])
|
# @app.route('/user_menu/<format>', methods=['GET'])
|
||||||
# @app.route('/user_menu/<format>/<application>', methods=['GET'])
|
# @app.route('/user_menu/<format>/<application>', methods=['GET'])
|
||||||
|
|
|
@ -18,6 +18,4 @@ if __name__ == "__main__":
|
||||||
engineio_logger = logging.getLogger("engineio")
|
engineio_logger = logging.getLogger("engineio")
|
||||||
engineio_logger.setLevel("ERROR")
|
engineio_logger.setLevel("ERROR")
|
||||||
|
|
||||||
socketio.run(
|
socketio.run(app, host="0.0.0.0", port=7039, debug=False)
|
||||||
app, host="0.0.0.0", port=7039, debug=False
|
|
||||||
)
|
|
||||||
|
|
|
@ -3,23 +3,29 @@ from pprint import pprint
|
||||||
|
|
||||||
from keycloak import KeycloakAdmin
|
from keycloak import KeycloakAdmin
|
||||||
|
|
||||||
|
|
||||||
def keycloak_connect():
|
def keycloak_connect():
|
||||||
keycloak_admin = KeycloakAdmin(server_url="http://isard-sso-keycloak:8080/auth/",
|
keycloak_admin = KeycloakAdmin(
|
||||||
username='admin',
|
server_url="http://isard-sso-keycloak:8080/auth/",
|
||||||
password='keycloakkeycloak',
|
username="admin",
|
||||||
|
password="keycloakkeycloak",
|
||||||
realm_name="master",
|
realm_name="master",
|
||||||
user_realm_name="only_if_other_realm_than_master",
|
user_realm_name="only_if_other_realm_than_master",
|
||||||
client_secret_key="client-secret",
|
client_secret_key="client-secret",
|
||||||
verify=True)
|
verify=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def keycloak_dumps():
|
def keycloak_dumps():
|
||||||
print('Dumping keycloak config...')
|
print("Dumping keycloak config...")
|
||||||
|
|
||||||
|
|
||||||
def Keycloak_imports():
|
def Keycloak_imports():
|
||||||
with open('saml_client.json') as json_file:
|
with open("saml_client.json") as json_file:
|
||||||
data = json.load(json_file)
|
data = json.load(json_file)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
keycloak_connect()
|
keycloak_connect()
|
||||||
if sys.argv[1]=='dump':
|
if sys.argv[1] == "dump":
|
||||||
keycloak_dumps()
|
keycloak_dumps()
|
||||||
|
|
Loading…
Reference in New Issue