Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/openapi/components/schemas/AddApplicationRequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ properties:
description: Name of the new application.
relyingPartyHostname:
type: string
description: Hostname of the application, e.g. example.com
description: Hostname of the application, e.g. example.com
relyingPartyName:
type: string
description: Name of the relying party presented to clients.
5 changes: 4 additions & 1 deletion docs/openapi/components/schemas/Application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
5 changes: 4 additions & 1 deletion docs/openapi/components/schemas/EditApplicationRequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ properties:
description: Name of the application.
relyingPartyHostname:
type: string
description: Hostname of the relying party.
description: Hostname of the relying party.
relyingPartyName:
type: string
description: Name of the relying party presented to clients.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,9 +34,14 @@
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface ClientApplicationMapper {
Application toResponse(ClientApplication clientApplication);

List<Application> toResponse(List<ClientApplication> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,14 +63,11 @@ public List<Application> 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)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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<Application> 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
Expand Down