Skip to content

Commit

Permalink
Fixes token retrieval mechanism issue in Authorized version of Connec…
Browse files Browse the repository at this point in the history
…ted client versions in AAS/SM Service (eclipse-basyx#366)

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
  • Loading branch information
mdanish98 authored Jul 31, 2024
1 parent 5ffa72d commit 938397f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public ConnectedSubmodelService getSubmodelService(String identifier) {
public List<ConnectedSubmodelService> getAllSubmodels(String shellIdentifier) {
AssetAdministrationShell shell = getAasService(shellIdentifier).getAAS();
List<Reference> submodelReferences = shell.getSubmodels();
return submodelReferences.parallelStream()
return submodelReferences.stream()
.map(this::extractSubmodelIdentifierFromReference)
.map(this::getSubmodelService)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

package org.eclipse.digitaltwin.basyx.aasservice.client;

import java.io.IOException;
import java.net.http.HttpRequest;

import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApi;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;

Expand All @@ -46,16 +43,7 @@ public class AuthorizedConnectedAasService extends ConnectedAasService {
* @param tokenManager
*/
public AuthorizedConnectedAasService(String repoUrl, TokenManager tokenManager) {
super(new AssetAdministrationShellServiceApi(repoUrl, getRequestBuilder(tokenManager)));
}

private static HttpRequest.Builder getRequestBuilder(TokenManager tokenManager) {
try {
return HttpRequest.newBuilder()
.header("Authorization", "Bearer " + tokenManager.getAccessToken());
} catch (IOException e) {
throw new RuntimeException("Unable to request access token");
}
super(new AssetAdministrationShellServiceApi(repoUrl, tokenManager));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
import org.eclipse.digitaltwin.basyx.client.internal.ApiResponse;
import org.eclipse.digitaltwin.basyx.client.internal.Pair;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
import org.eclipse.digitaltwin.basyx.core.exceptions.AccessTokenRetrievalException;
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
import org.eclipse.digitaltwin.basyx.http.description.ServiceDescription;
import org.eclipse.digitaltwin.basyx.http.pagination.Base64UrlEncodedCursorResult;
Expand All @@ -73,36 +75,33 @@ public class AssetAdministrationShellServiceApi {
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
private final Duration memberVarReadTimeout;
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
private HttpRequest.Builder httpRequestBuilder;
private TokenManager tokenManager;

public AssetAdministrationShellServiceApi() {
this(new ApiClient());
this.httpRequestBuilder = HttpRequest.newBuilder();
}

public AssetAdministrationShellServiceApi(HttpRequest.Builder httpRequestBuilder) {
this();
this.httpRequestBuilder = httpRequestBuilder;
}
public AssetAdministrationShellServiceApi(TokenManager tokenManager) {
this(new ApiClient());
this.tokenManager = tokenManager;
}

public AssetAdministrationShellServiceApi(ObjectMapper mapper, String baseUri) {
this(new ApiClient(HttpClient.newBuilder(), mapper, baseUri));
this.httpRequestBuilder = HttpRequest.newBuilder();
}

public AssetAdministrationShellServiceApi(ObjectMapper mapper, String baseUri, HttpRequest.Builder httpRequestBuilder) {
this(mapper, baseUri);
this.httpRequestBuilder = httpRequestBuilder;
}
public AssetAdministrationShellServiceApi(ObjectMapper mapper, String baseUri, TokenManager tokenManager) {
this(new ApiClient(HttpClient.newBuilder(), mapper, baseUri));
this.tokenManager = tokenManager;
}

public AssetAdministrationShellServiceApi(String baseUri) {
this(new ApiClient(HttpClient.newBuilder(), new JsonMapperFactory().create(new SimpleAbstractTypeResolverFactory().create()), baseUri));
this.httpRequestBuilder = HttpRequest.newBuilder();
}

public AssetAdministrationShellServiceApi(String baseUri, HttpRequest.Builder httpRequestBuilder) {
this(baseUri);
this.httpRequestBuilder = httpRequestBuilder;
public AssetAdministrationShellServiceApi(String baseUri, TokenManager tokenManager) {
this(new ApiClient(HttpClient.newBuilder(), new JsonMapperFactory().create(new SimpleAbstractTypeResolverFactory().create()), baseUri));
this.tokenManager = tokenManager;
}


Expand Down Expand Up @@ -201,13 +200,15 @@ private HttpRequest.Builder deleteSubmodelReferenceByIdRequestBuilder(String sub
throw new ApiException(400, "Missing the required parameter 'submodelIdentifier' when calling deleteSubmodelReferenceById");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/submodel-refs/{submodelIdentifier}".replace("{submodelIdentifier}", ApiClient.urlEncode(submodelIdentifier.toString()));

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -284,13 +285,15 @@ public ApiResponse<Void> deleteThumbnailWithHttpInfoNoUrlEncoding() throws ApiEx

private HttpRequest.Builder deleteThumbnailRequestBuilder() throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/asset-information/thumbnail";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -348,9 +351,9 @@ private ApiResponse<Base64UrlEncodedCursorResult<List<Reference>>> getAllSubmode

private HttpRequest.Builder getAllSubmodelReferencesRequestBuilder(Integer limit, String cursor) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/submodel-refs";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

List<Pair> localVarQueryParams = new ArrayList<>();
StringJoiner localVarQueryStringJoiner = new StringJoiner("&");
Expand All @@ -369,6 +372,8 @@ private HttpRequest.Builder getAllSubmodelReferencesRequestBuilder(Integer limit
}

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -440,11 +445,13 @@ public ApiResponse<AssetAdministrationShell> getAssetAdministrationShellWithHttp

private HttpRequest.Builder getAssetAdministrationShellRequestBuilder() throws ApiException {

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri));

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -523,6 +530,8 @@ private HttpRequest.Builder getAssetAdministrationShellReferenceRequestBuilder()
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -594,13 +603,15 @@ public ApiResponse<AssetInformation> getAssetInformationWithHttpInfoNoUrlEncodin

private HttpRequest.Builder getAssetInformationRequestBuilder() throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/asset-information";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -679,6 +690,8 @@ private HttpRequest.Builder getDescriptionRequestBuilder() throws ApiException {
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -748,13 +761,15 @@ public ApiResponse<File> getThumbnailWithHttpInfoNoUrlEncoding() throws ApiExcep

private HttpRequest.Builder getThumbnailRequestBuilder() throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/asset-information/thumbnail";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Accept", "application/octet-stream, application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -833,14 +848,16 @@ private HttpRequest.Builder postSubmodelReferenceRequestBuilder(Reference refere
throw new ApiException(400, "Missing the required parameter 'reference' when calling postSubmodelReference");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/submodel-refs";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Content-Type", "application/json");
localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

try {
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(reference);
Expand Down Expand Up @@ -935,6 +952,8 @@ private HttpRequest.Builder putAssetAdministrationShellRequestBuilder(AssetAdmin

localVarRequestBuilder.header("Content-Type", "application/json");
localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

try {
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(assetAdministrationShell);
Expand Down Expand Up @@ -1021,14 +1040,16 @@ private HttpRequest.Builder putAssetInformationRequestBuilder(AssetInformation a
throw new ApiException(400, "Missing the required parameter 'assetInformation' when calling putAssetInformation");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/asset-information";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

localVarRequestBuilder.header("Content-Type", "application/json");
localVarRequestBuilder.header("Accept", "application/json");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

try {
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(assetInformation);
Expand Down Expand Up @@ -1087,11 +1108,8 @@ private ApiResponse<Void> putThumbnailWithHttpInfoNoUrlEncoding(String fileName,
}

private HttpRequest.Builder putThumbnailRequestBuilder(String fileName, ContentType contentType, InputStream inputStream) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
String localVarPath = "/asset-information/thumbnail";

HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy();

localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
localVarRequestBuilder.header("Accept", "application/json");

Expand All @@ -1118,6 +1136,8 @@ private HttpRequest.Builder putThumbnailRequestBuilder(String fileName, ContentT
HttpRequest.BodyPublisher formDataPublisher = HttpRequest.BodyPublishers.ofInputStream(() -> Channels.newInputStream(pipe.source()));

localVarRequestBuilder.header("Content-Type", entity.getContentType().getValue()).method("PUT", formDataPublisher);

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
Expand All @@ -1140,5 +1160,16 @@ private static String extractFileName(HttpHeaders headers) {
private static String buildUniqueFilename() {
return UUID.randomUUID().toString();
}

private void addAuthorizationHeaderIfAuthIsEnabled(HttpRequest.Builder localVarRequestBuilder) {
if (tokenManager != null) {
try {
localVarRequestBuilder.header("Authorization", "Bearer " + tokenManager.getAccessToken());
} catch (IOException e) {
e.printStackTrace();
throw new AccessTokenRetrievalException("Unable to request access token");
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

package org.eclipse.digitaltwin.basyx.submodelservice.client;

import java.io.IOException;
import java.net.http.HttpRequest;

import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
import org.eclipse.digitaltwin.basyx.submodelservice.client.internal.SubmodelServiceApi;

Expand All @@ -46,17 +43,7 @@ public class AuthorizedConnectedSubmodelService extends ConnectedSubmodelService
* @param tokenManager
*/
public AuthorizedConnectedSubmodelService(String repoUrl, TokenManager tokenManager) {
super(new SubmodelServiceApi(repoUrl, getRequestBuilder(tokenManager)));
}

private static HttpRequest.Builder getRequestBuilder(TokenManager tokenManager) {
try {
return HttpRequest.newBuilder()
.header("Authorization", "Bearer " + tokenManager.getAccessToken());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Unable to request access token");
}
super(new SubmodelServiceApi(repoUrl, tokenManager));
}

}
Loading

0 comments on commit 938397f

Please sign in to comment.