From 2e1b8b3f95b4a523ae1a5d3ce5d853e89bebed3d Mon Sep 17 00:00:00 2001 From: Djeumen Rolain Bonaventure Date: Thu, 7 Mar 2024 22:01:14 +0100 Subject: [PATCH] feat(jans-config-api): enhancements to saml api #7362 (#7989) * feat(jans-config-api): enhancements to saml api * added method to get the trustrelationship metadata as a stream Signed-off-by: Rolain Djeumen * feat(jans-config-api): enhancements to saml api * added method to SamlIdpService to get file from the DocumentStore Signed-off-by: Rolain Djeumen * feat(jans-config-api): enhancements to saml api #7362 * added endpoint to retrieve file metadata for trustrelationship Signed-off-by: Rolain Djeumen * feat(jans-config-api): enhancements to saml api #7362 * added newly generate swagger yaml for kc-saml-plugino Signed-off-by: Rolain Djeumen * feat(jans-config-api): enhancements to saml api #7362 * pluralized the operationid for the endpoint retrieving all trust relationships Signed-off-by: Rolain Djeumen --------- Signed-off-by: Rolain Djeumen --- .../plugins/docs/kc-saml-plugin-swagger.yaml | 43 ++++++++++++++++++- .../saml/rest/TrustRelationshipResource.java | 32 +++++++++++++- .../plugin/saml/service/SamlIdpService.java | 11 +++++ .../plugin/saml/service/SamlService.java | 6 +++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/jans-config-api/plugins/docs/kc-saml-plugin-swagger.yaml b/jans-config-api/plugins/docs/kc-saml-plugin-swagger.yaml index 188878ddb25..c6275308780 100644 --- a/jans-config-api/plugins/docs/kc-saml-plugin-swagger.yaml +++ b/jans-config-api/plugins/docs/kc-saml-plugin-swagger.yaml @@ -785,7 +785,7 @@ paths: - SAML - Trust Relationship summary: Get all Trust Relationship description: Get all TrustRelationship. - operationId: get-trust-relationship + operationId: get-trust-relationships responses: "200": description: Ok @@ -830,6 +830,47 @@ paths: security: - oauth2: - https://jans.io/oauth/config/saml.readonly + /kc/saml/trust-relationship/sp-metadata-file/{id}: + get: + tags: + - SAML - Trust Relationship + summary: Get TrustRelationship file metadata + description: Get TrustRelationship file metadata + operationId: get-trust-relationship-file-metadata + parameters: + - name: id + in: path + description: TrustRelationship inum + required: true + schema: + type: string + responses: + "200": + description: OK + content: + application/xml: + schema: + type: string + format: binary + "400": + description: Bad Request + content: + application/json: + schema: + $ref: '#/components/schemas/ApiError' + "401": + description: Unauthorized + "404": + description: Not Found + content: + application/json: + schema: + $ref: '#/components/schemas/ApiError' + "500": + description: Internal Server Error + security: + - oauth2: + - https://jans.io/oauth/config/saml.readonly /kc/saml/trust-relationship/process-sp-meta-file: post: tags: diff --git a/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/rest/TrustRelationshipResource.java b/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/rest/TrustRelationshipResource.java index 1d7ef8a397f..ca4b206142f 100644 --- a/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/rest/TrustRelationshipResource.java +++ b/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/rest/TrustRelationshipResource.java @@ -56,7 +56,7 @@ public class TrustRelationshipResource extends BaseResource { @Inject SamlService samlService; - @Operation(summary = "Get all Trust Relationship", description = "Get all TrustRelationship.", operationId = "get-trust-relationship", tags = { + @Operation(summary = "Get all Trust Relationship", description = "Get all TrustRelationship.", operationId = "get-trust-relationships", tags = { "SAML - Trust Relationship" }, security = @SecurityRequirement(name = "oauth2", scopes = { Constants.SAML_READ_ACCESS })) @ApiResponses(value = { @@ -240,6 +240,36 @@ public Response deleteTrustRelationship( return Response.noContent().build(); } + @Operation(summary="Get TrustRelationship file metadata", description="Get TrustRelationship file metadata", + operationId = "get-trust-relationship-file-metadata", tags = {"SAML - Trust Relationship"}, + security = @SecurityRequirement(name = "oauth2", scopes= {Constants.SAML_READ_ACCESS}), + responses = { + @ApiResponse(responseCode="200",description="OK",content= @Content(mediaType = MediaType.APPLICATION_XML,schema = @Schema(type="string",format="binary"))), + @ApiResponse(responseCode="400",description="Bad Request",content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ApiError.class, description = "BadRequestException"))), + @ApiResponse(responseCode="401",description="Unauthorized"), + @ApiResponse(responseCode="404",description="Not Found",content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ApiError.class, description = "NotFoundException"))), + @ApiResponse(responseCode="500",description="Internal Server Error") + } + ) + @Path(Constants.SP_METADATA_FILE_PATH+Constants.ID_PATH_PARAM) + @GET + @ProtectedApi(scopes = {Constants.SAML_READ_ACCESS}) + public Response gettrustRelationshipFileMetadata( + @Parameter(description="TrustRelationship inum") @PathParam(Constants.ID) @NotNull String id) { + + logger.info("getTrustRelationshipFileMeta(). ID: - {}",id); + TrustRelationship trustrelationship = samlService.getTrustRelationshipByInum(id); + checkResourceNotNull(trustrelationship,SAML_TRUST_RELATIONSHIP); + if(trustrelationship.getSpMetaDataSourceType() != MetadataSourceType.FILE) { + throwBadRequestException("TrustRelationship metadatasource type isn't a FILE"); + } + InputStream fs = samlService.getTrustRelationshipMetadataFile(trustrelationship); + if(fs == null) { + return getNotFoundError(String.format("metadata file for tr '%s' ",id)); + } + return Response.ok(fs,MediaType.APPLICATION_XML).build(); + } + @Operation(summary = "Process unprocessed metadata files", description = "Process unprocessed metadata files", operationId = "post-metadata-files", tags = { "SAML - Trust Relationship" }, security = @SecurityRequirement(name = "oauth2", scopes = { Constants.SAML_WRITE_ACCESS })) diff --git a/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlIdpService.java b/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlIdpService.java index afdd20a0da0..5b2c0e82287 100644 --- a/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlIdpService.java +++ b/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlIdpService.java @@ -131,6 +131,17 @@ public boolean renameMetadata(String metadataPath, String destinationMetadataPat return false; } + public InputStream getFileFromDocumentStore(String path) { + + logger.debug("Get file from DocumentStore. Path: {}",path); + try { + return documentStoreService.readDocumentAsStream(path); + }catch(Exception e) { + logger.error("Failed to get file '{}' from DocumentStore",path); + return null; + } + } + private String getTempMetadataFilename(String metadataFolder, String fileName) { logger.info("documentStoreService:{}, localDocumentStoreService:{}, metadataFolder:{}, fileName:{}", documentStoreService, localDocumentStoreService, metadataFolder, fileName); diff --git a/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlService.java b/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlService.java index 3336adb674a..8cc9b8f28c6 100644 --- a/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlService.java +++ b/jans-config-api/plugins/kc-saml-plugin/src/main/java/io/jans/configapi/plugin/saml/service/SamlService.java @@ -359,5 +359,11 @@ public void processUnprocessedSpMetadataFiles() { } } + + public InputStream getTrustRelationshipMetadataFile(TrustRelationship trustrelationship) { + + log.debug("Get trustrelationship metadata file"); + return samlIdpService.getFileFromDocumentStore(trustrelationship.getSpMetaDataFN()); + } }