1- from init import app , auth , db
2- from models import Users
1+ from init import app , auth
2+ from models import User
33from flask_login import login_user , logout_user , LoginManager
44import flask
5+ import requests
6+ import sys
57
68from functools import wraps
79
@@ -19,25 +21,49 @@ def wrapped_function(*args, **kwargs):
1921 is_devcade_admin = "devcade" in groups
2022 auth_dict = {
2123 "uid" : uid ,
24+ "user_type" : "CSH" ,
2225 "first" : first ,
2326 "last" : last ,
27+ "email" : f"{ uid } @csh.rit.edu" ,
2428 "picture" : picture ,
25- "admin" : is_eboard or is_rtp or is_devcade_admin
29+ "admin" : any ((is_eboard , is_rtp , is_devcade_admin ))
30+ }
31+ kwargs ["auth_dict" ] = auth_dict
32+ return func (* args , ** kwargs )
33+ return wrapped_function
34+
35+ def google_user_auth (func ):
36+ @wraps (func )
37+ def wrapped_function (* args , ** kwargs ):
38+ uid = str (flask .session ["userinfo" ].get ("sub" , "" ))
39+ last = str (flask .session ["userinfo" ].get ("family_name" , "" ))
40+ first = str (flask .session ["userinfo" ].get ("given_name" , "" ))
41+ email = str (flask .session ["userinfo" ].get ("email" , "" ))
42+ picture = str (flask .session ["userinfo" ].get ("picture" , "" ))
43+ auth_dict = {
44+ "uid" : uid ,
45+ "user_type" : "GOOGLE" ,
46+ "first" : first ,
47+ "last" : last ,
48+ "email" : email ,
49+ "picture" : picture ,
50+ "admin" : False
2651 }
2752 kwargs ["auth_dict" ] = auth_dict
2853 return func (* args , ** kwargs )
2954 return wrapped_function
3055
3156login_manager = LoginManager ()
3257login_manager .init_app (app )
33- login_manager .login_view = 'csh_auth'
34-
58+ login_manager .login_view = 'homepage'
3559
3660@login_manager .user_loader
3761def load_user (user_id ):
38- q = Users .query .get (user_id )
39- if q :
40- return q
62+ user_req = requests .get (app .config ["DEVCADE_API_URI" ] + "users/" + user_id )
63+ if user_req .status_code == 200 :
64+ user_data = user_req .json ()
65+ user = User (user_data ['id' ], user_data ['user_type' ], user_data ['first_name' ], user_data ['last_name' ], user_data ['email' ], user_data ['picture' ], user_data ['admin' ])
66+ return user
4167 return None
4268
4369
@@ -49,7 +75,6 @@ def _logout():
4975
5076
5177@app .route ('/csh_auth' )
52- @app .route ('/login' )
5378@auth .oidc_auth ('default' )
5479@csh_user_auth
5580def csh_auth (auth_dict = None ):
@@ -58,18 +83,44 @@ def csh_auth(auth_dict=None):
5883 """
5984 if auth_dict is None :
6085 return flask .redirect ("/csh_auth" )
61- user = Users .query .get (auth_dict ['uid' ])
62- if user is not None :
63- user .firstname = auth_dict ['first' ]
64- user .lastname = auth_dict ['last' ]
65- user .picture = auth_dict ['picture' ]
66- user .admin = auth_dict ['admin' ]
86+ return update_backend_user (auth_dict )
87+
88+ @app .route ('/google_auth' )
89+ @auth .oidc_auth ('google' )
90+ @google_user_auth
91+ def google_auth (auth_dict = None ):
92+ """
93+ Gets new logger inner data
94+ """
95+ if auth_dict is None :
96+ return flask .redirect ("/google_auth" )
97+ return update_backend_user (auth_dict )
98+
99+
100+ def update_backend_user (auth_dict ):
101+ # headers={"frontend_api_key":app.config["FRONTEND_API_KEY"]}
102+ user_req = requests .get (app .config ["DEVCADE_API_URI" ] + "users/" + auth_dict ['uid' ])
103+ if user_req .status_code == 400 :
104+ requests .post (app .config ["DEVCADE_API_URI" ] + "users/" , json = {
105+ 'id' : auth_dict ['uid' ],
106+ 'user_type' : auth_dict ['user_type' ],
107+ 'first_name' : auth_dict ['first' ],
108+ 'last_name' : auth_dict ['last' ],
109+ 'picture' : auth_dict ['picture' ],
110+ 'email' : auth_dict ['email' ],
111+ 'admin' : auth_dict ['admin' ]
112+ }, headers = {"frontend_api_key" :app .config ["FRONTEND_API_KEY" ]})
67113 else :
68- user = Users (auth_dict ['uid' ], auth_dict ['first' ],
69- auth_dict ['last' ], auth_dict ['picture' ], auth_dict ['admin' ])
70- db .session .add (user )
71- db .session .commit ()
72- login_user (user )
114+ requests .put (app .config ["DEVCADE_API_URI" ] + "users/" + auth_dict ['uid' ], json = {
115+ 'id' : auth_dict ['uid' ],
116+ 'user_type' : auth_dict ['user_type' ],
117+ 'first_name' : auth_dict ['first' ],
118+ 'last_name' : auth_dict ['last' ],
119+ 'picture' : auth_dict ['picture' ],
120+ 'email' : auth_dict ['email' ],
121+ 'admin' : auth_dict ['admin' ]
122+ }, headers = {"frontend_api_key" :app .config ["FRONTEND_API_KEY" ]})
123+ login_user (User (auth_dict ['uid' ], auth_dict ['user_type' ], auth_dict ['first' ], auth_dict ['last' ], auth_dict ['email' ], auth_dict ['picture' ], auth_dict ['admin' ]))
73124 goto = flask .request .args .get ('goto' )
74125 if goto == None :
75126 goto = 'homepage'
@@ -78,7 +129,3 @@ def csh_auth(auth_dict=None):
78129 except :
79130 goto = flask .url_for ('homepage' )
80131 return flask .redirect (goto )
81-
82-
83- with app .app_context ():
84- db .create_all ()
0 commit comments