-
Notifications
You must be signed in to change notification settings - Fork 135
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
Adapt single tenant token keys requests #1407
Open
liga-oz
wants to merge
7
commits into
main
Choose a base branch
from
adapt_single_tenant_token_keys_requests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd602b5
adapt token keys parameter composer
liga-oz 4c94f7e
adapt token keys parameter composer for spring-xsuaa
liga-oz 41585b1
Merge remote-tracking branch 'origin/main' into adapt_single_tenant_t…
liga-oz f52e768
migrate springrunner to junit5
liga-oz 99ba65f
Merge branch 'main' into adapt_single_tenant_token_keys_requests
liga-oz c762e98
adapt tokenservice mock with the clientid
liga-oz fab3e7f
Merge branch 'main' into adapt_single_tenant_token_keys_requests
liga-oz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,13 +76,13 @@ private PublicKey fetchPublicKey(Token token, JwtSignatureAlgorithm algorithm) t | |
throw new IllegalArgumentException("Token does not contain the mandatory " + KID_PARAMETER_NAME + " header."); | ||
} | ||
|
||
String zidQueryParam = composeZidQueryParameter(token); | ||
String queryParameters = composeQueryParameters(token); | ||
|
||
String jwksUri; | ||
if (jkuFactories.isEmpty()) { | ||
jwksUri = configuration.isLegacyMode() | ||
? configuration.getUrl() + "/token_keys" | ||
: configuration.getProperty(UAA_DOMAIN) + "/token_keys" + zidQueryParam; | ||
: configuration.getProperty(UAA_DOMAIN) + "/token_keys" + queryParameters; | ||
} else { | ||
LOGGER.info("Loaded custom JKU factory"); | ||
jwksUri = jkuFactories.get(0).create(token.getTokenValue()); | ||
|
@@ -94,11 +94,21 @@ private PublicKey fetchPublicKey(Token token, JwtSignatureAlgorithm algorithm) t | |
return tokenKeyService.getPublicKey(algorithm, keyId, uri, params); | ||
} | ||
|
||
private String composeZidQueryParameter(Token token) { | ||
String composeQueryParameters(Token token) { | ||
String zid = token.getAppTid(); | ||
String clientId = token.getClientId(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be changed to configuration.getClientId() |
||
String query = ""; | ||
if (zid != null && !zid.isBlank()){ | ||
return "?zid=" + zid; | ||
query = "?zid=" + zid; | ||
} | ||
return ""; | ||
if (clientId != null && !clientId.isBlank()){ | ||
if (query.isBlank()){ | ||
query = "?client_id=" + clientId; | ||
} else { | ||
query = query + "&client_id=" + clientId; | ||
} | ||
} | ||
LOGGER.debug("Composed query parameter for token keys: {}", query); | ||
return query; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,8 +14,10 @@ | |
import com.sap.cloud.security.xsuaa.client.OidcConfigurationService; | ||
import com.sap.cloud.security.xsuaa.http.HttpHeaders; | ||
import org.apache.commons.io.IOUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
|
@@ -27,18 +29,18 @@ | |
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class XsuaaJwtSignatureValidatorTest { | ||
private Token xsuaaToken; | ||
private Token xsuaaTokenSignedWithVerificationKey; // signed with verificationkey (from configuration) | ||
private static Token xsuaaToken; | ||
private static Token xsuaaTokenSignedWithVerificationKey; // signed with verificationkey (from configuration) | ||
|
||
private JwtSignatureValidator cut; | ||
private OAuth2TokenKeyService tokenKeyServiceMock; | ||
private OAuth2ServiceConfiguration mockConfiguration; | ||
private static XsuaaJwtSignatureValidator cut; | ||
private static OAuth2ServiceConfiguration mockConfiguration; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
@BeforeAll | ||
public static void setup() throws IOException { | ||
/** | ||
* Header -------- { "alg": "RS256", "jku": | ||
* "https://authentication.stagingaws.hanavlab.ondemand.com/token_keys", "kid": | ||
|
@@ -57,9 +59,9 @@ public void setup() throws IOException { | |
when(mockConfiguration.getService()).thenReturn(Service.XSUAA); | ||
when(mockConfiguration.getProperty(UAA_DOMAIN)).thenReturn("authentication.stagingaws.hanavlab.ondemand.com"); | ||
|
||
tokenKeyServiceMock = Mockito.mock(OAuth2TokenKeyService.class); | ||
OAuth2TokenKeyService tokenKeyServiceMock = Mockito.mock(OAuth2TokenKeyService.class); | ||
when(tokenKeyServiceMock | ||
.retrieveTokenKeys(URI.create("https://authentication.stagingaws.hanavlab.ondemand.com/token_keys?zid=uaa"), | ||
.retrieveTokenKeys(URI.create("https://authentication.stagingaws.hanavlab.ondemand.com/token_keys?zid=uaa&client_id=sap_osb"), | ||
Map.of(HttpHeaders.X_ZID, "uaa"))) | ||
.thenReturn(IOUtils.resourceToString("/jsonWebTokenKeys.json", UTF_8)); | ||
|
||
|
@@ -71,12 +73,12 @@ public void setup() throws IOException { | |
} | ||
|
||
@Test | ||
public void xsuaa_RSASignatureMatchesJWKS() { | ||
void xsuaa_RSASignatureMatchesJWKS() { | ||
assertThat(cut.validate(xsuaaToken).isValid(), is(true)); | ||
} | ||
|
||
@Test | ||
public void generatedToken_SignatureMatchesVerificationkey() { | ||
void generatedToken_SignatureMatchesVerificationkey() { | ||
when(mockConfiguration.hasProperty("verificationkey")).thenReturn(true); | ||
when(mockConfiguration.getProperty("verificationkey")).thenReturn( | ||
""" | ||
|
@@ -93,7 +95,7 @@ public void generatedToken_SignatureMatchesVerificationkey() { | |
} | ||
|
||
@Test | ||
public void validationFails_whenVerificationkeyIsInvalid() { | ||
void validationFails_whenVerificationkeyIsInvalid() { | ||
when(mockConfiguration.hasProperty("verificationkey")).thenReturn(true); | ||
when(mockConfiguration.getProperty("verificationkey")).thenReturn("INVALIDKEY"); | ||
|
||
|
@@ -103,15 +105,32 @@ public void validationFails_whenVerificationkeyIsInvalid() { | |
} | ||
|
||
@Test | ||
public void validationFails_whenSignatureOfGeneratedTokenDoesNotMatchVerificationkey() { | ||
void validationFails_whenSignatureOfGeneratedTokenDoesNotMatchVerificationkey() { | ||
when(mockConfiguration.hasProperty("verificationkey")).thenReturn(true); | ||
when(mockConfiguration.getProperty("verificationkey")).thenReturn( | ||
"-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm1QaZzMjtEfHdimrHP3/2Yr+1z685eiOUlwybRVG9i8wsgOUh+PUGuQL8hgulLZWXU5MbwBLTECAEMQbcRTNVTolkq4i67EP6JesHJIFADbK1Ni0KuMcPuiyOLvDKiDEMnYG1XP3X3WCNfsCVT9YoU+lWIrZr/ZsIvQri8jczr4RkynbTBsPaAOygPUlipqDrpadMO1momNCbea/o6GPn38LxEw609ItfgDGhL6f/yVid5pFzZQWb+9l6mCuJww0hnhO6gt6Rv98OWDty9G0frWAPyEfuIW9B+mR/3vGhyU9IbbWpvFXiy9RVbbsM538TCjd5JF2dJvxy24addC4oQIDAQAB-----END PUBLIC KEY-----"); | ||
|
||
ValidationResult result = cut.validate(xsuaaTokenSignedWithVerificationKey); | ||
assertThat(result.isErroneous(), is(true)); | ||
assertThat(result.getErrorDescription(), containsString("Signature of Jwt Token is not valid")); | ||
assertThat(result.getErrorDescription(), containsString("(Signature: CetA62rQSNRj93S9mqaHrKJyzONKeEKcEJ9O5wObRD_")); | ||
assertThat(result.getErrorDescription(), | ||
containsString("(Signature: CetA62rQSNRj93S9mqaHrKJyzONKeEKcEJ9O5wObRD_")); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"cid, tid, ?zid=tid&client_id=cid", | ||
", tid, ?zid=tid", | ||
"' ', tid, ?zid=tid", | ||
"cid, , ?client_id=cid", | ||
"cid, ' ', ?client_id=cid", | ||
", , ''", | ||
"' ', ' ', ''" | ||
}) | ||
void composeQueryParams(String clientId, String appTid, String query) { | ||
Token tokenMock = Mockito.mock(Token.class); | ||
Mockito.when(tokenMock.getClientId()).thenReturn(clientId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this must be changed to Mockito.when(mockConfiguration.getClientId()).thenReturn(clientId); |
||
Mockito.when(tokenMock.getAppTid()).thenReturn(appTid); | ||
assertTrue(cut.composeQueryParameters(tokenMock).endsWith(query)); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about one of the following alternatives that, albeit not shorter, are probably more expressive / easier to maintain? Too much overhead?