2020Thanks to Chiroyce (https://replit.com/@Chiroyce/auth) for part of the code! Truly the GOAT.
2121"""
2222
23- def base64 (string ):
23+ def encode_base64 (string ):
2424 return b64encode (string .encode ("utf-8" )).decode ()
2525
2626def generate_random_code ():
@@ -38,24 +38,30 @@ def auth():
3838 if "username" not in session :
3939 # Generate a random code using the generate_random_code function
4040 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 } " )
4242 else :
4343 return render_template ("auth.html" , username = session ["username" ])
4444
45- @app .get ("/auth " )
45+ @app .get ("/authenticate " )
4646def authenticate ():
4747 code = request .args .get ("privateCode" )
4848
4949 if code is None :
5050 return "Bad Request" , 400
5151
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" ]
5662 return redirect ("/auth" )
5763 else :
58- return "Authentication failed!"
64+ return "Authentication failed!" , 401
5965 else :
6066 return "Invalid Redirect" , 400
6167
0 commit comments