oidc added

This commit is contained in:
Wolfgang Hottgenroth 2024-01-30 12:23:48 +01:00
parent 0bca4ba03b
commit 3c8d842e3b
Signed by: wn
GPG Key ID: 836E9E1192A6B132
3 changed files with 71 additions and 54 deletions

View File

@ -1,25 +1,25 @@
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: oidc-python-example name: nutri
labels: labels:
app: oidc-python-example app: nutri
spec: spec:
replicas: 1 replicas: 1
selector: selector:
matchLabels: matchLabels:
app: oidc-python-example app: nutri
template: template:
metadata: metadata:
labels: labels:
app: oidc-python-example app: nutri
spec: spec:
containers: containers:
- name: oidc-python-example - name: nutri
image: %IMAGE% image: %IMAGE%
envFrom: envFrom:
- secretRef: - secretRef:
name: secrets name: nutri-secrets
ports: ports:
- containerPort: 8080 - containerPort: 8080
protocol: TCP protocol: TCP
@ -27,11 +27,11 @@ spec:
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: oidc-python-example name: nutri
spec: spec:
type: ClusterIP type: ClusterIP
selector: selector:
app: oidc-python-example app: nutri
ports: ports:
- name: http - name: http
targetPort: 8080 targetPort: 8080
@ -40,23 +40,23 @@ spec:
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: Ingress kind: Ingress
metadata: metadata:
name: oidc-python-example name: nutri
annotations: annotations:
cert-manager.io/cluster-issuer: letsencrypt-production-http cert-manager.io/cluster-issuer: letsencrypt-production-http
spec: spec:
tls: tls:
- hosts: - hosts:
- oidc-python-example.hottis.de - nutri.hottis.de
secretName: oidc-python-example-cert secretName: nutri-cert
rules: rules:
- host: oidc-python-example.hottis.de - host: nutri.hottis.de
http: http:
paths: paths:
- path: / - path: /
pathType: Prefix pathType: Prefix
backend: backend:
service: service:
name: oidc-python-example name: nutri
port: port:
number: 80 number: 80

View File

