-
Notifications
You must be signed in to change notification settings - Fork 1
Oauth2 Support #21
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
Open
karen-avetisyan-mc
wants to merge
45
commits into
main
Choose a base branch
from
oauth2-test
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
Oauth2 Support #21
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 565f0dc
Initial Petstore
rossphelan 9f91085
Merge pull request #1 from Mastercard/feature/intial-petstore
karen-avetisyan-mc 7f8027b
removing extra file
karen-avetisyan-mc 2c8f53a
Merge pull request #2 from Mastercard/fixes
karen-avetisyan-mc 5b74f39
Fixing the docoumentation
karen-avetisyan-mc b4645ac
Updating javadoc
rossphelan b442f9c
Fixing the docoumentation
karen-avetisyan-mc ad19c24
Merge pull request #3 from Mastercard/fixes-documentation
rfeelin bb809b8
Create maven-test.yml
karen-avetisyan-mc 85aa605
Merge branch 'main' of https://github.com/Mastercard/petstore-applica…
rossphelan 3b1b8b2
Merge pull request #7 from Mastercard/karen-avetisyan-mc-patch-1
karen-avetisyan-mc 81c6414
Create sonar.yml
karen-avetisyan-mc 1fa7041
Merge branch 'main' of https://github.com/Mastercard/petstore-applica…
rossphelan aa5c054
Merge pull request #8 from Mastercard/karen-avetisyan-mc-patch-2
rfeelin d23123b
Updating Javadocs, updating petstore yaml
rossphelan 1686722
Updating Javadocs, updating petstore yaml, clean up
rossphelan ccb08e6
Merge branch 'main' of https://github.com/Mastercard/petstore-applica…
rossphelan dda797a
Updating yaml, updating MockDataBuilders
rossphelan 18468f3
Updating yaml, updating MockDataBuilders
rossphelan d3730a7
Fixing flow, fixing enc, updating documentation
rossphelan 085834e
Update README.md
karen-avetisyan-mc 7de3407
Merge pull request #9 from Mastercard/karen-avetisyan-mc-patch-1
NehaSony b30135e
updating yaml
rossphelan 6b9a161
updating client encryption
karen-avetisyan-mc e07d01e
Merge pull request #10 from Mastercard/update-client-encryption
karen-avetisyan-mc d9bed5e
updates
rfeelin c65122e
Merge branch 'main' into feature/documentation-updates
rossphelan 66fe2c9
Merge pull request #11 from Mastercard/feature/documentation-updates
rfeelin 18efc6f
updating the petstore yaml
karen-avetisyan-mc 8530950
Merge pull request #12 from Mastercard/error_codes_fixes
rfeelin 1f44b7e
Updating yaml text and error codes. Removing unused field from pom
rfeelin 3b87a63
Merge pull request #13 from Mastercard/feature/update-yaml-error-codes
rfeelin a90f30b
removing unused imports
karen-avetisyan-mc 619c3e5
Fixing the flow
karen-avetisyan-mc 9995e6c
Fixing the flow
karen-avetisyan-mc 4fc6eac
Fixing the flow
karen-avetisyan-mc cc3be39
Fixing the flow
karen-avetisyan-mc ce62f4b
Fixing the flow
karen-avetisyan-mc a64017a
Fixing the flow
karen-avetisyan-mc 7cb2bfc
Merge branch 'main' into oauth2-test
karen-avetisyan-mc e1d02dc
Fixing the flow
karen-avetisyan-mc fbadbd4
Fixing the flow
karen-avetisyan-mc ad9cbd7
Fixing the flow
karen-avetisyan-mc 4ea3622
Fixing the flow
karen-avetisyan-mc 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 hidden or 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
134 changes: 134 additions & 0 deletions
134
src/main/java/com/mastercard/app/petstore/utils/OAuth2Utils.java
This file contains hidden or 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 |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#OAUTH | ||
mastercard.oauth.pkcs12KeyFile= | ||
mastercard.oauth.consumerKey= | ||
mastercard.oauth.keyAlias= | ||
mastercard.oauth.keyPassword= |
This file contains hidden or 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 |
---|---|---|
@@ -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= | ||
|
||
|
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.
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