Fakultas Ilmu Komputer UI

Commit cc29a435 authored by DhanarSantika's avatar DhanarSantika
Browse files

Initial Commit

parents
env/
.vscode/
__pycache__/
\ No newline at end of file
from flask import Flask, redirect
from flask_sso_ui.decorators import login_sso_ui, logout_sso_ui
import json
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/login')
@login_sso_ui(force_login=True)
def login(sso_profile):
return json.dumps(sso_profile)
@app.route('/logout')
@logout_sso_ui
def logout():
pass
\ No newline at end of file
This diff is collapsed.
import json
from functools import wraps
from urllib.parse import urlparse
from flask import (
Flask,
redirect,
request,
session
)
from .utils import (
get_cas_client,
get_service_url,
authenticate,
normalize_username,
get_additional_info,
)
def login_sso_ui(force_login=True):
def decorator(func):
def wrapper(*args, **kwargs):
service_url = get_service_url(request)
client = get_cas_client(service_url)
login_url = client.get_login_url()
ticket = request.args.get("ticket")
if ticket:
sso_profile = authenticate(ticket, client)
if sso_profile is None and force_login:
return redirect(login_url)
kwargs.update({"sso_profile": sso_profile})
return func(*args, **kwargs)
else:
return redirect(login_url)
return wrapper
return decorator
def logout_sso_ui(func):
@wraps(func)
def decorator(*args, **kwargs):
service_url = get_service_url(request)
service_protocol = urlparse(service_url).scheme
service_host = urlparse(service_url).hostname
service_port = urlparse(service_url).port
if service_port:
service_address = service_protocol + "://" + service_host + ":" + str(service_port)
else:
service_address = service_protocol + "://" + service_host
client = get_cas_client(service_url)
logout_url = client.get_logout_url(redirect_url=service_address)
try:
request.args.get("ticket")
return redirect(logout_url)
except:
return redirect('/')
return decorator
\ No newline at end of file
import os
import json
from cas import CASClient
from urllib.parse import urlunparse
SSO_UI_URL = "https://sso.ui.ac.id/cas2/"
SSO_UI_FORCE_SERVICE_HTTPS = False
def normalize_username(username):
return username.lower()
def get_protocol(request):
if request.is_secure or SSO_UI_FORCE_SERVICE_HTTPS:
return "https"
return "http"
def get_service_url(request, redirect_to=None):
protocol = get_protocol(request)
host = request.host
service = urlunparse((protocol, host, request.path, "", "", ""))
return service
def get_cas_client(service_url=None, request=None):
server_url = SSO_UI_URL
if server_url and request and server_url.startswith("/"):
scheme = request.META.get("X-Forwarded-Proto", request.scheme)
server_url = scheme + "://" + request.META["HTTP_HOST"] + server_url
return CASClient(service_url=service_url, server_url=server_url, version=2)
def authenticate(ticket, client):
username, attributes, _ = client.verify_ticket(ticket)
if not username:
return None
if "kd_org" in attributes:
attributes.update(get_additional_info(attributes["kd_org"]) or {})
sso_profile = {"username": username, "attributes": attributes}
return sso_profile
def get_additional_info(kd_org):
path = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(path, "additional-info.json")
with open(filename, "r") as fd:
as_json = json.load(fd)
if kd_org in as_json:
return as_json[kd_org]
return None
Flask==1.1.1
requests==2.22.0
python-cas==1.4.0
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment