Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
79cd5b0
Initial commit
karen-avetisyan-mc Sep 10, 2024
565f0dc
Initial Petstore
rossphelan Oct 24, 2024
9f91085
Merge pull request #1 from Mastercard/feature/intial-petstore
karen-avetisyan-mc Oct 24, 2024
7f8027b
removing extra file
karen-avetisyan-mc Oct 24, 2024
2c8f53a
Merge pull request #2 from Mastercard/fixes
karen-avetisyan-mc Oct 24, 2024
5b74f39
Fixing the docoumentation
karen-avetisyan-mc Oct 25, 2024
b4645ac
Updating javadoc
rossphelan Oct 30, 2024
b442f9c
Fixing the docoumentation
karen-avetisyan-mc Oct 25, 2024
ad19c24
Merge pull request #3 from Mastercard/fixes-documentation
rfeelin Oct 31, 2024
bb809b8
Create maven-test.yml
karen-avetisyan-mc Oct 31, 2024
85aa605
Merge branch 'main' of https://github.com/Mastercard/petstore-applica…
rossphelan Oct 31, 2024
3b1b8b2
Merge pull request #7 from Mastercard/karen-avetisyan-mc-patch-1
karen-avetisyan-mc Oct 31, 2024
81c6414
Create sonar.yml
karen-avetisyan-mc Oct 31, 2024
1fa7041
Merge branch 'main' of https://github.com/Mastercard/petstore-applica…
rossphelan Oct 31, 2024
aa5c054
Merge pull request #8 from Mastercard/karen-avetisyan-mc-patch-2
rfeelin Oct 31, 2024
d23123b
Updating Javadocs, updating petstore yaml
rossphelan Oct 31, 2024
1686722
Updating Javadocs, updating petstore yaml, clean up
rossphelan Oct 31, 2024
ccb08e6
Merge branch 'main' of https://github.com/Mastercard/petstore-applica…
rossphelan Oct 31, 2024
dda797a
Updating yaml, updating MockDataBuilders
rossphelan Oct 31, 2024
18468f3
Updating yaml, updating MockDataBuilders
rossphelan Oct 31, 2024
d3730a7
Fixing flow, fixing enc, updating documentation
rossphelan Nov 14, 2024
085834e
Update README.md
karen-avetisyan-mc Nov 18, 2024
7de3407
Merge pull request #9 from Mastercard/karen-avetisyan-mc-patch-1
NehaSony Nov 18, 2024
b30135e
updating yaml
rossphelan Nov 19, 2024
6b9a161
updating client encryption
karen-avetisyan-mc Nov 22, 2024
e07d01e
Merge pull request #10 from Mastercard/update-client-encryption
karen-avetisyan-mc Nov 22, 2024
d9bed5e
updates
rfeelin Nov 22, 2024
c65122e
Merge branch 'main' into feature/documentation-updates
rossphelan Nov 22, 2024
66fe2c9
Merge pull request #11 from Mastercard/feature/documentation-updates
rfeelin Nov 25, 2024
18efc6f
updating the petstore yaml
karen-avetisyan-mc Nov 26, 2024
8530950
Merge pull request #12 from Mastercard/error_codes_fixes
rfeelin Nov 26, 2024
1f44b7e
Updating yaml text and error codes. Removing unused field from pom
rfeelin Nov 27, 2024
3b87a63
Merge pull request #13 from Mastercard/feature/update-yaml-error-codes
rfeelin Nov 27, 2024
a90f30b
removing unused imports
karen-avetisyan-mc Dec 10, 2024
619c3e5
Fixing the flow
karen-avetisyan-mc Aug 18, 2025
9995e6c
Fixing the flow
karen-avetisyan-mc Aug 18, 2025
4fc6eac
Fixing the flow
karen-avetisyan-mc Aug 20, 2025
cc3be39
Fixing the flow
karen-avetisyan-mc Aug 20, 2025
ce62f4b
Fixing the flow
karen-avetisyan-mc Aug 26, 2025
a64017a
Fixing the flow
karen-avetisyan-mc Aug 26, 2025
7cb2bfc
Merge branch 'main' into oauth2-test
karen-avetisyan-mc Aug 26, 2025
e1d02dc
Fixing the flow
karen-avetisyan-mc Aug 26, 2025
fbadbd4
Fixing the flow
karen-avetisyan-mc Aug 27, 2025
ad9cbd7
Fixing the flow
karen-avetisyan-mc Sep 4, 2025
4ea3622
Fixing the flow
karen-avetisyan-mc Sep 4, 2025
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
<artifactId>oauth1-signer</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>com.mastercard.developer</groupId>
<artifactId>oauth2-signer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/oauth2-signer/oauth2-signer-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
Expand Down
134 changes: 134 additions & 0 deletions src/main/java/com/mastercard/app/petstore/utils/OAuth2Utils.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding unit tests to verify that the constructor throws exceptions for missing/empty values, ensuring default values are reflected, and the client can create the object correctly

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.mastercard.app.petstore.utils;

