This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from conveyal/dev
4.0.1 Release
- Loading branch information
Showing
17 changed files
with
132 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,16 +81,17 @@ public static void main (String... args) { | |
// Default is JSON, will be overridden by the few controllers that do not return JSON | ||
res.type("application/json"); | ||
|
||
// Log each API request | ||
LOG.info("{} {}", req.requestMethod(), req.pathInfo()); | ||
|
||
if (!AnalysisServerConfig.offline) { | ||
if (AnalysisServerConfig.auth0ClientId != null && AnalysisServerConfig.auth0Secret != null) { | ||
handleAuthentication(req, res); | ||
} else { | ||
LOG.warn("No Auth0 credentials were supplied, setting accessGroup and email to placeholder defaults"); | ||
// hardwire group name if we're working offline | ||
req.attribute("accessGroup", "OFFLINE"); | ||
req.attribute("email", "[email protected]"); | ||
} | ||
|
||
// Log each API request | ||
LOG.info("{} {} by {} of {}", req.requestMethod(), req.pathInfo(), req.attribute("email"), req.attribute("accessGroup")); | ||
}); | ||
|
||
// Register all our HTTP request handlers with the Spark HTTP framework. | ||
|
@@ -123,23 +124,23 @@ public static void main (String... args) { | |
|
||
exception(AnalysisServerException.class, (e, request, response) -> { | ||
AnalysisServerException ase = ((AnalysisServerException) e); | ||
AnalysisServer.respondToException(response, ase, ase.type.name(), ase.message, ase.httpCode); | ||
AnalysisServer.respondToException(ase, request, response, ase.type.name(), ase.message, ase.httpCode); | ||
}); | ||
|
||
exception(IOException.class, (e, request, response) -> { | ||
AnalysisServer.respondToException(response, e, "BAD_REQUEST", e.getMessage(), 400); | ||
AnalysisServer.respondToException(e, request, response, "BAD_REQUEST", e.getMessage(), 400); | ||
}); | ||
|
||
exception(FileUploadException.class, (e, request, response) -> { | ||
AnalysisServer.respondToException(response, e, "BAD_REQUEST", e.getMessage(), 400); | ||
AnalysisServer.respondToException(e, request, response, "BAD_REQUEST", e.getMessage(), 400); | ||
}); | ||
|
||
exception(NullPointerException.class, (e, request, response) -> { | ||
AnalysisServer.respondToException(response, e, "UNKNOWN", e.getMessage(), 400); | ||
AnalysisServer.respondToException(e, request, response, "UNKNOWN", e.getMessage(), 400); | ||
}); | ||
|
||
exception(RuntimeException.class, (e, request, response) -> { | ||
AnalysisServer.respondToException(response, e, "RUNTIME", e.getMessage(), 400); | ||
AnalysisServer.respondToException(e, request, response, "RUNTIME", e.getMessage(), 400); | ||
}); | ||
|
||
LOG.info("Conveyal Analysis server is ready."); | ||
|
@@ -150,14 +151,14 @@ public static void handleAuthentication (Request req, Response res) { | |
|
||
// authorization required | ||
if (auth == null || auth.isEmpty()) { | ||
AnalysisServerException.Unauthorized("You must be logged in."); | ||
throw AnalysisServerException.Unauthorized("You must be logged in."); | ||
} | ||
|
||
// make sure it's properly formed | ||
String[] authComponents = auth.split(" "); | ||
|
||
if (authComponents.length != 2 || !"bearer".equals(authComponents[0].toLowerCase())) { | ||
AnalysisServerException.Unknown("Authorization header is malformed: " + auth); | ||
throw AnalysisServerException.Unknown("Authorization header is malformed: " + auth); | ||
} | ||
|
||
// validate the JWT | ||
|
@@ -167,33 +168,33 @@ public static void handleAuthentication (Request req, Response res) { | |
try { | ||
jwt = verifier.verify(authComponents[1]); | ||
} catch (Exception e) { | ||
AnalysisServerException.Forbidden("Login failed to verify with our authorization provider. " + e.getMessage()); | ||
throw AnalysisServerException.Forbidden("Login failed to verify with our authorization provider. " + e.getMessage()); | ||
} | ||
|
||
if (!jwt.containsKey("analyst")) { | ||
AnalysisServerException.Forbidden("Access denied. User does not have access to Analysis."); | ||
throw AnalysisServerException.Forbidden("Access denied. User does not have access to Analysis."); | ||
} | ||
|
||
String group = null; | ||
try { | ||
group = (String) ((Map<String, Object>) jwt.get("analyst")).get("group"); | ||
} catch (Exception e) { | ||
AnalysisServerException.Forbidden("Access denied. User is not associated with any group. " + e.getMessage()); | ||
throw AnalysisServerException.Forbidden("Access denied. User is not associated with any group. " + e.getMessage()); | ||
} | ||
|
||
if (group == null) { | ||
AnalysisServerException.Forbidden("Access denied. User is not associated with any group."); | ||
throw AnalysisServerException.Forbidden("Access denied. User is not associated with any group."); | ||
} | ||
|
||
// attributes to be used on models | ||
req.attribute("accessGroup", group); | ||
req.attribute("email", jwt.get("email")); | ||
} | ||
|
||
public static void respondToException(Response response, Exception e, String type, String message, int code) { | ||
public static void respondToException(Exception e, Request request, Response response, String type, String message, int code) { | ||
String stack = ExceptionUtils.getStackTrace(e); | ||
|
||
LOG.error("Server exception thrown, type: {}, message: {}", type, message); | ||
LOG.error("{} {} -> {} {} by {} of {}", type, message, request.requestMethod(), request.pathInfo(), request.attribute("email"), request.attribute("accessGroup")); | ||
LOG.error(stack); | ||
|
||
JSONObject body = new JSONObject(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.