-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added supoort for oauth2 access token for service to service (#63)
- Loading branch information
Showing
12 changed files
with
363 additions
and
5 deletions.
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
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
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
88 changes: 88 additions & 0 deletions
88
...t-common/src/main/java/com/adobe/platform/streaming/auth/impl/OAuth2IMSTokenProvider.java
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.adobe.platform.streaming.auth.impl; | ||
|
||
import com.adobe.platform.streaming.auth.AbstractAuthProvider; | ||
import com.adobe.platform.streaming.auth.AuthException; | ||
import com.adobe.platform.streaming.auth.AuthUtils; | ||
import com.adobe.platform.streaming.auth.TokenResponse; | ||
import com.adobe.platform.streaming.http.HttpException; | ||
import com.adobe.platform.streaming.http.HttpProducer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
/** | ||
* @author Adobe Inc. | ||
*/ | ||
public class OAuth2IMSTokenProvider extends AbstractAuthProvider { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OAuth2IMSTokenProvider.class); | ||
private static final String IMS_ENDPOINT_PATH = "/ims/token/v3"; | ||
private static final String GRANT_TYPE = "client_credentials"; | ||
private static final String SCOPE = "openid,AdobeID,read_organizations,additional_info.projectedProductContext," + | ||
"session"; | ||
|
||
private String endpoint = System.getenv(AuthUtils.AUTH_ENDPOINT); | ||
|
||
private final String clientId; | ||
private final String clientSecret; | ||
private HttpProducer httpProducer; | ||
|
||
OAuth2IMSTokenProvider(final String clientId, final String clientSecret, | ||
final AuthProxyConfiguration authProxyConfiguration) { | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.httpProducer = HttpProducer.newBuilder(endpoint) | ||
.withProxyHost(authProxyConfiguration.getProxyHost()) | ||
.withProxyPort(authProxyConfiguration.getProxyPort()) | ||
.withProxyUser(authProxyConfiguration.getProxyUsername()) | ||
.withProxyPassword(authProxyConfiguration.getProxyPassword()) | ||
.build(); | ||
} | ||
|
||
OAuth2IMSTokenProvider(final String endpoint, final String clientId, final String clientSecret, | ||
final AuthProxyConfiguration authProxyConfiguration) { | ||
this(clientId, clientSecret, authProxyConfiguration); | ||
this.endpoint = endpoint; | ||
this.httpProducer = HttpProducer.newBuilder(endpoint) | ||
.withProxyHost(authProxyConfiguration.getProxyHost()) | ||
.withProxyPort(authProxyConfiguration.getProxyPort()) | ||
.withProxyUser(authProxyConfiguration.getProxyUsername()) | ||
.withProxyPassword(authProxyConfiguration.getProxyPassword()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
protected TokenResponse getTokenResponse() throws AuthException { | ||
LOG.debug("refreshing expired oauth2 accessToken: {}", clientId); | ||
StringBuilder params = new StringBuilder() | ||
.append("grant_type=").append(GRANT_TYPE) | ||
.append("&client_id=").append(clientId) | ||
.append("&client_secret=").append(clientSecret) | ||
.append("&scope=").append(SCOPE); | ||
|
||
try { | ||
final TokenResponse tokenResponse = httpProducer.post(IMS_ENDPOINT_PATH, params.toString().getBytes(), | ||
getContentHandler()); | ||
// As the expiresIn time we get from the API is in seconds we need to convert this into milliseconds | ||
final long expiresInMilliSecond = tokenResponse.getExpiresIn() * 1000; | ||
final TokenResponse updatedTokenResponse = new TokenResponse(tokenResponse.getTokenType(), expiresInMilliSecond, | ||
tokenResponse.getRefreshToken(), tokenResponse.getAccessToken()); | ||
return updatedTokenResponse; | ||
} catch (HttpException httpException) { | ||
throw new AuthException("Exception while fetching oauth2 access token", httpException); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...mmon/src/test/java/com/adobe/platform/streaming/auth/impl/OAuth2IMSTokenProviderTest.java
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.adobe.platform.streaming.auth.impl; | ||
|
||
import com.adobe.platform.streaming.auth.AuthException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* @author Adobe Inc. | ||
*/ | ||
class OAuth2IMSTokenProviderTest { | ||
|
||
private static final String TEST_ENDPOINT = "https://ims-na1.adobelogin.com"; | ||
private static final String TEST_CLIENT_ID = "testClientId"; | ||
private static final String TEST_CLIENT_SECRET = "testClientSecret"; | ||
|
||
@Test | ||
void testGetTokenInvalidClientId() { | ||
OAuth2IMSTokenProvider imsTokenProvider = new OAuth2IMSTokenProvider(TEST_ENDPOINT, null, | ||
TEST_CLIENT_SECRET, AuthProxyConfiguration.builder().build()); | ||
assertThrows(AuthException.class, imsTokenProvider::getToken); | ||
} | ||
|
||
@Test | ||
void testGetTokenInvalidSecret() { | ||
OAuth2IMSTokenProvider imsTokenProvider = new OAuth2IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, null, | ||
AuthProxyConfiguration.builder().build()); | ||
assertThrows(AuthException.class, imsTokenProvider::getToken); | ||
} | ||
|
||
@Test | ||
void testGetToken() { | ||
OAuth2IMSTokenProvider imsTokenProvider = new OAuth2IMSTokenProvider( | ||
TEST_ENDPOINT, | ||
TEST_CLIENT_ID, | ||
TEST_CLIENT_SECRET, | ||
AuthProxyConfiguration.builder().build() | ||
); | ||
assertThrows(AuthException.class, imsTokenProvider::getTokenResponse); | ||
} | ||
|
||
} |
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.