Skip to content
Open
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
15 changes: 15 additions & 0 deletions .github/check-sandbox-credentials.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail

if [[ "${GITHUB_EVENT_NAME:-}" != "push" || "${GITHUB_REF:-}" != "refs/heads/main" ]]; then
exit 0
fi

[[ "${CLIENT_ID:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: CLIENT_ID"; exit 1; }
[[ "${KID:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: KID"; exit 1; }
[[ "${PRIVATE_KEY:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: PRIVATE_KEY"; exit 1; }
[[ "${TOKEN_ENDPOINT:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: TOKEN_ENDPOINT"; exit 1; }
[[ "${ISSUER:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: ISSUER"; exit 1; }
[[ "${API_BASE_URL:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: API_BASE_URL"; exit 1; }
[[ "${READ_SCOPES:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: READ_SCOPES"; exit 1; }
[[ "${WRITE_SCOPES:-}" =~ [^[:space:]] ]] || { echo "::error::Missing sandbox credential: WRITE_SCOPES"; exit 1; }
3 changes: 2 additions & 1 deletion .github/workflows/ci-apache-httpclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -41,7 +42,7 @@ jobs:
${{ runner.os }}-maven-

- name: Build and run tests with Apache HttpClient ${{ matrix.httpclient-version }}
run: mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.apache.OAuth2HttpClientTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dhttpclient.version=${{ matrix.httpclient-version }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
run: bash .github/check-sandbox-credentials.sh && mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.apache.OAuth2HttpClientTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dhttpclient.version=${{ matrix.httpclient-version }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-feign.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -41,7 +42,7 @@ jobs:
${{ runner.os }}-maven-

- name: Build and run tests with Feign ${{ matrix.feign-version }}
run: mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.feign.OAuth2ClientTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dfeign.version=${{ matrix.feign-version }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
run: bash .github/check-sandbox-credentials.sh && mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.feign.OAuth2ClientTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dfeign.version=${{ matrix.feign-version }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-java-http-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -40,7 +41,7 @@ jobs:
${{ runner.os }}-maven-

- name: Build and run tests with Java ${{ matrix.java-version }}
run: mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.java.OAuth2HttpClientTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
run: bash .github/check-sandbox-credentials.sh && mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.java.OAuth2HttpClientTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-java-lts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -34,7 +35,7 @@ jobs:
${{ runner.os }}-maven-

- name: Build and run tests
run: mvn -B test
run: bash .github/check-sandbox-credentials.sh && mvn -B test
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-okhttp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -41,7 +42,7 @@ jobs:
${{ runner.os }}-maven-

- name: Build and run tests with OkHttp ${{ matrix.okhttp-version }}
run: mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.okhttp3.OAuth2InterceptorTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dokhttp.version=${{ matrix.okhttp-version }}" "-Dmaven.test.dependency.excludes=${{ env.FEIGN }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
run: bash .github/check-sandbox-credentials.sh && mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.okhttp3.OAuth2InterceptorTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dokhttp.version=${{ matrix.okhttp-version }}" "-Dmaven.test.dependency.excludes=${{ env.FEIGN }},${{ env.SPRING_WEB }},${{ env.SPRING_WEBFLUX }}"
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-spring-restclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -41,7 +42,7 @@ jobs:
${{ runner.os }}-maven-

- name: Build and run tests with spring-web ${{ matrix.spring-web-version }}
run: mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.spring.restclient.OAuth2ClientHttpRequestInterceptorTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dspring-web.version=${{ matrix.spring-web-version }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }},${{ env.SPRING_WEBFLUX }}"
run: bash .github/check-sandbox-credentials.sh && mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.spring.restclient.OAuth2ClientHttpRequestInterceptorTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dspring-web.version=${{ matrix.spring-web-version }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }},${{ env.SPRING_WEBFLUX }}"
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci-spring-webclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
build:
environment: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'sandbox' || 'ci' }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -51,7 +52,7 @@ jobs:
fi

- name: Build and run tests with spring-webflux ${{ matrix.spring-webflux-version }}
run: mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.spring.webclient.OAuth2FilterTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dspring-webflux.version=${{ matrix.spring-webflux-version }}" "-Dspring-web.version=${{ matrix.spring-webflux-version }}" "-Duse-jakarta-ee=${{ steps.set-jakarta.outputs.use-jakarta-ee }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }}"
run: bash .github/check-sandbox-credentials.sh && mvn -B test "-Dtest=com.mastercard.developer.oauth2.http.spring.webclient.OAuth2FilterTest" "-Dsurefire.failIfNoSpecifiedTests=false" "-Dspring-webflux.version=${{ matrix.spring-webflux-version }}" "-Dspring-web.version=${{ matrix.spring-webflux-version }}" "-Duse-jakarta-ee=${{ steps.set-jakarta.outputs.use-jakarta-ee }}" "-Dmaven.test.dependency.excludes=${{ env.OKHTTP }},${{ env.FEIGN }}"
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
KID: ${{ secrets.KID }}
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,10 @@ Running integration tests against a real Mastercard API requires API details and

#### GitHub Actions

This repository is configured with the following secrets GitHub Actions automatically uses when running [workflows](https://github.com/mastercard/oauth2-client-java/actions):
- Branches and pull requests run Java tests against fake servers, without sandbox credentials.
- `main` also runs live tests. If a required credential is missing, the job fails.

<picture>
<source media="(prefers-color-scheme: dark)" srcset="./res/secrets-dark.svg">
<img src="./res/secrets-light.svg" alt="Configured secrets">
</picture>
**Maintainers:** Keep sandbox credentials only in a `main`-restricted environment, not in repository or organization secrets accessible to this repo.

### Fake Servers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private <Request, Response> AccessTokenResult<Response> retrieveAccessToken(
Optional<AccessToken> existingToken = tokenStore.get(filter);
if (existingToken.isPresent()) {
AccessToken accessToken = existingToken.get();
logger.debug("Valid access token found: {}", accessToken);
logger.debug("Valid access token found (expires at {}, {} scopes)", accessToken.expiresAt(), accessToken.scopes().size());
return AccessTokenResult.from(accessToken);
}

Expand Down Expand Up @@ -140,7 +140,7 @@ private <Request, Response> AccessTokenResult<Response> retrieveAccessToken(
adapter.close(tokenResponse);
AccessTokenResponse accessTokenResponse = parseAccessTokenJson(body);
var accessToken = new AccessToken(config.getClientId(), accessTokenResponse.scopes(), accessTokenResponse.expiry(), jkt, accessTokenResponse.tokenValue());
logger.debug("Adding access token to store: {}", accessToken);
logger.debug("Adding access token to store (expires at {}, {} scopes)", accessToken.expiresAt(), accessToken.scopes().size());
tokenStore.put(accessToken);
return AccessTokenResult.from(accessToken);
}
Expand All @@ -153,13 +153,13 @@ private <Request, Response> Response makeAccessTokenRequest(Request originalRequ
logger.debug("Creating token request DPoP proof (nonce: {})", logNonce(nonce));
}
String dpopProof = createTokenRequestDPoP(config, dpopKeyId, nonce);
logger.debug("Token request DPoP proof: {}", dpopProof);
logger.debug("Token request DPoP proof: <redacted>");
var scope = String.join(" ", scopes);
logger.debug("Creating client assertion");
String clientAssertion = createClientAssertion(config);
logger.debug("Client assertion: {}", clientAssertion);
logger.debug("Client assertion: <redacted>");
String formBody = createAccessTokenRequestBody(clientId, scope, clientAssertion);
logger.debug("Sending access token request with body: {}", formBody);
logger.debug("Sending access token request with body: <redacted>");
var headers = new HttpHeaders()
.add(USER_AGENT, config.getUserAgent())
.add(CONTENT_TYPE, "application/x-www-form-urlencoded")
Expand Down Expand Up @@ -201,7 +201,7 @@ private <Request, Response> Response makeResourceRequest(Request request, HttpAd
logger.debug("Creating resource request DPoP proof (nonce: {})", logNonce(nonce));
}
String dpopProof = createResourceRequestDPoP(config, dpopKeyId, method, resourceUrl, accessToken, nonce);
logger.debug("Resource request DPoP proof: {}", dpopProof);
logger.debug("Resource request DPoP proof: <redacted>");
var headers = new HttpHeaders()
.add(USER_AGENT, config.getUserAgent())
.add(AUTHORIZATION, "DPoP " + accessToken)
Expand Down Expand Up @@ -450,7 +450,7 @@ private static boolean isSuccess(int statusCode) {
}

private static String logBody(String body) {
return body == null || body.isEmpty() ? "<none>" : body;
return body == null || body.isEmpty() ? "<none>" : "<redacted>";
}

private static String logNonce(String nonce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ void setUp() {
httpClient = HttpClient.newBuilder().build();
}

@BeforeAll
static void beforeAll() {
System.setProperty("jdk.httpclient.HttpClient.log", "all");
}

@AfterAll
static void afterAll() {
System.clearProperty("jdk.httpclient.HttpClient.log");
}

@ParameterizedTest
@MethodSource("serverAndKeyProvider")
void callSequence_ShouldSucceed(Supplier<TestConfig> configSupplier) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void execute_ShouldReturnErrorResponse_WhenAuthorizationServerError() throws Exc
verify(adapter).sendAccessTokenRequest(eq(requestObject), any(URL.class), anyString(), any(HttpHeaders.class));
verify(adapter, never()).sendResourceRequest(any(), any());
assertEquals(
"Access token request failed (HTTP 500), body: {\"error\":\"internal_error\"}",
"Access token request failed (HTTP 500), body: <redacted>",
listAppender.list
.stream()
.filter(e -> e.getLevel() == Level.ERROR)
Expand All @@ -177,7 +177,7 @@ void execute_ShouldReturnErrorResponse_WhenResourceServerError() throws Exceptio
verify(adapter, times(2)).sendAccessTokenRequest(eq(requestObject), any(URL.class), anyString(), any(HttpHeaders.class));
verify(adapter).sendResourceRequest(eq(requestObject), any(HttpHeaders.class));
assertEquals(
"API call failed (HTTP 500), body: {\"error\":\"internal_error\"}",
"API call failed (HTTP 500), body: <redacted>",
listAppender.list
.stream()
.filter(e -> e.getLevel() == Level.ERROR)
Expand All @@ -196,12 +196,6 @@ void execute_ShouldExecuteEntireFlow() throws Exception {

// THEN
assertEquals(responseObject, response);
assertNotNull(
listAppender.list
.stream()
.filter(e -> e.getLevel() == Level.DEBUG && "API call successful (HTTP 201), body: {\"success\":\"true\"}".equals(e.getFormattedMessage()))
.findFirst()
);

// Verify authorization server was called, request headers and DPoP nonces
var tokenRequestBodyCaptor = ArgumentCaptor.forClass(String.class);
Expand All @@ -225,6 +219,11 @@ void execute_ShouldExecuteEntireFlow() throws Exception {
var uniqueAssertions = new HashSet<>(clientAssertions);
assertEquals(clientAssertions.size(), uniqueAssertions.size());

String loggedMessages = listAppender.list.stream().map(ILoggingEvent::getFormattedMessage).collect(java.util.stream.Collectors.joining("\n"));
assertFalse(loggedMessages.contains(sampleAccessToken));
assertFalse(loggedMessages.contains(tokenRequestHeaders.get(0).get(DPOP.value()).get()));
assertFalse(loggedMessages.contains(clientAssertions.get(0)));

// Verify resource server was called, request headers and DPoP nonces
var resourceRequestHeadersCaptor = ArgumentCaptor.forClass(HttpHeaders.class);
verify(adapter, times(2)).sendResourceRequest(eq(requestObject), resourceRequestHeadersCaptor.capture());
Expand Down
Loading