CLI OAUTH POG

This commit is contained in:
Rushil Umaretiya 2020-06-09 14:48:00 -04:00
parent e151acabfb
commit 45c6c78b52
5 changed files with 118 additions and 5 deletions

9
CLI/index.html Normal file
View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Sign in with Ion</title>
</head>
<body>
Sign in with
</body>
</html>

23
CLI/oauth/index.html Normal file
View File

@ -0,0 +1,23 @@
<html>
<head>
<title>Sign into Ion</title>
<style>
body {
background: #5ac8fb;
background: -webkit-linear-gradient(to left, #52edc7, #5ac8fb);
background: linear-gradient(to left, #52edc7, #5ac8fb);
}
</style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="d-flex align-items-center justify-content-center" style="height: 100vh">
<a href="https://ion.tjhsst.edu/oauth/authorize/?response_type=code&client_id=QeZPBSKqdvWFfBv1VYTSv9iFGz5T9pVJtNUjbEr6&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2F&scope=read&state=QzCUoJEflSWlZe2v7Y3IQEXJFWKIOA" title="Ion" class="border border-dark p-3 btn btn-lg mx-auto" style="box-shadow: 5px 10px;">
<img src="https://ion.tjhsst.edu/static/img/favicon.png">
Sign in with Ion
</a>
</div>
</body>
</html>

23
CLI/oauth/template.html Normal file
View File

@ -0,0 +1,23 @@
<html>
<head>
<title>Sign into Ion</title>
<style>
body {
background: #5ac8fb;
background: -webkit-linear-gradient(to left, #52edc7, #5ac8fb);
background: linear-gradient(to left, #52edc7, #5ac8fb);
}
</style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="d-flex align-items-center justify-content-center" style="height: 100vh">
<a href="AUTH_URL" title="Ion" class="border border-dark p-3 btn btn-lg mx-auto" style="box-shadow: 5px 10px;">
<img src="https://ion.tjhsst.edu/static/img/favicon.png">
Sign in with Ion
</a>
</div>
</body>
</html>

23
CLI/server.py Normal file
View File

@ -0,0 +1,23 @@
from socket import *
from selenium import webdriver
import http.server
import socketserver
import threading
def create_server():
port = 8000
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
print("serving at port:" + str(port))
httpd.serve_forever()
threading.Thread(target=create_server).start()
print("Server has started. Continuing..")
browser = webdriver.Chrome()
browser.get("http://localhost:8000")
assert "<title>" in browser.page_source

View File

@ -3,9 +3,13 @@ from urllib.parse import urlparse
import requests import requests
from requests_oauthlib import OAuth2Session from requests_oauthlib import OAuth2Session
from selenium import webdriver; from selenium import webdriver
import os.path import os.path
import time import time
import http.server
import socketserver
import threading
from werkzeug.urls import url_decode
client_id = r'QeZPBSKqdvWFfBv1VYTSv9iFGz5T9pVJtNUjbEr6' client_id = r'QeZPBSKqdvWFfBv1VYTSv9iFGz5T9pVJtNUjbEr6'
client_secret = r'0Wl3hAIGY9SvYOqTOLUiLNYa4OlCgZYdno9ZbcgCT7RGQ8x2f1l2HzZHsQ7ijC74A0mrOhhCVeZugqAmOADHIv5fHxaa7GqFNtQr11HX9ySTw3DscKsphCVi5P71mlGY' client_secret = r'0Wl3hAIGY9SvYOqTOLUiLNYa4OlCgZYdno9ZbcgCT7RGQ8x2f1l2HzZHsQ7ijC74A0mrOhhCVeZugqAmOADHIv5fHxaa7GqFNtQr11HX9ySTw3DscKsphCVi5P71mlGY'
@ -28,20 +32,45 @@ def main():
else: else:
print(open(".profile", "r").read()) print(open(".profile", "r").read())
while True:
pass
def authenticate(): def authenticate():
oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope) oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope)
authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/") authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/")
web_dir = os.path.join(os.path.dirname(__file__), 'oauth')
os.chdir(web_dir)
if os.path.exists("index.html"):
os.remove("index.html")
template = open("template.html", "rt")
index = open("index.html", "wt")
for line in template:
index.write(line.replace('AUTH_URL', authorization_url))
template.close()
index.close()
threading.Thread(target=create_server).start()
browser = webdriver.Chrome() browser = webdriver.Chrome()
browser.get(authorization_url) browser.get("localhost:8000/")
while "http://localhost:8000/?code" not in browser.current_url: while "http://localhost:8000/?code" not in browser.current_url:
time.sleep(0.25) time.sleep(0.25)
code = urlparse(browser.current_url).query[5:] url = browser.current_url
gets = url_decode(url.replace("http://localhost:8000/?", ""))
code = gets.get("code")
if state == gets.get("state"):
state = gets.get("state")
print("states good")
browser.quit() browser.quit()
print(code)
print(state)
payload = {'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'client_id': client_id, payload = {'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'client_id': client_id,
'client_secret': client_secret, 'csrfmiddlewaretoken': state} 'client_secret': client_secret, 'csrfmiddlewaretoken': state}
token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json() token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json()
@ -57,6 +86,12 @@ def authenticate():
# print(profile) # print(profile)
def create_server():
port = 8000
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
print("serving at port:" + str(port))
httpd.serve_forever()
if __name__ == "__main__": if __name__ == "__main__":
main() main()