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 (#355)

* Fixes issues while in Authorized version of Connected client versions
- AuthorizedConnectedAasRepository
- AuthorizedConnectedSubmodelRepository
- AuthorizedConnectedAasRegistry
- AuthorizedConnectedSubmodelRegistry

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Removes unnecessary files

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Resets commented out modules

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Fixes the old rbac rule targeInfo in CI

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Extracts repeated LOC

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

---------

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
  • Loading branch information
mdanish98 authored Jul 25, 2024
1 parent d9de413 commit 45afd4f
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

package org.eclipse.digitaltwin.basyx.aasregistry.main.client;

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

import org.eclipse.digitaltwin.basyx.aasregistry.client.api.RegistryAndDiscoveryInterfaceApi;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;

Expand All @@ -48,7 +45,7 @@ public class AuthorizedConnectedAasRegistry extends RegistryAndDiscoveryInterfac
* @param tokenManager
*/
public AuthorizedConnectedAasRegistry(String basePath, TokenManager tokenManager) {
super(basePath, getRequestBuilder(tokenManager));
super(basePath, tokenManager);
this.aasRegistryBasePath = basePath;
this.tokenManager = tokenManager;
}
Expand All @@ -61,13 +58,4 @@ public TokenManager getTokenManager() {
return 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");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import java.util.function.Consumer;

import java.util.concurrent.CompletableFuture;
{{/asyncNative}}
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
import org.eclipse.digitaltwin.basyx.core.exceptions.AccessTokenRetrievalException;

{{>generatedAnnotation}}
{{#operations}}
Expand All @@ -59,36 +61,33 @@ public class {{classname}} {
private final Duration memberVarReadTimeout;
private final {{#fullJavaUtil}}java.util.function.{{/fullJavaUtil}}Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
private final {{#fullJavaUtil}}java.util.function.{{/fullJavaUtil}}Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
private HttpRequest.Builder httpRequestBuilder;
private TokenManager tokenManager;

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

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

public {{classname}}(String protocol, String host, int port) {
this(protocol + "://" + host + ":" + port);
this.httpRequestBuilder = HttpRequest.newBuilder();
}

public {{classname}}(String protocol, String host, int port, HttpRequest.Builder httpRequestBuilder) {
public {{classname}}(String protocol, String host, int port, TokenManager tokenManager) {
this(protocol + "://" + host + ":" + port);
this.httpRequestBuilder = httpRequestBuilder;
this.tokenManager = tokenManager;
}

public {{classname}}(String basePath) {
this(withBaseUri(new ApiClient(), basePath));
this.httpRequestBuilder = HttpRequest.newBuilder();
}

public {{classname}}(String basePath, HttpRequest.Builder httpRequestBuilder) {
public {{classname}}(String basePath, TokenManager tokenManager) {
this(withBaseUri(new ApiClient(), basePath));
this.httpRequestBuilder = httpRequestBuilder;
this.tokenManager = tokenManager;
}

private static ApiClient withBaseUri(ApiClient client, String uri) {
Expand Down Expand Up @@ -380,7 +379,7 @@ public class {{classname}} {
{{#hasPathParams}}{{#pathParams}}{{#vendorExtensions.x-utf8-base64-url-encoded-as-string}}String {{{paramName}}}AsBase64EncodedParam = {{{paramName}}} == null ? null : new String(java.util.Base64.getUrlEncoder().encode({{paramName}}.getBytes(java.nio.charset.StandardCharsets.UTF_8)), java.nio.charset.StandardCharsets.UTF_8);
{{/vendorExtensions.x-utf8-base64-url-encoded-as-string}}{{/pathParams}}{{/hasPathParams}}

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

{{! Switch delimiters for baseName so we can write constants like "{query}" }}
String localVarPath = "{{{path}}}"{{#pathParams}}
Expand Down Expand Up @@ -509,6 +508,8 @@ public class {{classname}} {
localVarRequestBuilder.header("Content-Type", "{{#hasConsumes}}{{#consumes}}{{#-first}}{{mediaType}}{{/-first}}{{/consumes}}{{/hasConsumes}}{{#hasConsumes}}{{^consumes}}application/json{{/consumes}}{{/hasConsumes}}{{^hasConsumes}}application/json{{/hasConsumes}}");
{{/bodyParam}}
localVarRequestBuilder.header("Accept", "{{#hasProduces}}{{#produces}}{{mediaType}}{{^-last}}, {{/-last}}{{/produces}}{{/hasProduces}}{{#hasProduces}}{{^produces}}application/json{{/produces}}{{/hasProduces}}{{^hasProduces}}application/json{{/hasProduces}}");

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

{{#bodyParam}}
{{#isString}}
Expand Down Expand Up @@ -667,5 +668,16 @@ public class {{classname}} {
{{/hasParams}}
{{/vendorExtensions.x-group-parameters}}
{{/operation}}

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");
}
}
}
}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

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

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

import org.eclipse.digitaltwin.basyx.aasrepository.client.internal.AssetAdministrationShellRepositoryApi;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.aasservice.client.AuthorizedConnectedAasService;
Expand All @@ -44,7 +41,7 @@ public class AuthorizedConnectedAasRepository extends ConnectedAasRepository {
private TokenManager tokenManager;

public AuthorizedConnectedAasRepository(String repoUrl, TokenManager tokenManager) {
super(repoUrl, new AssetAdministrationShellRepositoryApi(repoUrl, getRequestBuilder(tokenManager)));
super(repoUrl, new AssetAdministrationShellRepositoryApi(repoUrl, tokenManager));
this.tokenManager = tokenManager;
}

Expand All @@ -62,13 +59,4 @@ public ConnectedAasService getConnectedAasService(String aasId) {
}
}

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");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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.pagination.Base64UrlEncodedCursorResult;

Expand All @@ -64,38 +66,36 @@ public class AssetAdministrationShellRepositoryApi {
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;

private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
private HttpRequest.Builder httpRequestBuilder;

private TokenManager tokenManager;
public AssetAdministrationShellRepositoryApi() {
this(new ApiClient());
this.httpRequestBuilder = HttpRequest.newBuilder();
}

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

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

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

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

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


public AssetAdministrationShellRepositoryApi(ApiClient apiClient) {
memberVarHttpClient = apiClient.getHttpClient();
memberVarObjectMapper = apiClient.getObjectMapper();
Expand Down Expand Up @@ -191,16 +191,19 @@ private HttpRequest.Builder deleteAssetAdministrationShellByIdRequestBuilder(Str
throw new ApiException(400, "Missing the required parameter 'aasIdentifier' when calling deleteAssetAdministrationShellById");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/shells/{aasIdentifier}"
.replace("{aasIdentifier}", ApiClient.urlEncode(aasIdentifier.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) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
Expand Down Expand Up @@ -298,8 +301,11 @@ private HttpRequest.Builder deleteSubmodelReferenceByIdAasRepositoryRequestBuild
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

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

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody());

if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
Expand Down Expand Up @@ -362,9 +368,9 @@ private ApiResponse<Base64UrlEncodedCursorResult<List<AssetAdministrationShell>>

private HttpRequest.Builder getAllAssetAdministrationShellsRequestBuilder(List<String> assetIds, String idShort, Integer limit, String cursor) throws ApiException {

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/shells";

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

List<Pair> localVarQueryParams = new ArrayList<>();
StringJoiner localVarQueryStringJoiner = new StringJoiner("&");
Expand All @@ -390,6 +396,8 @@ private HttpRequest.Builder getAllAssetAdministrationShellsRequestBuilder(List<S
}

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

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
if (memberVarReadTimeout != null) {
Expand Down Expand Up @@ -470,14 +478,16 @@ private HttpRequest.Builder getAssetAdministrationShellByIdRequestBuilder(String
throw new ApiException(400, "Missing the required parameter 'aasIdentifier' when calling getAssetAdministrationShellById");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/shells/{aasIdentifier}"
.replace("{aasIdentifier}", ApiClient.urlEncode(aasIdentifier.toString()));

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 @@ -565,6 +575,8 @@ private HttpRequest.Builder getAssetAdministrationShellByIdReferenceAasRepositor
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 @@ -652,6 +664,8 @@ private HttpRequest.Builder getAssetInformationAasRepositoryRequestBuilder(Strin
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 @@ -731,14 +745,16 @@ private HttpRequest.Builder postAssetAdministrationShellRequestBuilder(AssetAdmi
throw new ApiException(400, "Missing the required parameter 'assetAdministrationShell' when calling postAssetAdministrationShell");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/shells";

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(assetAdministrationShell);
Expand Down Expand Up @@ -839,6 +855,8 @@ private HttpRequest.Builder postSubmodelReferenceAasRepositoryRequestBuilder(Str

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

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

try {
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(reference);
Expand Down Expand Up @@ -934,15 +952,17 @@ private HttpRequest.Builder putAssetAdministrationShellByIdRequestBuilder(String
throw new ApiException(400, "Missing the required parameter 'assetAdministrationShell' when calling putAssetAdministrationShellById");
}

HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

String localVarPath = "/shells/{aasIdentifier}"
.replace("{aasIdentifier}", ApiClient.urlEncode(aasIdentifier.toString()));

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(assetAdministrationShell);
Expand Down Expand Up @@ -1046,6 +1066,8 @@ private HttpRequest.Builder putAssetInformationAasRepositoryRequestBuilder(Strin

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

addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder);

try {
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(assetInformation);
Expand All @@ -1061,4 +1083,16 @@ private HttpRequest.Builder putAssetInformationAasRepositoryRequestBuilder(Strin
}
return localVarRequestBuilder;
}
}

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 @@ -63,5 +63,11 @@
<artifactId>httpclient5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Loading

0 comments on commit 45afd4f

Please sign in to comment.