-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_keys.py
97 lines (77 loc) · 2.53 KB
/
generate_keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import json
from Crypto.PublicKey import RSA
from jwcrypto.jwk import JWK
from main import app, db, LTIConfig
# LTIConfig Reference
# class LTIConfig(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# iss = db.Column(db.Text)
# client_id = db.Column(db.Text)
# auth_login_url = db.Column(db.Text)
# auth_token_url = db.Column(db.Text)
# key_set_url = db.Column(db.Text)
# private_key_file = db.Column(db.Text)
# public_key_file = db.Column(db.Text)
# deployment_id = db.Column(db.Text)
# Create app context so database doesn't complain about it
app.app_context().push()
print("Creating Database if it doesn't exist...")
db.create_all()
print("Starting key generation...")
key = RSA.generate(4096)
print("Generating Private Key...")
private_key = key.exportKey()
print("Generating Public Key...")
public_key = key.publickey().exportKey()
print("Converting Keys to JWKS...")
jwk_obj = JWK.from_pem(public_key)
public_jwk = json.loads(jwk_obj.export_public())
public_jwk["alg"] = "RS256"
public_jwk["use"] = "sig"
public_jwk_str = json.dumps(public_jwk)
canvas_url = """
What is your canvas url?
1 - https://canvas.instructure.com/
2 - https://canvas.test.instructure.com/
3 - Other
"""
print(canvas_url)
server_url = input()
if server_url == "1":
server_url = "https://canvas.instructure.com"
if server_url == "2":
server_url = "https://canvas.test.instructure.com"
if server_url == "3":
print("Please type your server url: ")
server_url = input()
if ".test." in server_url:
issuer = "https://canvas.test.instructure.com"
else:
issuer = "https://canvas.instructure.com"
lticonfig = LTIConfig(
iss=issuer,
client_id="CHANGEME",
auth_login_url="%s/api/lti/authorize_redirect" % server_url,
auth_token_url="%s/login/oauth2/token" % server_url,
key_set_url="%s/api/lti/security/jwks" % server_url,
private_key_file=private_key.decode("utf-8"),
public_key_file=public_key.decode("utf-8"),
public_jwk=public_jwk_str,
deployment_id="{CHANGEME:CHANGEME}",
)
db.session.add(lticonfig)
db.session.commit()
print("JSON url: http://127.0.0.1:8000/cyclops/config/%s/json" % lticonfig.id)
message = """
You will now need to install the tool into your LMS, and update the Deployment ID
and Client ID via your database manager of choice, or from here:
"""
print(message)
print("Client ID: ")
client_id = input()
print("Deployment ID: ")
deployment_id = input()
lticonfig.deployment_id = deployment_id
lticonfig.client_id = client_id
db.session.add(lticonfig)
db.session.commit()