diff --git a/docs/openapi/components/schemas/AddApplicationRequest.yaml b/docs/openapi/components/schemas/AddApplicationRequest.yaml index 039cc73..80387dd 100644 --- a/docs/openapi/components/schemas/AddApplicationRequest.yaml +++ b/docs/openapi/components/schemas/AddApplicationRequest.yaml @@ -6,4 +6,7 @@ properties: description: Name of the new application. relyingPartyHostname: type: string - description: Hostname of the application, e.g. example.com \ No newline at end of file + description: Hostname of the application, e.g. example.com + relyingPartyName: + type: string + description: Name of the relying party presented to clients. diff --git a/docs/openapi/components/schemas/Application.yaml b/docs/openapi/components/schemas/Application.yaml index abf6630..acca891 100644 --- a/docs/openapi/components/schemas/Application.yaml +++ b/docs/openapi/components/schemas/Application.yaml @@ -7,7 +7,7 @@ properties: description: Unique identifier for the application. name: type: string - description: Name of the application. + description: Name of the application. Used internally. createdAt: type: string format: date-time @@ -19,3 +19,6 @@ properties: relyingPartyHostname: type: string description: Hostname of the relying party. + relyingPartyName: + type: string + description: Name of the relying party presented to clients. \ No newline at end of file diff --git a/docs/openapi/components/schemas/EditApplicationRequest.yaml b/docs/openapi/components/schemas/EditApplicationRequest.yaml index fee86b6..7783b46 100644 --- a/docs/openapi/components/schemas/EditApplicationRequest.yaml +++ b/docs/openapi/components/schemas/EditApplicationRequest.yaml @@ -6,4 +6,7 @@ properties: description: Name of the application. relyingPartyHostname: type: string - description: Hostname of the relying party. \ No newline at end of file + description: Hostname of the relying party. + relyingPartyName: + type: string + description: Name of the relying party presented to clients. diff --git a/src/main/java/com/helioauth/passkeys/api/mapper/ClientApplicationMapper.java b/src/main/java/com/helioauth/passkeys/api/mapper/ClientApplicationMapper.java index 66263e2..452350b 100644 --- a/src/main/java/com/helioauth/passkeys/api/mapper/ClientApplicationMapper.java +++ b/src/main/java/com/helioauth/passkeys/api/mapper/ClientApplicationMapper.java @@ -17,10 +17,12 @@ package com.helioauth.passkeys.api.mapper; import com.helioauth.passkeys.api.domain.ClientApplication; +import com.helioauth.passkeys.api.generated.models.AddApplicationRequest; import com.helioauth.passkeys.api.generated.models.Application; import com.helioauth.passkeys.api.generated.models.ApplicationApiKey; import com.helioauth.passkeys.api.generated.models.EditApplicationRequest; import org.mapstruct.Mapper; +import org.mapstruct.Mapping; import org.mapstruct.MappingConstants; import org.mapstruct.MappingTarget; @@ -32,9 +34,14 @@ @Mapper(componentModel = MappingConstants.ComponentModel.SPRING) public interface ClientApplicationMapper { Application toResponse(ClientApplication clientApplication); + List toResponse(List clientApplication); ApplicationApiKey toApiKeyResponse(ClientApplication clientApplication); + @Mapping(target = "id", ignore = true) // ID will be generated by the database + @Mapping(target = "apiKey", ignore = true) // API key is generated in the service + ClientApplication toClientApplication(AddApplicationRequest request); + void updateClientApplication(@MappingTarget ClientApplication clientApplication, EditApplicationRequest request); } diff --git a/src/main/java/com/helioauth/passkeys/api/service/ClientApplicationService.java b/src/main/java/com/helioauth/passkeys/api/service/ClientApplicationService.java index 16ff1ba..fd9bc64 100644 --- a/src/main/java/com/helioauth/passkeys/api/service/ClientApplicationService.java +++ b/src/main/java/com/helioauth/passkeys/api/service/ClientApplicationService.java @@ -24,6 +24,7 @@ import com.helioauth.passkeys.api.generated.models.EditApplicationRequest; import com.helioauth.passkeys.api.mapper.ClientApplicationMapper; import lombok.RequiredArgsConstructor; +import lombok.val; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -62,14 +63,11 @@ public List listAll() { } public Application add(AddApplicationRequest request) { + val clientApplication = clientApplicationMapper.toClientApplication(request); + clientApplication.setApiKey(generateApiKey()); + return clientApplicationMapper.toResponse( - repository.save( - ClientApplication.builder() - .name(request.getName()) - .apiKey(generateApiKey()) - .relyingPartyHostname(request.getRelyingPartyHostname()) - .build() - ) + repository.save(clientApplication) ); } diff --git a/src/test/java/com/helioauth/passkeys/api/service/ClientApplicationServiceTest.java b/src/test/java/com/helioauth/passkeys/api/service/ClientApplicationServiceTest.java index e36f104..4b4b7f2 100644 --- a/src/test/java/com/helioauth/passkeys/api/service/ClientApplicationServiceTest.java +++ b/src/test/java/com/helioauth/passkeys/api/service/ClientApplicationServiceTest.java @@ -51,6 +51,8 @@ public class ClientApplicationServiceTest { public static final Application DTO = new Application() .id(UUID.randomUUID()) .name("App Name") + .relyingPartyHostname("example.com") + .relyingPartyName("Example App") .createdAt(Instant.now()) .updatedAt(Instant.now()); @@ -79,6 +81,8 @@ public void addClientApplicationTest() { // Execute AddApplicationRequest addApplicationRequest = new AddApplicationRequest(); addApplicationRequest.setName(DTO.getName()); + addApplicationRequest.setRelyingPartyHostname(DTO.getRelyingPartyHostname()); + addApplicationRequest.setRelyingPartyName(DTO.getRelyingPartyName()); Application result = service.add(addApplicationRequest); // Capture the argument @@ -89,6 +93,8 @@ public void addClientApplicationTest() { assertFalse(savedClientApplication.getApiKey().isEmpty()); assertEquals(DTO.getName(), savedClientApplication.getName()); assertEquals(DTO.getName(), result.getName()); + assertEquals(DTO.getRelyingPartyHostname(), savedClientApplication.getRelyingPartyHostname()); + assertEquals(DTO.getRelyingPartyName(), savedClientApplication.getRelyingPartyName()); } @Test @@ -103,8 +109,8 @@ public void listAllClientApplicationsTest() { // Validate assertNotNull(result); assertEquals(1, result.size()); - assertEquals(DTO.getId(), result.get(0).getId()); - assertEquals(DTO.getName(), result.get(0).getName()); + assertEquals(DTO.getId(), result.getFirst().getId()); + assertEquals(DTO.getName(), result.getFirst().getName()); } @Test @@ -115,6 +121,8 @@ public void editClientApplicationTest() { ClientApplication existingClientApplication = ClientApplication.builder() .id(id) .name("Old Name") + .relyingPartyHostname("example.com") + .relyingPartyName("Example RP") .createdAt(Instant.now()) .updatedAt(Instant.now()) .build(); @@ -125,11 +133,15 @@ public void editClientApplicationTest() { // Execute EditApplicationRequest editApplicationRequest = new EditApplicationRequest(); editApplicationRequest.setName(newName); + editApplicationRequest.setRelyingPartyHostname("example2.com"); + editApplicationRequest.setRelyingPartyName("Example RP 2"); Optional result = service.edit(id, editApplicationRequest); // Validate assertTrue(result.isPresent()); assertEquals(newName, result.get().getName()); + assertEquals(editApplicationRequest.getRelyingPartyHostname(), result.get().getRelyingPartyHostname()); + assertEquals(editApplicationRequest.getRelyingPartyName(), result.get().getRelyingPartyName()); } @Test