From a958b54928164bbb1a458f3bae669a509d33a55e Mon Sep 17 00:00:00 2001 From: Oz Gitelson <47493278+OzGitelson@users.noreply.github.com> Date: Mon, 13 Nov 2023 12:05:40 -0500 Subject: [PATCH] add basic CAS login api integration --- firebase.json | 91 +++++++++++++++++++------------------- frontend/index.html | 65 +++++++++++++++++++++++++-- functions/.gitignore | 0 functions/main.py | 39 ++++++++++++++++ functions/requirements.txt | 2 + package-lock.json | 6 +++ 6 files changed, 154 insertions(+), 49 deletions(-) create mode 100644 functions/.gitignore create mode 100644 functions/main.py create mode 100644 functions/requirements.txt create mode 100644 package-lock.json diff --git a/firebase.json b/firebase.json index f3036a6..f299602 100644 --- a/firebase.json +++ b/firebase.json @@ -1,19 +1,20 @@ { - // "functions": [ - // { - // "source": "functions", - // "codebase": "default", - // "ignore": [ - // "venv", - // ".git", - // "firebase-debug.log", - // "firebase-debug.*.log" - // ] - // } - // ], - // "database": { - // "rules": "database.rules.json" - // }, + "functions": [ + { + "source": "functions", + "codebase": "default", + "ignore": [ + "venv", + ".git", + "firebase-debug.log", + "firebase-debug.*.log" + ], + "requirements": "functions/requirements.txt" + } + ], + "database": { + "rules": "database.rules.json" + }, "hosting": { "public": "frontend", "ignore": [ @@ -26,35 +27,35 @@ }, "storage": { "rules": "storage.rules" + }, + "emulators": { + "auth": { + "port": 9099 + }, + "functions": { + "port": 5001 + }, + "firestore": { + "port": 8080 + }, + "database": { + "port": 9000 + }, + "hosting": { + "port": 5000 + }, + "pubsub": { + "port": 8085 + }, + "storage": { + "port": 9199 + }, + "eventarc": { + "port": 9299 + }, + "ui": { + "enabled": true + }, + "singleProjectMode": true } - // "emulators": { - // "auth": { - // "port": 9099 - // }, - // "functions": { - // "port": 5001 - // }, - // "firestore": { - // "port": 8080 - // }, - // "database": { - // "port": 9000 - // }, - // "hosting": { - // "port": 5000 - // }, - // "pubsub": { - // "port": 8085 - // }, - // "storage": { - // "port": 9199 - // }, - // "eventarc": { - // "port": 9299 - // }, - // "ui": { - // "enabled": true - // }, - // "singleProjectMode": true - // } } diff --git a/frontend/index.html b/frontend/index.html index 25d2b3d..411f6b9 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,9 +1,66 @@ - + - This is the title of the webpage! + + + Welcome to Firebase Hosting + + + + + + + + + + + + + + + + + + -

Hello World

+ + +
+ +
+ + + + - \ No newline at end of file + diff --git a/functions/.gitignore b/functions/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/functions/main.py b/functions/main.py new file mode 100644 index 0000000..32e42b8 --- /dev/null +++ b/functions/main.py @@ -0,0 +1,39 @@ +# Welcome to Cloud Functions for Firebase for Python! +# To get started, simply uncomment the below code or create your own. +# Deploy with `firebase deploy` +from urllib.request import urlopen + +from firebase_functions import https_fn +from firebase_admin import initialize_app, auth +from flask import redirect +from xmltodict import parse + +initialize_app() + + +@https_fn.on_request() +def login(req: https_fn.Request) -> https_fn.Response: + # response=https_fn.Response() + + redirect_url = '''https://secure.its.yale.edu/cas/login?service=http%3A%2F%2F127.0.0.1%3A5001%2Fmajoraudit%2Fus-central1%2Fvalidate''' + return redirect(redirect_url) + +@https_fn.on_request() +def validate(req: https_fn.Request) -> https_fn.Response: + response=https_fn.Response() + + # redirect_url = '''https://secure.its.yale.edu/cas/login?service=http%3A%2F%2F127.0.0.1%3A5001%2Fmajoraudit%2Fus-central1%2Flogin''' + if 'ticket' in req.args: + val_url=f'''https://secure.its.yale.edu/cas/serviceValidate?service=http%3A%2F%2F127.0.0.1%3A5001%2Fmajoraudit%2Fus-central1%2Fvalidate&ticket={req.args['ticket']}''' + + xml_dump = urlopen(val_url).read().strip().decode('utf8', 'ignore') + try: + xml_dic = parse(xml_dump)["cas:serviceResponse"]["cas:authenticationSuccess"] + username=xml_dic["cas:user"] + resp=username + except: + resp='invalid CAS ticket' + + else: + resp='no ticket found' + return https_fn.Response(resp) diff --git a/functions/requirements.txt b/functions/requirements.txt new file mode 100644 index 0000000..0e1156a --- /dev/null +++ b/functions/requirements.txt @@ -0,0 +1,2 @@ +firebase_functions~=0.1.0 +xmltodict diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7300504 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "MajorAudit", + "lockfileVersion": 2, + "requires": true, + "packages": {} +}