Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make generated jwt attributes in a predictable order #1403

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.annotation.Nonnull;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.time.Instant;
Expand All @@ -42,8 +43,8 @@ public class JwtGenerator {
private static final String DEFAULT_JWKS_URL = "http://localhost/token_keys";
private static final char DOT = '.';

private final JSONObject jsonHeader = new JSONObject();
private final JSONObject jsonPayload = new JSONObject();
private final JSONObject jsonHeader = newPredictableOrderingJSONObject();
private final JSONObject jsonPayload = newPredictableOrderingJSONObject();

private final SignatureCalculator signatureCalculator;
private final Service service;
Expand All @@ -60,6 +61,23 @@ private JwtGenerator(Service service, SignatureCalculator signatureCalculator) {
predefineTokenClaims();
}

/**
* Creates a new JSONObject object with LinkedHashMap with predictable iteration order.
* @return JSONObject
*/
private static JSONObject newPredictableOrderingJSONObject() {
JSONObject jsonObject = new JSONObject();
try {
Field declaredMapField = jsonObject.getClass().getDeclaredField("map");
declaredMapField.setAccessible(true);
declaredMapField.set(jsonObject, new LinkedHashMap<>());
declaredMapField.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {
liga-oz marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.info("Couldn't create a JSONObject with a LinkedHashMap field. {}", e.getMessage());
}
return jsonObject;
}

/**
* This factory method creates an {@link JwtGenerator} instance that can be used
* to create tokens for testing purposes. The tokens are prefilled with data so
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Cloud Security Client Java contributors
*
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Cloud Security Client Java contributors*
* <p>
* SPDX-License-Identifier: Apache-2.0
*/
package com.sap.cloud.security.comp;
Expand Down Expand Up @@ -227,7 +227,7 @@ void getSubdomainFails() {
@Test
void getAppToken() {
token = XsuaaTokenComp.createInstance(jwtGenerator.createToken());
assertThat(token.getAppToken(), startsWith("eyJqa3UiOiJodHRwOi8vbG9jYWx"));
assertThat(token.getAppToken(), startsWith("eyJraWQiOiJkZWZhdWx0LWtpZCIs"));
}

@Test
Expand Down
Loading