Fakultas Ilmu Komputer UI

Commit 65df28b9 authored by zakiraihan's avatar zakiraihan
Browse files

add dummy sso login with akun.cs.ui.ac.id

parent 595168d8
......@@ -3,9 +3,10 @@ import requests
import telegram
import urllib.parse
from flask import Flask, request, render_template, jsonify
from flask import Flask, request, render_template, jsonify, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sso.utils import authenticate, get_cas_client
from sso.csui_helper import get_access_token
global bot
global TOKEN
......@@ -27,14 +28,11 @@ def respond():
botNavigation(bot, update)
return 'ok'
@app.route('/setwebhook/<noNgrok>', methods=['GET', 'POST'])
def set_webhook(noNgrok):
@app.route('/setwebhook', methods=['GET', 'POST'])
def set_webhook():
# we use the bot object to link the bot to our app which live
# in the link provided by URL
print (("https://" + noNgrok + URL + '/'))
if (noNgrok == 'prod'):
noNgrok = ""
s = bot.setWebhook('{URL}{HOOK}'.format(URL=("https://" + noNgrok + URL + '/'), HOOK=TOKEN))
s = bot.setWebhook('{URL}{HOOK}'.format(URL=("https://" + URL + '/'), HOOK=TOKEN))
# something to let us know things work
if s:
return "webhook setup ok"
......@@ -76,10 +74,36 @@ def auth():
@app.route("/auth/", methods=['GET'])
def authGet():
data = request.json
print(data)
ticket = request.args.get('ticket')
print(ticket)
service_url = "https://56e25c7f.ngrok.io/auth/"
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 "AHOYYYY"
@app.route('/loginsso', methods=['GET'])
def renderLoginSSOPage():
return render_template('loginSSO.html')
@app.route('/loginsso/auth', methods=['POST'])
def getLoginSSOData():
username = request.form['username']
password = request.form['password']
try:
access_token = get_access_token(username, password)
except Exception as e:
access_token = None
if (access_token != None):
return render_template('authPhoneNumber.html', username=username)
else:
return "gagal :("
if __name__ == '__main__':
app.run(threaded=True)
\ No newline at end of file
import os
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from models import UserModel
from models import UserModel, AccountModel
from app import app, db
......
from app import db
from sqlalchemy.dialects.postgresql import JSON
class AccountModel(db.Model):
__tablename__ = 'account'
id = db.Column(db.Integer, primary_key=True)
chatId = db.Column(db.String(), unique=True)
phoneNumber = db.Column(db.String(), unique=True, nullable=False)
ssoId = db.Column(db.String(), unique=True, nullable=False)
firstName = db.Column(db.String())
lastName = db.Column(db.String())
def __init__(self, chatId, phoneNumber, ssoId, firstName, lastName):
self.chatId = chatId
self.phoneNumber = phoneNumber
self.ssoId = ssoId
self.firstName = firstName
self.lastName = lastName
def save(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def __repr__(self):
return '<id {}>'.format(self.id)
\ No newline at end of file
import os
import requests
API_MAHASISWA_LIST_URL = "https://api.cs.ui.ac.id/siakngcs/mahasiswa-list/"
API_MAHASISWA = "https://api-dev.cs.ui.ac.id/siakngcs/mahasiswa/"
API_AKUN_CS = "https://akun.cs.ui.ac.id/oauth/token/"
API_VERIFY_USER = "https://akun.cs.ui.ac.id/oauth/token/verify/"
API_CS_CLIENT_SECRET = os.environ.get('API_CS_CLIENT_SECRET')
API_CS_CLIENT_ID = os.environ.get('API_CS_CLIENT_ID')
def get_access_token(username, password):
try:
payload = "username=" + username + "&password=" + password + "&grant_type=password"
headers = {
'authorization': "Basic " + API_CS_CLIENT_SECRET,
'cache-control': "no-cache",
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", API_AKUN_CS, data=payload, headers=headers)
return response.json()["access_token"]
except Exception as e:
raise Exception("username atau password sso salah, input : [{}, {}]".format(username, password,))
def verify_user(access_token):
print ("#get identity number")
parameters = {"access_token": access_token, "client_id": get_client_id()}
response = requests.get(API_VERIFY_USER, params=parameters)
print ("response => ", response.json())
return response.json()
def get_data_user(access_token, id):
print ("#get data user => ", id)
parameters = {"access_token": access_token, "client_id": API_CS_CLIENT_ID}
response = requests.get(API_MAHASISWA+id, params=parameters)
print ("response => ", response.text)
print ("response => ", response.json())
return response.json()
import os
from models.UserModel import UserModel
from models.AccountModel import AccountModel
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
def basic_response(bot, chat_id):
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta content='width=device-width, initial-scale=1' name='viewport' />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Login Fasilkom Telegram Bot</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://sso.ui.ac.id/cas/themes/ui/sso.css" />
<link rel="icon" href="https://sso.ui.ac.id/cas/themes/ui/logo2_0.gif" type="image/x-icon" />
</head>
<body>
<div class="container">
<div class="row justify-content-center align-item-center">
<h1>{{ username }}</h1>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta content='width=device-width, initial-scale=1' name='viewport' />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Login Fasilkom Telegram Bot</title>
<link rel="stylesheet" type="text/css" href="https://sso.ui.ac.id/cas/themes/ui/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="https://sso.ui.ac.id/cas/themes/ui/sso.css" />
<link rel="icon" href="https://sso.ui.ac.id/cas/themes/ui/logo2_0.gif" type="image/x-icon" />
</head>
<body>
<div class="container">
<div class="main">
<head>
<meta content='width=device-width, initial-scale=1' name='viewport' />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Login Fasilkom Telegram Bot</title>
<link rel="stylesheet" type="text/css" href="https://sso.ui.ac.id/cas/themes/ui/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="https://sso.ui.ac.id/cas/themes/ui/sso.css" />
<link rel="icon" href="https://sso.ui.ac.id/cas/themes/ui/logo2_0.gif" type="image/x-icon" />
</head>
<body>
<div class="container">
<div class="main">
<div class="bg"></div>
<div class="login-box">
<form id="fm1" class="login" action="/loginsso/auth" method="post">
<div class="top">
<div class="pull-right"><img src="https://sso.ui.ac.id/cas/themes/ui/logo.png" /></div>
<h1>SSO</h1>
<div class="sub">Single Sign On</div>
</div>
<div class="middle">
<p>Masukkan username dan password UI Anda/<br /><em>Enter your username and password</em>:</p>
<div class="form-group">
<input id="username" name="username" class="form-control" placeholder="Username" type="text" value="" autocomplete="false"/>
</div>
<div class="form-group">
<input id="password" name="password" class="form-control" placeholder="Password" type="password" value="" autocomplete="off"/>
</div>
</div>
<div class="bg"></div>
<div class="login-box">
<form id="fm1" class="login" action="/cas/login" method="post">
<div class="top">
<div class="pull-right"><img src="https://sso.ui.ac.id/cas/themes/ui/logo.png" /></div>
<h1>SSO</h1>
<div class="sub">Single Sign On</div>
</div>
<div class="middle">
<p>Masukkan username dan password UI Anda/<br /><em>Enter your username and password</em>:</p>
<div class="form-group">
<input id="username" name="username" class="form-control" placeholder="Username" type="text" value="" autocomplete="false"/>
</div>
<div class="form-group">
<input id="password" name="password" class="form-control" placeholder="Password" type="password" value="" autocomplete="off"/>
</div>
</div>
<input type="hidden" name="lt" value="LT-103348-2cBkKRmmrCOb0xQdqc2hsTP2mdULjL" />
<input type="hidden" name="execution" value="e2s1" />
<input type="hidden" name="_eventId" value="submit" />
<div class="bottom clearfix">
<div class="pull-right" style="margin-top:12px">
<button type="submit" class="btn btn-lg btn-primary">Login</button>
</div>
<table>
<tr>
<!--
<td style="padding: 10px 10px 10px 10px;">
<div class="help"><a href="#">Lupa password<br /><em>Forget password</em></a></div>
</td>
-->
<td style="padding: 0px 10px 10px 10px;">
<div class="help"><a href="/account/user/register">Daftar Akun<br /><em>Guest Account</em></a></div>
</td>
<td style="border-left: solid #D5D5D5; ">
</td>
<td style="padding: 0px 0px 10px 10px;">
<div class="help"><a href="/account/static/frequently-asked-question-faq">Butuh Bantuan ?<br /><em>Need Help ?</em></a></div>
</td>
</tr>
</table>
</div>
</form>
</div>
<input type="hidden" name="lt" value="LT-103348-2cBkKRmmrCOb0xQdqc2hsTP2mdULjL" />
<input type="hidden" name="execution" value="e2s1" />
<input type="hidden" name="_eventId" value="submit" />
<div class="bottom clearfix">
<div class="pull-right" style="margin-top:12px">
<button type="submit" class="btn btn-lg btn-primary">Login</button>
</div>
<table>
<tr>
<td style="padding: 0px 10px 10px 10px;">
<div class="help"><a href="/account/user/register">Daftar Akun<br /><em>Guest Account</em></a></div>
</td>
<td style="border-left: solid #D5D5D5; ">
</td>
<td style="padding: 0px 0px 10px 10px;">
<div class="help"><a href="/account/static/frequently-asked-question-faq">Butuh Bantuan ?<br /><em>Need Help ?</em></a></div>
</td>
</tr>
</table>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
function hasError(input) {
if (input && /has-error/.test(input.parentElement.className)) return input;
}
function isEmpty(input) {
if (input && input.value == "") return input;
}
var u = document.getElementById("username");
var p = document.getElementById("password");
var candidates = [hasError(u), hasError(p), isEmpty(u), isEmpty(p), u];
for (var i = 0; i < candidates.length; ++i) {
if (candidates[i]) {
candidates[i].focus();
break;
}
}
</script>
</body>
<script type="text/javascript">
function hasError(input) {
if (input && /has-error/.test(input.parentElement.className)) return input;
}
function isEmpty(input) {
if (input && input.value == "") return input;
}
var u = document.getElementById("username");
var p = document.getElementById("password");
var candidates = [hasError(u), hasError(p), isEmpty(u), isEmpty(p), u];
for (var i = 0; i < candidates.length; ++i) {
if (candidates[i]) {
candidates[i].focus();
break;
}
}
</script>
</body>
</html>
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