20
20
Thanks to Chiroyce (https://replit.com/@Chiroyce/auth) for part of the code! Truly the GOAT.
21
21
"""
22
22
23
- def base64 (string ):
23
+ def encode_base64 (string ):
24
24
return b64encode (string .encode ("utf-8" )).decode ()
25
25
26
26
def generate_random_code ():
@@ -38,24 +38,30 @@ def auth():
38
38
if "username" not in session :
39
39
# Generate a random code using the generate_random_code function
40
40
random_code = generate_random_code ()
41
- return redirect (f"https://auth.itinerary.eu.org/auth/?redirect={ base64 ('https://scratch-coding-hut.github.io/auth' ) } &name=NotFenixio%27s%20ScratchAuth%20Example&code={ random_code } " )
41
+ return redirect (f"https://auth.itinerary.eu.org/auth/?redirect={ encode_base64 ('https://scratch-coding-hut.github.io/auth' )} &name=NotFenixio%27s%20ScratchAuth%20Example&code={ random_code } " )
42
42
else :
43
43
return render_template ("auth.html" , username = session ["username" ])
44
44
45
- @app .get ("/auth " )
45
+ @app .get ("/authenticate " )
46
46
def authenticate ():
47
47
code = request .args .get ("privateCode" )
48
48
49
49
if code is None :
50
50
return "Bad Request" , 400
51
51
52
- response = get (f"https://auth.itinerary.eu.org/api/auth/verifyToken?privateCode={ code } " ).json ()
53
- if response ["redirect" ] == "https://scratch-coding-hut.github.io/auth" :
54
- if response ["valid" ]:
55
- session ["username" ] = response ["username" ]
52
+ response = get (f"https://auth.itinerary.eu.org/api/auth/verifyToken?privateCode={ code } " )
53
+
54
+ if response .status_code != 200 :
55
+ return "Error communicating with authentication service" , 500
56
+
57
+ response_json = response .json ()
58
+
59
+ if response_json .get ("redirect" ) == "https://scratch-coding-hut.github.io/auth" :
60
+ if response_json .get ("valid" ):
61
+ session ["username" ] = response_json ["username" ]
56
62
return redirect ("/auth" )
57
63
else :
58
- return "Authentication failed!"
64
+ return "Authentication failed!" , 401
59
65
else :
60
66
return "Invalid Redirect" , 400
61
67
0 commit comments