|
43 | 43 | import io.vavr.CheckedFunction0; |
44 | 44 | import io.vavr.control.Try; |
45 | 45 | import lombok.AccessLevel; |
| 46 | +import lombok.AllArgsConstructor; |
46 | 47 | import lombok.Getter; |
47 | 48 | import lombok.RequiredArgsConstructor; |
48 | 49 | import lombok.extern.slf4j.Slf4j; |
|
51 | 52 | * This interface handles the communication with an OAuth2 service. |
52 | 53 | */ |
53 | 54 | @RequiredArgsConstructor( access = AccessLevel.PACKAGE ) |
| 55 | +@AllArgsConstructor( access = AccessLevel.PRIVATE ) |
54 | 56 | @Slf4j |
55 | 57 | class OAuth2Service |
56 | 58 | { |
@@ -89,6 +91,8 @@ class OAuth2Service |
89 | 91 | @Nonnull |
90 | 92 | @Getter( AccessLevel.PACKAGE ) |
91 | 93 | private final ResilienceConfiguration resilienceConfiguration; |
| 94 | + @Nullable |
| 95 | + private ServiceIdentifier serviceIdentifier; |
92 | 96 |
|
93 | 97 | // package-private for testing |
94 | 98 | @Nonnull |
@@ -196,16 +200,25 @@ private OAuth2TokenResponse executeClientCredentialsFlow( @Nullable final Tenant |
196 | 200 |
|
197 | 201 | private TokenRequestFailedException buildException( @Nonnull final Throwable e, @Nullable final Tenant tenant ) |
198 | 202 | { |
199 | | - String msg = "Failed to resolve access token."; |
| 203 | + String message = "Failed to resolve access token."; |
200 | 204 | // In case where tenant is not the provider tenant, and we get 401 error, add hint to error message. |
201 | 205 | if( e instanceof OAuth2ServiceException |
202 | 206 | && ((OAuth2ServiceException) e).getHttpStatusCode().equals(401) |
203 | 207 | && tenant != null ) { |
204 | | - msg += |
205 | | - " In case you are accessing a multi-tenant BTP service, ensure that the service instance is declared as dependency " |
206 | | - + "to SaaS Provisioning Service or Subscription Manager (SMS) and subscribed for the current tenant."; |
| 208 | + String extension; |
| 209 | + if( serviceIdentifier != null ) { |
| 210 | + extension = |
| 211 | + " In case you are accessing a multi-tenant BTP service on behalf of a subscriber tenant, ensure that the service instance (here, of the " |
| 212 | + + serviceIdentifier |
| 213 | + + " service) is declared as dependency to SaaS Provisioning Service or Subscription Manager (SMS) and subscribed for the current tenant."; |
| 214 | + } else { |
| 215 | + extension = |
| 216 | + " In case you are accessing a multi-tenant BTP service on behalf of a subscriber tenant, ensure that the service instance" |
| 217 | + + " is declared as dependency to SaaS Provisioning Service or Subscription Manager (SMS) and subscribed for the current tenant."; |
| 218 | + } |
| 219 | + message += extension; |
207 | 220 | } |
208 | | - return new TokenRequestFailedException(msg, e); |
| 221 | + return new TokenRequestFailedException(message, e); |
209 | 222 | } |
210 | 223 |
|
211 | 224 | private void setAppTidInCaseOfIAS( @Nullable final String tenantId ) |
@@ -335,6 +348,7 @@ static class Builder |
335 | 348 | private TenantPropagationStrategy tenantPropagationStrategy = TenantPropagationStrategy.ZID_HEADER; |
336 | 349 | private final Map<String, String> additionalParameters = new HashMap<>(); |
337 | 350 | private ResilienceConfiguration.TimeLimiterConfiguration timeLimiter = OAuth2Options.DEFAULT_TIMEOUT; |
| 351 | + private ServiceIdentifier serviceIdentifier; |
338 | 352 |
|
339 | 353 | @Nonnull |
340 | 354 | Builder withTokenUri( @Nonnull final String tokenUri ) |
@@ -380,6 +394,7 @@ Builder withTenantPropagationStrategy( @Nonnull final TenantPropagationStrategy |
380 | 394 | @Nonnull |
381 | 395 | Builder withTenantPropagationStrategyFrom( @Nullable final ServiceIdentifier serviceIdentifier ) |
382 | 396 | { |
| 397 | + this.serviceIdentifier = serviceIdentifier; |
383 | 398 | final TenantPropagationStrategy tenantPropagationStrategy; |
384 | 399 | if( ServiceIdentifier.IDENTITY_AUTHENTICATION.equals(serviceIdentifier) ) { |
385 | 400 | tenantPropagationStrategy = TenantPropagationStrategy.TENANT_SUBDOMAIN; |
@@ -440,7 +455,8 @@ OAuth2Service build() |
440 | 455 | onBehalfOf, |
441 | 456 | tenantPropagationStrategy, |
442 | 457 | additionalParameters, |
443 | | - resilienceConfig); |
| 458 | + resilienceConfig, |
| 459 | + serviceIdentifier); |
444 | 460 | } |
445 | 461 | } |
446 | 462 |
|
|
0 commit comments