diff --git a/.profile b/.profile deleted file mode 100644 index df779be..0000000 --- a/.profile +++ /dev/null @@ -1 +0,0 @@ -{'absences': 2, 'address': None, 'counselor': {'first_name': 'Sean', 'full_name': 'Sean Burke', 'id': 37, 'last_name': 'Burke', 'url': 'https://ion.tjhsst.edu/api/profile/37', 'user_type': 'counselor', 'username': 'SPBurke'}, 'display_name': 'Raffu Khondaker', 'emails': [], 'first_name': 'Raffu', 'full_name': 'Raffu Khondaker', 'grade': {'name': 'sophomore', 'number': 10}, 'graduation_year': 2022, 'id': 36508, 'ion_username': '2022rkhondak', 'is_announcements_admin': False, 'is_eighth_admin': False, 'is_student': True, 'is_teacher': False, 'last_name': 'Khondaker', 'middle_name': 'Al', 'nickname': '', 'phones': [], 'picture': 'https://ion.tjhsst.edu/api/profile/36508/picture', 'sex': 'Male', 'short_name': 'Raffu', 'title': None, 'tj_email': '2022rkhondak@tjhsst.edu', 'user_type': 'student', 'websites': []} \ No newline at end of file diff --git a/CLI/.profile b/CLI/.profile deleted file mode 100644 index ca78410..0000000 --- a/CLI/.profile +++ /dev/null @@ -1 +0,0 @@ -{'id': 1000417, 'ion_username': '2023rumareti', 'sex': 'Male', 'title': None, 'display_name': 'Rushil Umaretiya', 'full_name': 'Rushil Umaretiya', 'short_name': 'Rushil', 'first_name': 'Rushil', 'middle_name': 'Haresh', 'last_name': 'Umaretiya', 'nickname': None, 'tj_email': '2023rumareti@tjhsst.edu', 'emails': ['rushilwiz@gmail.com', 'r@crucialnet.org'], 'grade': {'number': 9, 'name': 'freshman'}, 'graduation_year': 2023, 'user_type': 'student', 'phones': ['Mobile Phone: 7034570803'], 'websites': ['http://crucialnet.org'], 'counselor': {'id': 115, 'url': 'https://ion.tjhsst.edu/api/profile/115', 'user_type': 'counselor', 'username': 'kchamblin', 'full_name': 'Kerry Hamblin', 'first_name': 'Kerry', 'last_name': 'Hamblin'}, 'address': None, 'picture': 'https://ion.tjhsst.edu/api/profile/1000417/picture', 'is_eighth_admin': False, 'is_announcements_admin': False, 'is_teacher': False, 'is_student': True, 'absences': 1} diff --git a/CLI/oauth/index.html b/CLI/oauth/index.html index cdce72e..1209807 100644 --- a/CLI/oauth/index.html +++ b/CLI/oauth/index.html @@ -14,10 +14,10 @@
- - + + Sign in with Ion
- + \ No newline at end of file diff --git a/CLI/skoolos.py b/CLI/skoolos.py index 73c7e37..9477806 100644 --- a/CLI/skoolos.py +++ b/CLI/skoolos.py @@ -37,7 +37,14 @@ def main(): input("Welcome to SkoolOS. Press any key to create an account") authenticate() else: - print(open(".profile", "r").read()) + file = open('key.key', 'rb') + key = file.read() # The key will be type bytes + file.close() + f = Fernet(key) + file = open('.profile', 'rb') + p = file.read() # The key will be type bytes + file.close() + # while True: # pass @@ -50,9 +57,14 @@ def authenticate(): #Linux: chromdriver-linux #Macos: chromdriver-mac #Windows: chromdriver.exe + if('CLI' in os.getcwd()): + path = os.path.join(os.getcwd(), '../','chromedriver-mac') + else: + path = os.path.join(os.getcwd(), 'chromedriver-mac') - + browser = webdriver.Chrome(path) web_dir = os.path.join(os.path.dirname(__file__), 'oauth') + print(web_dir) os.chdir(web_dir) if os.path.exists("index.html"): os.remove("index.html") @@ -86,13 +98,13 @@ def authenticate(): print("states good") browser.quit() - print(code) + #print(code) print(state) payload = {'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'client_id': client_id, 'client_secret': client_secret, 'csrfmiddlewaretoken': state} token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json() - print(token) + #print(token) headers = {'Authorization': f"Bearer {token['access_token']}"} # And finally get the user's profile! @@ -105,14 +117,18 @@ def authenticate(): last_name = profile['last_name'] os.chdir(cdir) - profileFile = open(".profile", "w") - #profileFile.write(profile.text()) - key = Fernet.generate_key() - file = open('key.key', 'wb') - file.write(key) # The key is type bytes still - file.close() - profileFile.write(str(profile)) - profileFile.close() + # key = Fernet.generate_key() + # file = open('key.key', 'wb') + # file.write(key) # The key is type bytes still + # file.close() + # p = str(profile).encode() + # f = Fernet(key) + # encrypted = f.encrypt(p) + + # profileFile = open(".profile", "wb") + # #profileFile.write(profile.text()) + # profileFile.write(encrypted) + # profileFile.close() sys.exit diff --git a/Website/api/migrations/0001_initial.py b/Website/api/migrations/0001_initial.py index 37f2d7e..ec9b9d7 100644 --- a/Website/api/migrations/0001_initial.py +++ b/Website/api/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.7 on 2020-06-13 18:15 +# Generated by Django 3.0.7 on 2020-06-14 19:59 from django.conf import settings from django.db import migrations, models @@ -41,19 +41,15 @@ class Migration(migrations.Migration): migrations.CreateModel( name='Student', fields=[ - ('created', models.DateTimeField(auto_now_add=True)), - ('first_name', models.CharField(max_length=100)), - ('last_name', models.CharField(max_length=100)), + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('student_id', models.IntegerField()), - ('ion_user', models.CharField(max_length=100, primary_key=True, serialize=False)), - ('email', models.CharField(blank=True, default='', max_length=100)), ('grade', models.IntegerField()), ('git', models.CharField(max_length=100)), ('repo', models.URLField(blank=True, default='')), ('classes', models.CharField(blank=True, default='', max_length=100)), ('added_to', models.CharField(blank=True, default='', max_length=100)), ('completed', models.TextField(blank=True, default='')), - ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='students', to=settings.AUTH_USER_MODEL)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], ), migrations.CreateModel( @@ -67,7 +63,7 @@ class Migration(migrations.Migration): ('default_file', models.CharField(blank=True, default='', max_length=100)), ('confirmed', models.TextField(blank=True, default='')), ('unconfirmed', models.TextField(blank=True, default='')), - ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='classes', to=settings.AUTH_USER_MODEL)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], ), migrations.CreateModel( diff --git a/Website/api/serializers.py b/Website/api/serializers.py index 2b4bb54..2a05750 100644 --- a/Website/api/serializers.py +++ b/Website/api/serializers.py @@ -10,7 +10,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User - fields = ['id', 'username', 'students','teachers'] + fields = ['id', 'username'] # class DefFilesSerializer(serializers.HyperlinkedModelSerializer): # class Meta: diff --git a/Website/users/migrations/0001_initial.py b/Website/users/migrations/0001_initial.py index 86549e5..bbc2ba6 100644 --- a/Website/users/migrations/0001_initial.py +++ b/Website/users/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.7 on 2020-06-13 08:25 +# Generated by Django 3.0.7 on 2020-06-14 19:59 from django.db import migrations, models diff --git a/Website/users/pwd.py b/Website/users/pwd.py new file mode 100644 index 0000000..a91574a --- /dev/null +++ b/Website/users/pwd.py @@ -0,0 +1,6 @@ +import os + +pwd = "heyyy" +path = os.getcwd() +p = os.path.join(path, '../../', 'pwd.txt') +open(p, 'w') \ No newline at end of file diff --git a/Website/users/views.py b/Website/users/views.py index 3078208..7d180af 100644 --- a/Website/users/views.py +++ b/Website/users/views.py @@ -16,6 +16,7 @@ from django.contrib.auth import login as auth_login from django.contrib.auth import logout as auth_logout from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required +import os # Create your views here. # Thanks Django, what would I do without this comment diff --git a/skoolos.py b/skoolos.py new file mode 100644 index 0000000..43e3e42 --- /dev/null +++ b/skoolos.py @@ -0,0 +1,144 @@ +import sys +from urllib.parse import urlparse +import requests +from requests_oauthlib import OAuth2Session +from selenium import webdriver +import os.path +import time +import http.server +import socketserver +from threading import Thread +from werkzeug.urls import url_decode +import pprint +from PyInquirer import prompt, print_json +import json +import os +import argparse +from cryptography.fernet import Fernet + +client_id = r'QeZPBSKqdvWFfBv1VYTSv9iFGz5T9pVJtNUjbEr6' +client_secret = r'0Wl3hAIGY9SvYOqTOLUiLNYa4OlCgZYdno9ZbcgCT7RGQ8x2f1l2HzZHsQ7ijC74A0mrOhhCVeZugqAmOADHIv5fHxaa7GqFNtQr11HX9ySTw3DscKsphCVi5P71mlGY' +redirect_uri = 'http://localhost:8000/callback/' +token_url = 'https://ion.tjhsst.edu/oauth/token/' +scope = ["read"] + + +def main(): + print("") + print("░██████╗██╗░░██╗░█████╗░░█████╗░██╗░░░░░  ░█████╗░░██████╗") + print("██╔════╝██║░██╔╝██╔══██╗██╔══██╗██║░░░░░  ██╔══██╗██╔════╝") + print("╚█████╗░█████═╝░██║░░██║██║░░██║██║░░░░░  ██║░░██║╚█████╗░") + print("░╚═══██╗██╔═██╗░██║░░██║██║░░██║██║░░░░░  ██║░░██║░╚═══██╗") + print("██████╔╝██║░╚██╗╚█████╔╝╚█████╔╝███████╗  ╚█████╔╝██████╔╝") + print("╚═════╝░╚═╝░░╚═╝░╚════╝░░╚════╝░╚══════╝  ░╚════╝░╚═════╝░") + print("") + + if not os.path.exists(".profile"): + input("Welcome to SkoolOS. Press any key to create an account") + authenticate() + else: + file = open('key.key', 'rb') + key = file.read() # The key will be type bytes + file.close() + f = Fernet(key) + file = open('.profile', 'rb') + p = file.read() # The key will be type bytes + file.close() + + + # while True: + # pass + +def authenticate(): + oauth = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri, scope=scope) + authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/") + + cdir = os.getcwd() + #Linux: chromdriver-linux + #Macos: chromdriver-mac + #Windows: chromdriver.exe + if('CLI' in os.getcwd()): + path = os.path.join(os.getcwd(), '../','chromedriver-mac') + else: + path = os.path.join(os.getcwd(), 'chromedriver-mac') + + browser = webdriver.Chrome(path) + web_dir = os.path.join(os.getcwd(), 'CLI', 'oauth') + print(web_dir) + os.chdir(web_dir) + if os.path.exists("index.html"): + os.remove("index.html") + + template = open("template.html", "r") + index = open("index.html", "w") + for line in template: + index.write(line.replace('AUTH_URL', authorization_url)) + template.close() + index.close() + + server = Thread(target=create_server) + server.daemon = True + server.start() + + browser.get("localhost:8000/") + + while "http://localhost:8000/callback/?code" not in browser.current_url: + time.sleep(0.25) + + url = browser.current_url + gets = url_decode(url.replace("http://localhost:8000/callback/?", "")) + while "http://localhost:8000/callback/?code" not in browser.current_url: + time.sleep(0.25) + + url = browser.current_url + gets = url_decode(url.replace("http://localhost:8000/callback/?", "")) + code = gets.get("code") + if state == gets.get("state"): + state = gets.get("state") + print("states good") + browser.quit() + + #print(code) + print(state) + + payload = {'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'client_id': client_id, + 'client_secret': client_secret, 'csrfmiddlewaretoken': state} + token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json() + #print(token) + headers = {'Authorization': f"Bearer {token['access_token']}"} + + # And finally get the user's profile! + profile = requests.get("https://ion.tjhsst.edu/api/profile", headers=headers).json() + + #pprint.pprint(profile) + username = profile['ion_username'] + email = profile['tj_email'] + first_name = profile['first_name'] + last_name = profile['last_name'] + password = input("Enter SkoolOS password: ") + profile = { + 'username':username, + 'email':email, + 'first_name':first_name, + 'last_name':last_name, + 'password':password, + } + os.chdir(cdir) + + profileFile = open(".profile", "w") + #profileFile.write(profile.text()) + profileFile.write(str(profile)) + profileFile.close() + + sys.exit + + +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__": + main()