1
+ # pylint: disable=wrong-import-order
2
+ from ._version import __version__
3
+
1
4
import os
2
- import subprocess
3
5
from datetime import datetime
4
- from flask import Flask , redirect , request , render_template , g
5
- from flask_sqlalchemy import SQLAlchemy
6
- from flask_migrate import Migrate
6
+
7
7
from csh_ldap import CSHLDAP
8
- from raven import fetch_git_sha
8
+ from flask import Flask , redirect , render_template , g
9
+ from flask_migrate import Migrate
10
+ from flask_pyoidc .flask_pyoidc import OIDCAuthentication
11
+ from flask_sqlalchemy import SQLAlchemy
9
12
from raven .contrib .flask import Sentry
10
- from raven .exceptions import InvalidGitRepository
11
13
import structlog
12
14
15
+ from conditional import config
16
+
13
17
app = Flask (__name__ )
14
18
15
- config = os .path .join (app .config .get ('ROOT_DIR' , os .getcwd ()), "config.py" )
19
+ app .config .from_object (config )
20
+ if os .path .exists (os .path .join (os .getcwd (), "config.py" )):
21
+ app .config .from_pyfile (os .path .join (os .getcwd (), "config.py" ))
16
22
17
- app .config .from_pyfile (config )
18
23
app .config ["SQLALCHEMY_TRACK_MODIFICATIONS" ] = False
19
24
20
- app .config ["GIT_REVISION" ] = subprocess .check_output (['git' ,
21
- 'rev-parse' ,
22
- '--short' ,
23
- 'HEAD' ]).decode ('utf-8' ).rstrip ()
24
-
25
+ app .config ["VERSION" ] = __version__
25
26
26
27
db = SQLAlchemy (app )
27
28
migrate = Migrate (app , db )
31
32
app .config ['LDAP_BIND_PW' ],
32
33
ro = app .config ['LDAP_RO' ])
33
34
35
+ auth = OIDCAuthentication (app , issuer = app .config ["OIDC_ISSUER" ],
36
+ client_registration_info = app .config ["OIDC_CLIENT_CONFIG" ])
37
+
38
+ app .secret_key = app .config ["SECRET_KEY" ]
39
+
40
+
34
41
def start_of_year ():
35
42
start = datetime (datetime .today ().year , 6 , 1 )
36
43
if datetime .today () < start :
37
- start = datetime (datetime .today ().year - 1 , 6 , 1 )
44
+ start = datetime (datetime .today ().year - 1 , 6 , 1 )
38
45
return start
39
46
47
+
40
48
# pylint: disable=C0413
41
- from conditional .models .models import UserLog
49
+ from .models .models import UserLog
50
+
42
51
43
52
# Configure Logging
44
- def request_processor (logger , log_method , event_dict ): # pylint: disable=unused-argument, redefined-outer-name
53
+ def request_processor (logger , log_method , event_dict ): # pylint: disable=unused-argument, redefined-outer-name
45
54
if 'request' in event_dict :
46
55
flask_request = event_dict ['request' ]
47
- event_dict ['user' ] = flask_request .headers .get ("x-webauth-user" )
48
56
event_dict ['ip' ] = flask_request .remote_addr
49
57
event_dict ['method' ] = flask_request .method
50
58
event_dict ['blueprint' ] = flask_request .blueprint
51
59
event_dict ['path' ] = flask_request .full_path
60
+ if 'auth_dict' in event_dict :
61
+ auth_dict = event_dict ['auth_dict' ]
62
+ event_dict ['user' ] = auth_dict ['username' ]
52
63
return event_dict
53
64
54
65
55
- def database_processor (logger , log_method , event_dict ): # pylint: disable=unused-argument, redefined-outer-name
66
+ def database_processor (logger , log_method , event_dict ): # pylint: disable=unused-argument, redefined-outer-name
56
67
if 'request' in event_dict :
57
68
if event_dict ['method' ] != 'GET' :
58
69
log = UserLog (
@@ -62,35 +73,37 @@ def database_processor(logger, log_method, event_dict): # pylint: disable=unused
62
73
blueprint = event_dict ['blueprint' ],
63
74
path = event_dict ['path' ],
64
75
description = event_dict ['event' ]
65
- )
76
+ )
66
77
db .session .add (log )
67
78
db .session .flush ()
68
79
db .session .commit ()
69
80
del event_dict ['request' ]
70
81
return event_dict
71
82
83
+
72
84
structlog .configure (processors = [
73
85
request_processor ,
74
86
database_processor ,
75
87
structlog .processors .KeyValueRenderer ()
76
- ])
88
+ ])
77
89
78
90
logger = structlog .get_logger ()
79
91
80
-
81
- from conditional .blueprints .dashboard import dashboard_bp # pylint: disable=ungrouped-imports
82
- from conditional .blueprints .attendance import attendance_bp
83
- from conditional .blueprints .major_project_submission import major_project_bp
84
- from conditional .blueprints .intro_evals import intro_evals_bp
85
- from conditional .blueprints .intro_evals_form import intro_evals_form_bp
86
- from conditional .blueprints .housing import housing_bp
87
- from conditional .blueprints .spring_evals import spring_evals_bp
88
- from conditional .blueprints .conditional import conditionals_bp
89
- from conditional .blueprints .member_management import member_management_bp
90
- from conditional .blueprints .slideshow import slideshow_bp
91
- from conditional .blueprints .cache_management import cache_bp
92
- from conditional .blueprints .co_op import co_op_bp
93
- from conditional .blueprints .logs import log_bp
92
+ from conditional .util .auth import get_user
93
+
94
+ from .blueprints .dashboard import dashboard_bp # pylint: disable=ungrouped-imports
95
+ from .blueprints .attendance import attendance_bp
96
+ from .blueprints .major_project_submission import major_project_bp
97
+ from .blueprints .intro_evals import intro_evals_bp
98
+ from .blueprints .intro_evals_form import intro_evals_form_bp
99
+ from .blueprints .housing import housing_bp
100
+ from .blueprints .spring_evals import spring_evals_bp
101
+ from .blueprints .conditional import conditionals_bp
102
+ from .blueprints .member_management import member_management_bp
103
+ from .blueprints .slideshow import slideshow_bp
104
+ from .blueprints .cache_management import cache_bp
105
+ from .blueprints .co_op import co_op_bp
106
+ from .blueprints .logs import log_bp
94
107
95
108
app .register_blueprint (dashboard_bp )
96
109
app .register_blueprint (attendance_bp )
@@ -106,7 +119,8 @@ def database_processor(logger, log_method, event_dict): # pylint: disable=unused
106
119
app .register_blueprint (co_op_bp )
107
120
app .register_blueprint (log_bp )
108
121
109
- from conditional .util .ldap import ldap_get_member
122
+ from .util .ldap import ldap_get_member
123
+
110
124
111
125
@app .route ('/<path:path>' )
112
126
def static_proxy (path ):
@@ -115,20 +129,28 @@ def static_proxy(path):
115
129
116
130
117
131
@app .route ('/' )
132
+ @auth .oidc_auth
118
133
def default_route ():
119
134
return redirect ('/dashboard' )
120
135
136
+
137
+ @app .route ("/logout" )
138
+ @auth .oidc_logout
139
+ def logout ():
140
+ return redirect ("/" , 302 )
141
+
142
+
121
143
@app .errorhandler (404 )
122
144
@app .errorhandler (500 )
123
- def route_errors (error ):
145
+ @auth .oidc_auth
146
+ @get_user
147
+ def route_errors (error , user_dict = None ):
124
148
data = dict ()
125
- username = request .headers .get ('x-webauth-user' )
126
149
127
150
# Handle the case where the header isn't present
128
- if username is not None :
129
- member = ldap_get_member (username )
130
- data ['username' ] = member .uid
131
- data ['name' ] = member .cn
151
+ if user_dict ['username' ] is not None :
152
+ data ['username' ] = user_dict ['account' ].uid
153
+ data ['name' ] = user_dict ['account' ].cn
132
154
else :
133
155
data ['username' ] = "unknown"
134
156
data ['name' ] = "Unknown"
@@ -149,15 +171,17 @@ def route_errors(error):
149
171
error_desc = type (error ).__name__
150
172
151
173
return render_template ('errors.html' ,
152
- error = error_desc ,
153
- error_code = code ,
154
- event_id = g .sentry_event_id ,
155
- public_dsn = sentry .client .get_public_dsn ('https' ),
156
- ** data ), int (code )
174
+ error = error_desc ,
175
+ error_code = code ,
176
+ event_id = g .sentry_event_id ,
177
+ public_dsn = sentry .client .get_public_dsn ('https' ),
178
+ ** data ), int (code )
179
+
157
180
158
181
@app .cli .command ()
159
182
def zoo ():
160
183
from conditional .models .migrate import free_the_zoo
161
184
free_the_zoo (app .config ['ZOO_DATABASE_URI' ])
162
185
186
+
163
187
logger .info ('conditional started' )
0 commit comments