diff --git a/tutorials/apps/login/awscontroller.py b/tutorials/apps/login/awscontroller.py index 436c65d2f2d8ea2684b484910eb34b7fcbf67aeb..1ad5a2f1cc590eff3b4719b8d232277eb8533472 100644 --- a/tutorials/apps/login/awscontroller.py +++ b/tutorials/apps/login/awscontroller.py @@ -5,7 +5,11 @@ import os import json load_dotenv() task_arn_dict = {} -from login import log_into_file +from datetime import datetime +def log_into_file(info): + with open("log/app.log", "a", encoding="utf-8") as f: + f.write(f"{datetime.now().strftime('%d-%m-%Y %H:%M:%S')} - {info}\n") + def run_ecs_container_fargate(new_image, email): """ 1. Describes 'atlas-task' to get its container definitions. @@ -22,6 +26,7 @@ def run_ecs_container_fargate(new_image, email): security_groups = json.loads(os.getenv("SECURITY_GROUPS", [])) try: + # Check if user email based task definition exists first try: existing_td_resp = ecs_client.describe_task_definition(taskDefinition=user_family) print(f"Found existing task definition family: {user_family}") diff --git a/tutorials/apps/login/login.py b/tutorials/apps/login/login.py index e74ffe29ccc0b645540ae1ae8c8ee462c203b1ac..42bdd5b26135001180c36ed3aa1de39942165dea 100644 --- a/tutorials/apps/login/login.py +++ b/tutorials/apps/login/login.py @@ -44,8 +44,8 @@ oauth.register( def log_into_file(info): - with open("../../log/app.log", "a", encoding="utf-8") as f: - f.write(f"{datetime.now().strftime("%d-%m-%Y %H:%M:%S")} - {info}\n") + with open("log/app.log", "a", encoding="utf-8") as f: + f.write(f"{datetime.now().strftime('%d-%m-%Y %H:%M:%S')} - {info}\n") def sanitize_email(email): return re.sub(r'[^a-zA-Z0-9_-]', '-', email) @@ -120,13 +120,15 @@ def index(): @app.route('/dashboard') def dashboard(): - userinfo = session.get('user') - if not userinfo: - return redirect(url_for('index')) - - containerImage = userinfo.get("custom:containerImage", "<not found>") - log_into_file(f"User {userinfo.get('sub')} - launching container with image name: {containerImage}") - return f""" + userinfo = session.get('user') + if not userinfo: + return redirect(url_for('index')) + + containerImage = userinfo.get("custom:containerImage", "<not found>") + id_token = request.args.get("token", "") + log_into_file(f"User {userinfo.get('sub')} - launching container with image name: {containerImage}") + + return f""" <html> <head> <title>Starting Container</title> @@ -137,7 +139,6 @@ def dashboard(): font-family: Arial, sans-serif; background-color: #f8f9fa; }} - .container {{ max-width: 600px; margin: 60px auto; @@ -147,19 +148,16 @@ def dashboard(): border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }} - h1 {{ margin-top: 0; font-size: 2em; color: #333; }} - p {{ font-size: 1em; color: #666; line-height: 1.5; }} - .loader {{ margin: 40px auto; border: 16px solid #f3f3f3; @@ -169,27 +167,21 @@ def dashboard(): height: 60px; animation: spin 2s linear infinite; }} - @keyframes spin {{ 0% {{ transform: rotate(0deg); }} 100% {{ transform: rotate(360deg); }} }} </style> <script> - function getCookie(name) {{ - const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); - return match ? match[2] : ''; - }} + const idToken = "{id_token}"; window.addEventListener('DOMContentLoaded', () => {{ - const myToken = getCookie('my_token'); - fetch('/launch?containerImage={containerImage}') .then(response => response.json()) .then(data => {{ if (data.public_ip) {{ window.location.href = 'http://' + data.public_ip + ':5006/app?token=' - + encodeURIComponent(myToken) + '&ip=' + encodeURIComponent(data.public_ip); + + encodeURIComponent(idToken) + '&ip=' + encodeURIComponent(data.public_ip); }} else {{ document.body.innerHTML = ` <div class="container"> @@ -203,7 +195,7 @@ def dashboard(): document.body.innerHTML = ` <div class="container"> <h1>Request Failed</h1> - <p>(err)</p> + <p>${{err}}</p> </div> `; }}); @@ -266,6 +258,7 @@ def authorize(): """ if 'code' not in request.args: return redirect(url_for('index')) + token = oauth.oidc.authorize_access_token() # token usually includes 'access_token', 'id_token', 'refresh_token', etc. @@ -275,17 +268,15 @@ def authorize(): return redirect(url_for('index')) session['user'] = userinfo id_token = token.get('id_token') + user_sub = userinfo.get('sub') if user_sub: log_into_file(f"User {user_sub} logged in successfully.") else: log_into_file("User with no 'sub' property logged in.") #decoded = jwt.decode(id_token, options={"verify_signature": False})# debugging - resp = make_response(redirect(url_for('dashboard'))) - if 'sub' in userinfo: - resp.set_cookie('my_token', id_token, httponly=False) - else: - resp.set_cookie('my_token', 'No_id_token', httponly=False) + resp = make_response(redirect(url_for('dashboard', token=id_token))) + return resp