-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(download): add credential download endpoint
Refs: #24
- Loading branch information
Showing
12 changed files
with
422 additions
and
113 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
63 changes: 63 additions & 0 deletions
63
src/issuer/SsiCredentialIssuer.Service/BusinessLogic/CredentialBusinessLogic.cs
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,63 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://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 CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.DBAccess; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.DBAccess.Repositories; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.Service.ErrorHandling; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.Service.Identity; | ||
using System.Text.Json; | ||
using System.Text.Unicode; | ||
|
||
namespace Org.Eclipse.TractusX.SsiCredentialIssuer.Service.BusinessLogic; | ||
|
||
public class CredentialBusinessLogic : ICredentialBusinessLogic | ||
{ | ||
private readonly IIssuerRepositories _repositories; | ||
private readonly IIdentityData _identityData; | ||
|
||
public CredentialBusinessLogic(IIssuerRepositories repositories, IIdentityService identityService) | ||
{ | ||
_repositories = repositories; | ||
_identityData = identityService.IdentityData; | ||
} | ||
|
||
public async Task<JsonDocument> GetCredentialDocument(Guid credentialId) | ||
{ | ||
var (exists, isSameCompany, documents) = await _repositories.GetInstance<ICredentialRepository>().GetSignedCredentialForCredentialId(credentialId, _identityData.Bpnl).ConfigureAwait(false); | ||
if (!exists) | ||
{ | ||
throw NotFoundException.Create(CredentialErrors.CREDENTIAL_NOT_FOUND, new[] { new ErrorParameter("credentialId", credentialId.ToString()) }); | ||
} | ||
|
||
if (!isSameCompany) | ||
{ | ||
throw ForbiddenException.Create(CredentialErrors.COMPANY_NOT_ALLOWED); | ||
} | ||
|
||
if (documents.Count() != 1) | ||
{ | ||
throw ConflictException.Create(CredentialErrors.SIGNED_CREDENTIAL_NOT_FOUND); | ||
} | ||
|
||
var credentialContent = documents.Single(); | ||
var content = System.Text.Encoding.UTF8.GetString(credentialContent.Content); | ||
return JsonDocument.Parse(content); | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/issuer/SsiCredentialIssuer.Service/Controllers/CredentialController.cs
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,50 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://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 CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Web; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.Service.BusinessLogic; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.Service.Extensions; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.Service.Identity; | ||
using Org.Eclipse.TractusX.SsiCredentialIssuer.Service.Models; | ||
using System.Text.Json; | ||
|
||
namespace Org.Eclipse.TractusX.SsiCredentialIssuer.Service.Controllers; | ||
|
||
public static class CredentialController | ||
{ | ||
public static RouteGroupBuilder MapCredentialApi(this RouteGroupBuilder group) | ||
{ | ||
var issuer = group.MapGroup("/credential"); | ||
|
||
issuer.MapGet("{credentialId}", ([FromRoute] Guid credentialId, [FromServices] ICredentialBusinessLogic logic) => logic.GetCredentialDocument(credentialId)) | ||
.WithSwaggerDescription("The endpoint enables users to download the credential (full json) of their own company.", | ||
"Example: GET: api/credential/{credentialId}") | ||
.RequireAuthorization(r => | ||
{ | ||
r.RequireRole("view_credential"); | ||
r.AddRequirements(new MandatoryIdentityClaimRequirement(PolicyTypeId.ValidBpn)); | ||
}) | ||
.WithDefaultResponses() | ||
.Produces(StatusCodes.Status200OK, typeof(JsonDocument), Constants.JsonContentType) | ||
.Produces(StatusCodes.Status409Conflict, typeof(ErrorResponse), Constants.JsonContentType); | ||
|
||
return issuer; | ||
} | ||
} |
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.