Fakultas Ilmu Komputer UI

Commit ab9b8b8e authored by zakiraihan's avatar zakiraihan
Browse files

Try to add sso login

parent 70b5dc27
......@@ -148,3 +148,4 @@ ENV/
# End of https://www.gitignore.io/api/python,intellij+iml
.vscode/
......@@ -3,8 +3,9 @@ import requests
import telegram
import urllib.parse
from flask import Flask, request, render_template
from flask import Flask, request, render_template, jsonify
from flask_sqlalchemy import SQLAlchemy
from sso.utils import authenticate, get_cas_client
global bot
global TOKEN
......@@ -58,9 +59,26 @@ def sendNotif():
r = requests.get(url = url)
return r.json()
@app.route('/loginsso', methods=['GET'])
def renderLoginSSOPage():
return render_template('loginSSO.html')
@app.route("/auth/", methods=['POST'])
def auth():
data = request.json
ticket = data.get("ticket")
service_url = data.get("service_url")
if (ticket is not None) and (service_url is not None):
client = get_cas_client(service_url)
sso_profile = authenticate(ticket, client)
if sso_profile is not None:
user_data = sso_profile
print(sso_profile)
return (jsonify(user_data), 200)
return (jsonify(), 400)
@app.route("/auth/", methods=['GET'])
def authGet():
data = request.json
print(data)
return "AHOYYYY"
if __name__ == '__main__':
......
......@@ -11,12 +11,16 @@ def botNavigation(bot, update):
basic_response(bot, chat_id)
elif (text == "/about"):
about_response(bot, chat_id)
elif (text == "/login"):
user_login(bot, chat_id)
elif (text == "/activateNotif"):
user_activate_notif(bot, chat_id)
elif (text == None and contact != None) :
save_user_data(bot, chat_id, contact)
elif (text == "/deactivateNotif"):
user_deactive_notif(bot, chat_id)
elif (text == "/login"):
user_login_sso(bot, chat_id)
elif (text == "/logout"):
user_logout(bot, chat_id)
user_logout_sso(bot, chat_id)
else :
command_not_found(bot, chat_id, msg_id)
\ No newline at end of file
alembic==1.2.0
asn1crypto==0.24.0
cas-client==1.0.0
certifi==2019.9.11
cffi==1.12.3
chardet==3.0.4
Click==7.0
cryptography==2.7
filelock==3.0.12
Flask==1.1.1
Flask-Migrate==1.8.0
Flask-Script==2.0.6
......@@ -12,18 +14,31 @@ Flask-SQLAlchemy==2.1
future==0.17.1
gunicorn==19.9.0
idna==2.8
importlib-metadata==0.23
itsdangerous==1.1.0
Jinja2==2.10.1
lxml==4.4.1
Mako==1.1.0
MarkupSafe==1.1.1
more-itertools==7.2.0
packaging==19.2
pluggy==0.13.0
psycopg2==2.8.3
py==1.8.0
pycparser==2.19
pycryptodome==3.9.0
pyparsing==2.4.2
python-cas==1.4.0
python-dateutil==2.8.0
python-editor==1.0.4
python-telegram-bot==12.1.1
requests==2.22.0
six==1.12.0
SQLAlchemy==1.3.8
toml==0.10.0
tornado==6.0.3
tox==3.14.0
urllib3==1.25.5
virtualenv==16.7.5
Werkzeug==0.16.0
zipp==0.6.0
This diff is collapsed.
import os
import json
from cas import CASClient
from flask import current_app as app
def get_cas_client(service_url=None, request=None):
server_url = f"{os.environ.get('SSO_UI_URL')}"
if server_url and request and server_url.startswith("/"):
scheme = request.headers.get("X-Forwarded-Proto", request.scheme)
server_url = scheme + "://" + request.headers["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
import os
from models.UserModel import UserModel
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
......@@ -13,7 +14,7 @@ def command_not_found(bot, chat_id, msg_id):
response = "Sorry your command is not in bot"
bot.sendMessage(chat_id=chat_id, text=response, reply_to_message_id=msg_id)
def user_login(bot, chat_id):
def user_activate_notif(bot, chat_id):
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first()
if (checkUserExistance is None):
con_keyboard = KeyboardButton(text="Send_contact", request_contact=True)
......@@ -26,7 +27,7 @@ def user_login(bot, chat_id):
else:
bot.sendMessage(chat_id=chat_id, text="You have been registered", reply_markup=ReplyKeyboardRemove())
def save_user_data(bot, chat_id, contact):
def save_user_notif_activation_data(bot, chat_id, contact):
checkUserExistance = UserModel.query.filter(UserModel.userId==str(contact.user_id), UserModel.phoneNumber==str(contact.phone_number)).first()
if (checkUserExistance is None):
user = UserModel(str(contact.user_id), str(contact.phone_number), contact.first_name, contact.last_name)
......@@ -35,11 +36,27 @@ def save_user_data(bot, chat_id, contact):
else:
bot.sendMessage(chat_id=chat_id, text="You have been registered", reply_markup=ReplyKeyboardRemove())
def user_logout(bot, chat_id):
def user_deactive_notif(bot, chat_id):
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first()
if (checkUserExistance is None):
bot.sendMessage(chat_id=chat_id, text="You are not registerd yet", reply_markup=ReplyKeyboardRemove())
else:
checkUserExistance = UserModel.query.filter(UserModel.userId==str(chat_id)).first()
checkUserExistance.delete()
bot.sendMessage(chat_id=chat_id, text="Successfully logged out from bot", reply_markup=ReplyKeyboardRemove())
\ No newline at end of file
bot.sendMessage(chat_id=chat_id, text="Successfully logged out from bot", reply_markup=ReplyKeyboardRemove())
def user_login_sso(bot, chat_id):
base_url = os.environ.get('URL')
sso_url = os.environ.get('SSO_UI_URL')
app_auth_url = "https://6e71a1d8" + base_url + '/auth/'
URL = sso_url + "/login?service=" + app_auth_url
response = "Please login through this link:\n" + URL
bot.sendMessage(chat_id=chat_id, text=response)
def user_logout_sso(bot, chat_id):
base_url = os.environ.get('URL')
sso_url = os.environ.get('SSO_UI_URL')
app_auth_url = "https://6e71a1d8" + base_url + '/auth/'
URL = sso_url + "/logout?url=" + app_auth_url
response = "Please logout through this link:\n" + URL
bot.sendMessage(chat_id=chat_id, text=response)
Supports Markdown
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