@@ -81,16 +81,17 @@ public static void main (String... args) {
81
81
// Default is JSON, will be overridden by the few controllers that do not return JSON
82
82
res .type ("application/json" );
83
83
84
- // Log each API request
85
- LOG .info ("{} {}" , req .requestMethod (), req .pathInfo ());
86
-
87
- if (!AnalysisServerConfig .offline ) {
84
+ if (AnalysisServerConfig .auth0ClientId != null && AnalysisServerConfig .auth0Secret != null ) {
88
85
handleAuthentication (req , res );
89
86
} else {
87
+ LOG .warn ("No Auth0 credentials were supplied, setting accessGroup and email to placeholder defaults" );
90
88
// hardwire group name if we're working offline
91
89
req .attribute ("accessGroup" , "OFFLINE" );
92
90
req .
attribute (
"email" ,
"[email protected] " );
93
91
}
92
+
93
+ // Log each API request
94
+ LOG .info ("{} {} by {} of {}" , req .requestMethod (), req .pathInfo (), req .attribute ("email" ), req .attribute ("accessGroup" ));
94
95
});
95
96
96
97
// Register all our HTTP request handlers with the Spark HTTP framework.
@@ -123,23 +124,23 @@ public static void main (String... args) {
123
124
124
125
exception (AnalysisServerException .class , (e , request , response ) -> {
125
126
AnalysisServerException ase = ((AnalysisServerException ) e );
126
- AnalysisServer .respondToException (response , ase , ase .type .name (), ase .message , ase .httpCode );
127
+ AnalysisServer .respondToException (ase , request , response , ase .type .name (), ase .message , ase .httpCode );
127
128
});
128
129
129
130
exception (IOException .class , (e , request , response ) -> {
130
- AnalysisServer .respondToException (response , e , "BAD_REQUEST" , e .getMessage (), 400 );
131
+ AnalysisServer .respondToException (e , request , response , "BAD_REQUEST" , e .getMessage (), 400 );
131
132
});
132
133
133
134
exception (FileUploadException .class , (e , request , response ) -> {
134
- AnalysisServer .respondToException (response , e , "BAD_REQUEST" , e .getMessage (), 400 );
135
+ AnalysisServer .respondToException (e , request , response , "BAD_REQUEST" , e .getMessage (), 400 );
135
136
});
136
137
137
138
exception (NullPointerException .class , (e , request , response ) -> {
138
- AnalysisServer .respondToException (response , e , "UNKNOWN" , e .getMessage (), 400 );
139
+ AnalysisServer .respondToException (e , request , response , "UNKNOWN" , e .getMessage (), 400 );
139
140
});
140
141
141
142
exception (RuntimeException .class , (e , request , response ) -> {
142
- AnalysisServer .respondToException (response , e , "RUNTIME" , e .getMessage (), 400 );
143
+ AnalysisServer .respondToException (e , request , response , "RUNTIME" , e .getMessage (), 400 );
143
144
});
144
145
145
146
LOG .info ("Conveyal Analysis server is ready." );
@@ -150,14 +151,14 @@ public static void handleAuthentication (Request req, Response res) {
150
151
151
152
// authorization required
152
153
if (auth == null || auth .isEmpty ()) {
153
- AnalysisServerException .Unauthorized ("You must be logged in." );
154
+ throw AnalysisServerException .Unauthorized ("You must be logged in." );
154
155
}
155
156
156
157
// make sure it's properly formed
157
158
String [] authComponents = auth .split (" " );
158
159
159
160
if (authComponents .length != 2 || !"bearer" .equals (authComponents [0 ].toLowerCase ())) {
160
- AnalysisServerException .Unknown ("Authorization header is malformed: " + auth );
161
+ throw AnalysisServerException .Unknown ("Authorization header is malformed: " + auth );
161
162
}
162
163
163
164
// validate the JWT
@@ -167,33 +168,33 @@ public static void handleAuthentication (Request req, Response res) {
167
168
try {
168
169
jwt = verifier .verify (authComponents [1 ]);
169
170
} catch (Exception e ) {
170
- AnalysisServerException .Forbidden ("Login failed to verify with our authorization provider. " + e .getMessage ());
171
+ throw AnalysisServerException .Forbidden ("Login failed to verify with our authorization provider. " + e .getMessage ());
171
172
}
172
173
173
174
if (!jwt .containsKey ("analyst" )) {
174
- AnalysisServerException .Forbidden ("Access denied. User does not have access to Analysis." );
175
+ throw AnalysisServerException .Forbidden ("Access denied. User does not have access to Analysis." );
175
176
}
176
177
177
178
String group = null ;
178
179
try {
179
180
group = (String ) ((Map <String , Object >) jwt .get ("analyst" )).get ("group" );
180
181
} catch (Exception e ) {
181
- AnalysisServerException .Forbidden ("Access denied. User is not associated with any group. " + e .getMessage ());
182
+ throw AnalysisServerException .Forbidden ("Access denied. User is not associated with any group. " + e .getMessage ());
182
183
}
183
184
184
185
if (group == null ) {
185
- AnalysisServerException .Forbidden ("Access denied. User is not associated with any group." );
186
+ throw AnalysisServerException .Forbidden ("Access denied. User is not associated with any group." );
186
187
}
187
188
188
189
// attributes to be used on models
189
190
req .attribute ("accessGroup" , group );
190
191
req .attribute ("email" , jwt .get ("email" ));
191
192
}
192
193
193
- public static void respondToException (Response response , Exception e , String type , String message , int code ) {
194
+ public static void respondToException (Exception e , Request request , Response response , String type , String message , int code ) {
194
195
String stack = ExceptionUtils .getStackTrace (e );
195
196
196
- LOG .error ("Server exception thrown, type: {}, message: {}" , type , message );
197
+ LOG .error ("{} {} -> {} {} by {} of {} " , type , message , request . requestMethod (), request . pathInfo (), request . attribute ( "email" ), request . attribute ( "accessGroup" ) );
197
198
LOG .error (stack );
198
199
199
200
JSONObject body = new JSONObject ();
0 commit comments