import com.mastercard.developer.oauth2.core.OAuth2Configuration;
import com.mastercard.developer.oauth2.interceptors.OkHttp3OAuth2Interceptor;
import com.mastercard.developer.utils.AuthenticationUtils;
import org.openapitools.client.ApiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;

import java.security.InvalidAlgorithmParameterException;
import java.security.PrivateKey;
import java.util.logging.Logger;

@Profile({"oauth2stage"})
@org.springframework.context.annotation.Configuration
public class OAuth2Utils {

private final String signingKeyContainer;
private final String signingKeyAlias;
private final String signingKeyPassword;
private final String basePath;
private final String tokenUrl;
private final String audience;
private final String keyId;
private final String clientId;
private final String scope;
private final String dpopKeyType;

@Autowired
public OAuth2Utils(
@Value("${mastercard.oauth2.pkcs12KeyFile}") String signingKeyContainer,
@Value("${mastercard.oauth2.keyAlias}") String signingKeyAlias,
@Value("${mastercard.oauth2.keyPassword}") String signingKeyPassword,
@Value("${mastercard.basePath}") String basePath,
@Value("${mastercard.oauth2.audience}") String audience,
@Value("${mastercard.oauth2.tokenUrl}") String tokentUri,
@Value("${mastercard.oauth2.keyId}") String keyId,
@Value("${mastercard.oauth2.clientId}") String clientId,
@Value("${mastercard.oauth2.scope}") String scope,
@Value("${mastercard.oauth2.dpopKeyType}") String dpopSigningAlgorithm) {

if (isNullOrEmpty(basePath)) {
throw new IllegalArgumentException("basePath in application.properties is empty");
}
this.basePath = basePath;
if (isNullOrEmpty(signingKeyContainer)) {
throw new IllegalArgumentException("pkcs12KeyFile in application-oauth2.properties is empty");
}
this.signingKeyContainer = signingKeyContainer;
if (isNullOrEmpty(clientId)) {
throw new IllegalArgumentException("client id in application-oauth2.properties is empty");
}
this.clientId = clientId;
if (isNullOrEmpty(signingKeyAlias)) {
throw new IllegalArgumentException("keyAlias in application-oauth2.properties is empty");
}
this.signingKeyAlias = signingKeyAlias;
if (isNullOrEmpty(signingKeyPassword)) {
throw new IllegalArgumentException("keyPassword in application-oauth2.properties is empty");
}
this.signingKeyPassword = signingKeyPassword;

if (isNullOrEmpty(tokentUri)) {
throw new IllegalArgumentException("tokentUri in application-oauth2.properties is empty");
}
this.tokenUrl = tokentUri;
if (isNullOrEmpty(audience)) {
throw new IllegalArgumentException("audience in application-oauth2.properties is empty");
}
this.audience = audience;

if (isNullOrEmpty(keyId)) {
throw new IllegalArgumentException("keyId in application-oauth2.properties is empty");
}

this.keyId = keyId;
if (isNullOrEmpty(scope)) {
throw new IllegalArgumentException("scope in application-oauth2.properties is empty");
}
this.scope = scope;
if (isNullOrEmpty(dpopSigningAlgorithm)) {
this.dpopKeyType = "ES256";
} else {
this.dpopKeyType = dpopSigningAlgorithm;
}
}

private boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
/**
* Sets an oAuth api client without encryption. This will be used to send authenticated requests to the server.
*
* @return the oAuth api client
*/
@Bean
public ApiClient apiClient() {
ApiClient client = newGenericClient();
try {
var config = OAuth2Configuration.productionConfigWithConsoleLogging();
client.setHttpClient(
client.getHttpClient()
.newBuilder()
.addInterceptor(
new OkHttp3OAuth2Interceptor(config, clientId, getSigningKey(), tokenUrl, keyId, scope))
.build()
);

} catch (InvalidAlgorithmParameterException e) {
System.out.println(e.getMessage());
}
return client;
}

private PrivateKey getSigningKey() {
PrivateKey signingKey = null;
try {
signingKey = AuthenticationUtils.loadSigningKey(signingKeyContainer, signingKeyAlias, signingKeyPassword);
} catch (Exception e) {
e.printStackTrace();
}
return signingKey;
}

private ApiClient newGenericClient() {
ApiClient client = new ApiClient();
client.setLenientOnJson(true);
client.setBasePath(basePath);
client.setDebugging(false);
return client;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application-oauth1.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#OAUTH
mastercard.oauth.pkcs12KeyFile=
mastercard.oauth.consumerKey=
mastercard.oauth.keyAlias=
mastercard.oauth.keyPassword=
13 changes: 13 additions & 0 deletions src/main/resources/application-oauth2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mastercard.basePath=https://api.mastercard.com/oauth-petstore/petstore
server.port=${port:8081}

mastercard.encryption.encryptionCert=
mastercard.encryption.decryptionKeys=
mastercard.encryption.decryptionKeyAlias=
mastercard.encryption.decryptionKeyPassword=
mastercard.encryption.encryptionCert=
mastercard.encryption.decryptionKeys=
mastercard.encryption.decryptionKeyAlias=
mastercard.encryption.decryptionKeyPassword=


Loading