44import requests
55import os
66
7- client_id = os .getenv ('GAMMA_CLIENT_ID' , '' )
8- client_secret = os .getenv ('GAMMA_CLIENT_SECRET' , '' )
9- redirect_uri = os .getenv ('GAMMA_REDIRECT_URI' , 'http://localhost:5000/api/auth/callbacks/gamma' )
10- auth_uri = os .getenv ('GAMMA_AUTH_URL' , 'https://auth.chalmers.it/oauth2/authorize' )
11- token_uri = os .getenv ('GAMMA_TOKEN_URL' , 'https://auth.chalmers.it/oauth2/token' )
12- user_info_uri = os .getenv ('GAMMA_USER_INFO_URL' , 'https://auth.chalmers.it/oauth2/userinfo' )
7+ client_id = os .getenv ('GAMMA_CLIENT_ID' , '' )
8+ client_secret = os .getenv ('GAMMA_CLIENT_SECRET' , '' )
9+ redirect_uri = os .getenv ('GAMMA_REDIRECT_URI' , 'http://localhost:5000/api/auth/callbacks/gamma' )
10+ auth_uri = os .getenv ('GAMMA_AUTH_URL' , 'https://auth.chalmers.it/oauth2/authorize' )
11+ token_uri = os .getenv ('GAMMA_TOKEN_URL' , 'https://auth.chalmers.it/oauth2/token' )
12+ user_info_uri = os .getenv ('GAMMA_USER_INFO_URL' , 'https://auth.chalmers.it/oauth2/userinfo' )
1313
1414auth = Blueprint ('auth' , __name__ )
1515
16+
1617@auth .route ('/login' )
1718def login ():
1819 return render_template ('login.html' )
1920
21+
2022@auth .route ('/authorize' )
2123def authorize ():
2224 # Generate and store state parameter for CSRF protection
2325 state = secrets .token_urlsafe (32 )
2426 session ['oauth2_state' ] = state
25-
2627 qs = {
2728 'response_type' : 'code' ,
2829 'client_id' : client_id ,
29- 'scope' : 'openid' , # profile
30+ 'scope' : 'openid' , # profile
3031 'redirect_uri' : redirect_uri ,
31- 'state' :state ,
32+ 'state' : state ,
3233 }
33-
3434 return redirect (f"{ auth_uri } ?{ urlencode (qs )} " )
3535
36+
3637@auth .route ('/api/auth/callbacks/gamma' )
3738def callback ():
3839 args_dict = dict (request .args )
3940 print (args_dict )
40-
41+
4142 if 'code' not in args_dict :
4243 return "Error: Missing authorization code parameter" , 400
43-
44+
4445 if 'state' not in args_dict :
4546 return "Error: Missing state parameter" , 400
46-
47+
4748 received_state = args_dict ['state' ]
4849 stored_state = session .get ('oauth2_state' )
49-
50+
5051 if not stored_state or received_state != stored_state :
5152 return "Error: Invalid state parameter" , 400
5253
5354 session .pop ('oauth2_state' , None )
54-
55+
5556 code = args_dict ['code' ]
56- return code
57+ return render_template ('profile.html' )
58+
5759
5860@auth .route ('/logout' )
5961def logout ():
60- return render_template ('logout.html' )
62+ return render_template ('logout.html' )
0 commit comments