@ -2,10 +2,10 @@
"web": { "web": {
"issuer": "https://auth2.hottis.de/realms/hottis", "issuer": "https://auth2.hottis.de/realms/hottis",
"auth_uri": "https://auth2.hottis.de/ealms/hottis/protocol/openid-connect/auth", "auth_uri": "https://auth2.hottis.de/ealms/hottis/protocol/openid-connect/auth",
"client_id": "oidc-python-example", "client_id": "nutri",
"client_secret": "%CLIENT_SECRET%", "client_secret": "%CLIENT_SECRET%",
"redirect_uris": [ "redirect_uris": [
"https://oidc-python-example.hottis.de/*" "http://localhost:8080/*"
], ],
"userinfo_uri": "https://auth2.hottis.de/realms/hottis/protocol/openid-connect/userinfo", "userinfo_uri": "https://auth2.hottis.de/realms/hottis/protocol/openid-connect/userinfo",
"token_uri": "https://auth2.hottis.de/realms/hottis/protocol/openid-connect/token" "token_uri": "https://auth2.hottis.de/realms/hottis/protocol/openid-connect/token"

View File

@ -1,11 +1,24 @@
from flask import Flask, request, render_template, jsonify, redirect, url_for from flask import Flask, request, render_template, jsonify, redirect, url_for, g
import sqlite3 import sqlite3
from flask_oidc import OpenIDConnect
from werkzeug.middleware.proxy_fix import ProxyFix
import os
import json
app = Flask(__name__) app = Flask(__name__)
app.config.update({ app.config.update({
'SECRET_KEY': "fdsgffdgretfsdgfsf" 'SECRET_KEY': os.environ['SECRET'],
'DEBUG': False,
'OIDC_CLIENT_SECRETS': json.loads(os.environ['CLIENT_SECRETS']),
'OIDC_ID_TOKEN_COOKIE_SECURE': False,
'OIDC_USER_INFO_ENABLED': True,
'OIDC_OPENID_REALM': 'hottis',
'OIDC_SCOPES': ['openid', 'email', 'profile']
}) })
oidc = OpenIDConnect(app)
# Datenbankverbindung konfigurieren # Datenbankverbindung konfigurieren
def get_db_connection(): def get_db_connection():
conn = sqlite3.connect('nutrition.db') # 'nutrition.db' ist der Name der Datenbankdatei conn = sqlite3.connect('nutrition.db') # 'nutrition.db' ist der Name der Datenbankdatei
@ -13,42 +26,42 @@ def get_db_connection():
return conn return conn
def init_db(): #def init_db():
conn = get_db_connection() # conn = get_db_connection()
cursor = conn.cursor() # cursor = conn.cursor()
#
# Erstellen der Tabelle # # Erstellen der Tabelle
cursor.execute(''' # cursor.execute('''
CREATE TABLE IF NOT EXISTS nutrition_table ( # CREATE TABLE IF NOT EXISTS nutrition_table (
id INTEGER PRIMARY KEY, # id INTEGER PRIMARY KEY,
name TEXT NOT NULL, # name TEXT NOT NULL,
kcal REAL, # kcal REAL,
EW REAL, # EW REAL,
Fett REAL, # Fett REAL,
KH REAL, # KH REAL,
BST REAL, # BST REAL,
CA REAL # CA REAL
) # )
''') # ''')
#
# Testdaten einfügen # # Testdaten einfügen
test_data = [ # test_data = [
('Apfel', 52, 0.3, 0.2, 14, 0.2, 6), # ('Apfel', 52, 0.3, 0.2, 14, 0.2, 6),
('Banane', 89, 1.1, 0.3, 23, 0.3, 5), # ('Banane', 89, 1.1, 0.3, 23, 0.3, 5),
('Karotte', 41, 0.9, 0.2, 10, 0.2, 3), # ('Karotte', 41, 0.9, 0.2, 10, 0.2, 3),
('Tomate', 18, 0.9, 0.2, 3.9, 0.2, 4), # ('Tomate', 18, 0.9, 0.2, 3.9, 0.2, 4),
('Brokkoli', 34, 2.8, 0.4, 6.6, 0.4, 2), # ('Brokkoli', 34, 2.8, 0.4, 6.6, 0.4, 2),
('Spinat', 23, 2.9, 0.4, 3.6, 0.4, 99), # ('Spinat', 23, 2.9, 0.4, 3.6, 0.4, 99),
('Kartoffel', 77, 2, 0.1, 17, 0.1, 12), # ('Kartoffel', 77, 2, 0.1, 17, 0.1, 12),
('Huhn', 239, 27, 14, 0, 0, 2), # ('Huhn', 239, 27, 14, 0, 0, 2),
('Lachs', 208, 20, 13, 0, 0, 1), # ('Lachs', 208, 20, 13, 0, 0, 1),
('Ei', 155, 13, 11, 1.1, 1, 1) # ('Ei', 155, 13, 11, 1.1, 1, 1)
] # ]
#
cursor.executemany('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, CA) VALUES (?, ?, ?, ?, ?, ?, ?)', test_data) # cursor.executemany('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, CA) VALUES (?, ?, ?, ?, ?, ?, ?)', test_data)
#
conn.commit() # conn.commit()
conn.close() # conn.close()
@ -80,12 +93,14 @@ def calculate_nutrition(food, weight):
# Index-Route # Index-Route
@app.route('/') @app.route('/')
@oidc.require_login
def index(): def index():
return render_template('index.html') return render_template('index.html')
# ... # ...
@app.route('/get_products') @app.route('/get_products')
@oidc.require_login
def get_products(): def get_products():
conn = get_db_connection() conn = get_db_connection()
cursor = conn.cursor() cursor = conn.cursor()
@ -95,13 +110,13 @@ def get_products():
print("ter") print("ter")
return {'products': [product[0] for product in products]} return {'products': [product[0] for product in products]}
# ...
# Route zum Hinzufügen und Berechnen von Lebensmitteln # Route zum Hinzufügen und Berechnen von Lebensmitteln
@app.route('/add_lm', methods=['GET']) @app.route('/add_lm', methods=['GET'])
@oidc.require_login
def add_lm(): def add_lm():
food = request.args.get('food') food = request.args.get('food')
weight = float(request.args.get('weight')) weight = float(request.args.get('weight'))
@ -125,6 +140,7 @@ def add_lm():
@app.route('/add_nutrition', methods=['POST']) @app.route('/add_nutrition', methods=['POST'])
@oidc.accept_token(require_token=True, scopes_required=['openid'])
def add_nutrition(): def add_nutrition():
food = request.form.get('food') food = request.form.get('food')
kcal = float(request.form.get('kcal')) kcal = float(request.form.get('kcal'))
@ -147,6 +163,7 @@ def add_nutrition():
@app.route('/nutrition') @app.route('/nutrition')
@oidc.require_login
def nutrition(): def nutrition():
return render_template('nutrition.html') return render_template('nutrition.html')