Skip to content

Commit

Permalink
Fix JOSM #21127: NPE in Mapillary login
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <tsmock@fb.com>
  • Loading branch information
tsmock committed Jul 19, 2021
1 parent dfed86d commit 23e59a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public void run() {
final HttpClient.Response response = client.connect();
try (JsonReader jsonReader = Json.createReader(response.getContentReader())) {
final JsonObject jsonObject = jsonReader.readObject();
OAuthUtils.updateAuthorization(jsonObject);
if (!OAuthUtils.updateAuthorization(jsonObject)) {
Logging.info("Mapillary: Failed to login: {0}", jsonObject);
return;
}
}

String username = MapillaryUser.getUsername();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,25 @@ private static void refreshAuthorization(final String authorizationCode) {
* Update authorization information from a JSON object
*
* @param jsonObject The json object to use
* @return {@code true} if the update was successful
*/
static void updateAuthorization(final JsonObject jsonObject) {
static boolean updateAuthorization(final JsonObject jsonObject) {
// The actual access token we want to use
final String accessToken = jsonObject.getString("access_token");
final String accessToken = jsonObject.getString("access_token", null);
// This is in seconds
final int expiresIn = jsonObject.getInt("expires_in");
final int expiresIn = jsonObject.getInt("expires_in", Integer.MIN_VALUE);
// Will probably always be bearer
final String tokenType = jsonObject.getString("token_type");
final String tokenType = jsonObject.getString("token_type", null);
if (tokenType == null || accessToken == null || expiresIn == Integer.MIN_VALUE) {
MapillaryUser.reset();
GuiHelper.runInEDT(() -> {
Notification notification = new Notification();
notification.setContent(tr("Mapillary: Could not refresh login, logging out"));
notification.setIcon(JOptionPane.ERROR_MESSAGE);
notification.show();
});
return false;
}
if (!"bearer".equals(tokenType)) {
MapillaryUser.reset();
throw new JosmRuntimeException("Mapillary: Login failed due to unknown token type: " + tokenType);
Expand Down Expand Up @@ -229,5 +240,6 @@ static void updateAuthorization(final JsonObject jsonObject) {
Logging.info("Successful authentication with Mapillary, the access token is {0} expiring in {1} seconds",
accessToken, expiresIn);
// Saves the access token in preferences.
return true;
}
}

0 comments on commit 23e59a8

Please sign in to comment.