Skip to content

Commit

Permalink
Improve logging and make capabilities lowercase (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
trondsevre authored Nov 11, 2024
1 parent f640834 commit bc735ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package no.fintlabs.provider.security;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.fintlabs.adapter.models.AdapterCapability;
import no.fintlabs.provider.exception.InvalidAdapterCapabilityException;
import no.fintlabs.provider.security.resource.ResourceContext;
import org.springframework.stereotype.Component;

import java.util.Set;

@Slf4j
@Component
@RequiredArgsConstructor
public class AdapterRegistrationValidator {
Expand All @@ -16,8 +18,9 @@ public class AdapterRegistrationValidator {

public void validateCapabilities(Set<AdapterCapability> capabilities) {
capabilities.forEach(capability -> {
String componentResource = "%s-%s-%s".formatted(capability.getDomainName(), capability.getPackageName(), capability.getResourceName());
String componentResource = "%s-%s-%s".formatted(capability.getDomainName(), capability.getPackageName(), capability.getResourceName()).toLowerCase();
if (!resourceContext.getValidResources().contains(componentResource)) {
log.warn("Validation failed: Capability '{}' from '{}' is not a valid resource.", capability, componentResource);
throw new InvalidAdapterCapabilityException("Invalid capability resource: %s".formatted(componentResource));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ public void validateAdapterId(CorePrincipal corePrincipal, String adapterId) {

public void validateAdapterCapabilityPermission(String adapterId, String domainName, String packageName, String entityName) {
if (!adapterContractContext.adapterCanPerformCapability(adapterId, domainName, packageName, entityName)) {
log.warn("Validation failed: Adapter '{}' lacks capability to perform action on '{}-{}-{}'.", adapterId, domainName, packageName, entityName);
throw new CapabilityNotSupportedException("Adapter lacks the necessary capabilities to perform this action");
}
}

public void validateOrgId(CorePrincipal corePrincipal, String requestedOrgId) {
if (corePrincipal.doesNotContainAsset(requestedOrgId.replace("-", ".").replace("_", "."))) {
log.warn("Validation failed: JWT for user '{}' does not have access to organization '{}'. Available assets: {}", corePrincipal.getUsername(), requestedOrgId, corePrincipal.getAssets());
throw new InvalidOrgId("Adapter assets does not contain the organization for the request");
}
}

public void validateUsername(CorePrincipal corePrincipal, String contractUsername) {
if (corePrincipal.doesNotHaveMatchingUsername(contractUsername)) {
log.warn("Validation failed: Username mismatch. JWT's username '{}' does not match contract username '{}'.", corePrincipal.getUsername(), contractUsername);
throw new InvalidUsername("Adapter username does not match contract username");
}
}

public void validateRole(CorePrincipal corePrincipal, String domain, String packageName) {
String role = String.format("FINT_Adapter_%s_%s", domain.toLowerCase(), packageName.toLowerCase());
if (corePrincipal.doesNotHaveRole(role)) {
log.warn("Validation failed: Principal '{}' is missing required role '{}'. Current roles: {}", corePrincipal.getName(), role, corePrincipal.getRoles());
throw new MissingRoleException("Adapter does not have the correct role to perform this action");
}

}

}

0 comments on commit bc735ce

Please sign in to comment.