Skip to content

Commit 198486c

Browse files
authored
Merge pull request #45 from ComputerScienceHouse/dev
Dev
2 parents a83768e + 2382bca commit 198486c

File tree

6 files changed

+105
-50
lines changed

6 files changed

+105
-50
lines changed

src/auth.py

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from init import app, auth, db
2-
from models import Users
1+
from init import app, auth
2+
from models import User
33
from flask_login import login_user, logout_user, LoginManager
44
import flask
5+
import requests
6+
import sys
57

68
from 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

3156
login_manager = LoginManager()
3257
login_manager.init_app(app)
33-
login_manager.login_view = 'csh_auth'
34-
58+
login_manager.login_view = 'homepage'
3559

3660
@login_manager.user_loader
3761
def 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
5580
def 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()

src/config.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
SQLALCHEMY_DATABASE_URI = 'sqlite:///users.sqlite3'
2323
SQLALCHEMY_TRACK_MODIFICATIONS = 'False'
2424

25-
AWS_ACCESS_KEY_ID = env.get('AWS_ACCESS_KEY_ID', '')
26-
AWS_SECRET_ACCESS_KEY = env.get('AWS_SECRET_ACCESS_KEY', '')
27-
2825
# OpenID Connect SSO config CSH
2926
OIDC_ISSUER = env.get('OIDC_ISSUER', 'https://sso.csh.rit.edu/auth/realms/csh')
3027
OIDC_CLIENT_ID = env.get('OIDC_CLIENT_ID', 'devcade')
3128
OIDC_CLIENT_SECRET = env.get('OIDC_CLIENT_SECRET', 'NOT-A-SECRET')
3229

30+
GOOGLE_OIDC_ISSUER = env.get('GOOGLE_OIDC_ISSUER', 'https://sso.csh.rit.edu/auth/realms/csh')
31+
GOOGLE_OIDC_CLIENT_ID = env.get('GOOGLE_OIDC_CLIENT_ID', 'devcade')
32+
GOOGLE_OIDC_CLIENT_SECRET = env.get('GOOGLE_OIDC_CLIENT_SECRET', 'NOT-A-SECRET')
33+
3334
DEVCADE_API_URI = env.get('DEVCADE_API_URI')
3435
FRONTEND_API_KEY = env.get('FRONTEND_API_KEY')
3536

src/init.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@
2121
client_metadata=ClientMetadata(
2222
app.config["OIDC_CLIENT_ID"],
2323
app.config["OIDC_CLIENT_SECRET"]))
24-
auth = OIDCAuthentication({'default': CSH_AUTH},
25-
app)
24+
GOOGLE_AUTH = ProviderConfiguration(issuer=app.config["GOOGLE_OIDC_ISSUER"],
25+
client_metadata=ClientMetadata(
26+
app.config["GOOGLE_OIDC_CLIENT_ID"],
27+
app.config["GOOGLE_OIDC_CLIENT_SECRET"]),
28+
auth_request_params={'scope': ['email', 'profile', 'openid']})
29+
auth = OIDCAuthentication(
30+
{
31+
'default': CSH_AUTH,
32+
'google': GOOGLE_AUTH
33+
},
34+
app
35+
)
2636

2737
auth.init_app(app)
2838
app.secret_key = os.urandom(16)
29-
30-
# DB
31-
db = SQLAlchemy(app)
32-
migrate = Migrate(app, db)

src/models.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
from init import db
2-
3-
4-
class Users(db.Model):
5-
__tablename__ = 'Users'
6-
7-
id = db.Column(db.String, primary_key=True)
8-
firstname = db.Column(db.String, nullable=False)
9-
lastname = db.Column(db.String, nullable=False)
10-
picture = db.Column(db.String, nullable=False)
11-
admin = db.Column(db.Boolean, nullable=False)
12-
13-
def __init__(self, uid, firstname, lastname, picture, admin):
1+
class User:
2+
def __init__(self, uid, user_type, firstname, lastname, email, picture, admin):
143
self.id = uid
4+
self.user_type = user_type
155
self.firstname = firstname
166
self.lastname = lastname
7+
self.email = email
178
self.picture = picture
189
self.admin = admin
1910

@@ -22,9 +13,12 @@ def __repr__(self):
2213

2314
def to_json(self):
2415
return {"uid": self.uid,
16+
"user_type": self.user_type,
2517
"first": self.firstname,
2618
"last": self.lastname,
27-
"picture": self.picture}
19+
"email": self.email,
20+
"picture": self.picture,
21+
"admin": self.admin}
2822

2923
def get_id(self):
3024
return self.id

src/templates/catalog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
block content %}
33
<div class="card-wrapper">
44
{% for i in range(0,gamelist|length) %}
5-
{% if gamelist[i].id != "31cb96a8-b061-4381-a433-dd78550d4b3c" %}
5+
{% if "CSH Only" not in gamelist[i].tags|map(attribute="name") or current_user.user_type == "CSH" %}
66
{{ gamecard(gamelist[i]) }}
77
{% endif %}
88
{% endfor %}

src/templates/header.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
<div class="game-name">
66
<div>
77
<h2>{{ game.name }}</h2>
8+
{% if game.user.user_type == "CSH" %}
89
<h3>{{ game.author }}</h3>
10+
{% else %}
11+
<h3>{{ game.user.email.split('@')[0] }}</h3>
12+
{% endif %}
913
</div>
1014
</div>
1115
<div class="game-desc"><div>{{ game.description }}</div></div>
@@ -95,8 +99,11 @@ <h1>Devcade</h1>
9599
</div>
96100
<div class="dropdown-menu dropped hidden">
97101
<!-- <div class="dropdown-divider"></div> -->
98-
<a class="dropdown-item" href="/login?goto={{ request.endpoint }}"
99-
>Log in</a
102+
<a class="dropdown-item" href="/csh_auth?goto={{ request.endpoint }}"
103+
>Log In With CSH</a
104+
>
105+
<a class="dropdown-item" href="/google_auth?goto={{ request.endpoint }}"
106+
>Log In With RIT</a
100107
>
101108
</div>
102109
</div>

0 commit comments

Comments
 (0)