Skip to content
Snippets Groups Projects
Commit 19d0f8c4 authored by srosse's avatar srosse
Browse files

Merge remote-tracking branch 'origin/OpenOLAT_14.2'

parents d1d62fc6 0917b1e9
No related branches found
No related tags found
No related merge requests found
...@@ -19,8 +19,10 @@ ...@@ -19,8 +19,10 @@
*/ */
package org.olat.login.oauth.spi; package org.olat.login.oauth.spi;
import org.apache.logging.log4j.Logger;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.olat.core.logging.Tracing;
import org.olat.core.util.StringHelper; import org.olat.core.util.StringHelper;
import com.github.scribejava.core.model.OAuth2AccessToken; import com.github.scribejava.core.model.OAuth2AccessToken;
...@@ -33,6 +35,8 @@ import com.github.scribejava.core.model.OAuth2AccessToken; ...@@ -33,6 +35,8 @@ import com.github.scribejava.core.model.OAuth2AccessToken;
*/ */
public class JSONWebToken { public class JSONWebToken {
private static final Logger log = Tracing.createLoggerFor(JSONWebToken.class);
private final String header; private final String header;
private final String payload; private final String payload;
private final JSONObject jsonPayload; private final JSONObject jsonPayload;
...@@ -56,24 +60,40 @@ public class JSONWebToken { ...@@ -56,24 +60,40 @@ public class JSONWebToken {
} }
public static JSONWebToken parse(OAuth2AccessToken token) throws JSONException { public static JSONWebToken parse(OAuth2AccessToken token) throws JSONException {
String accessToken= token.getAccessToken(); try {
String accessToken= token.getAccessToken();
int firstIndex = accessToken.indexOf('.');
int secondIndex = accessToken.indexOf('.', firstIndex + 1); int firstIndex = accessToken.indexOf('.');
int secondIndex = accessToken.indexOf('.', firstIndex + 1);
String header = StringHelper.decodeBase64(accessToken.substring(0, firstIndex));
String payload = StringHelper.decodeBase64(accessToken.substring(firstIndex, secondIndex)); String header = StringHelper.decodeBase64(accessToken.substring(0, firstIndex));
JSONObject jsonPayload = new JSONObject(payload); String payload = StringHelper.decodeBase64(accessToken.substring(firstIndex, secondIndex));
return new JSONWebToken(header, payload, jsonPayload); JSONObject jsonPayload = new JSONObject(payload);
return new JSONWebToken(header, payload, jsonPayload);
} catch (JSONException e) {
log.error("Cannot parse token: {}", token.getAccessToken());
throw e;
} catch (Exception e) {
log.error("Cannot parse token: {}", token.getAccessToken());
throw new JSONException(e);
}
} }
public static JSONWebToken parse(String accessToken) throws JSONException { public static JSONWebToken parse(String accessToken) throws JSONException {
int firstIndex = accessToken.indexOf('.'); try {
int secondIndex = accessToken.indexOf('.', firstIndex + 1); int firstIndex = accessToken.indexOf('.');
int secondIndex = accessToken.indexOf('.', firstIndex + 1);
String header = StringHelper.decodeBase64(accessToken.substring(0, firstIndex));
String payload = StringHelper.decodeBase64(accessToken.substring(firstIndex, secondIndex)); String header = StringHelper.decodeBase64(accessToken.substring(0, firstIndex));
JSONObject jsonPayload = new JSONObject(payload); String payload = StringHelper.decodeBase64(accessToken.substring(firstIndex, secondIndex));
return new JSONWebToken(header, payload, jsonPayload); JSONObject jsonPayload = new JSONObject(payload);
return new JSONWebToken(header, payload, jsonPayload);
} catch (JSONException e) {
log.error("Cannot parse token: {}", accessToken);
throw e;
} catch (Exception e) {
log.error("Cannot parse token: {}", accessToken);
throw new JSONException(e);
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment