All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.fitko.fitconnect.core.utils.EventLogUtil Maven / Gradle / Ivy

Go to download

Library that provides client access to the FIT-Connect api-endpoints for sending, subscribing and routing

The newest version!
package dev.fitko.fitconnect.core.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import dev.fitko.fitconnect.api.domain.model.event.Event;
import dev.fitko.fitconnect.api.domain.model.event.EventClaimFields;
import dev.fitko.fitconnect.api.domain.model.event.EventIssuer;
import dev.fitko.fitconnect.api.domain.model.event.EventLog;
import dev.fitko.fitconnect.api.domain.model.event.EventLogEntry;
import dev.fitko.fitconnect.api.domain.model.event.EventSubjectType;
import dev.fitko.fitconnect.api.domain.model.event.TypeAndUUID;
import dev.fitko.fitconnect.api.domain.model.event.authtags.AuthenticationTags;
import dev.fitko.fitconnect.api.domain.model.event.problems.Problem;
import dev.fitko.fitconnect.api.exceptions.internal.EventLogException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static dev.fitko.fitconnect.api.domain.model.event.EventIssuer.DESTINATION;
import static dev.fitko.fitconnect.api.domain.model.event.EventIssuer.SUBMISSION_SERVICE;
import static dev.fitko.fitconnect.api.domain.model.event.EventIssuer.UNDEFINED;

public final class EventLogUtil {

    private static final ObjectMapper MAPPER = new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

    public static final String AUTH_TAG_SPLIT_TOKEN = "\\.";

    private EventLogUtil() {
    }

    public static List mapEventLogToEntries(final List eventLog) {
        return eventLog.stream().map(EventLogUtil::eventToLogEntry).collect(Collectors.toList());
    }

    public static EventIssuer resolveIssuerType(final String issuer) {
        if (issuer == null) {
            return UNDEFINED;
        }
        return issuer.startsWith("http") ? SUBMISSION_SERVICE : DESTINATION;
    }

    public static Event getEventFromClaims(final JWTClaimsSet claims) {
        final Map events = (Map) claims.getClaim(EventClaimFields.CLAIM_EVENTS);
        final Map.Entry eventEntry = (Map.Entry) events.entrySet().iterator().next();
        return Event.fromSchemaUri(eventEntry.getKey());
    }

    public static Event getEventFromJWT(final SignedJWT event) {
        try {
            final JWTClaimsSet claims = event.getJWTClaimsSet();
            final Map events = (Map) claims.getClaim(EventClaimFields.CLAIM_EVENTS);
            final Map.Entry eventEntry = (Map.Entry) events.entrySet().iterator().next();
            return Event.fromSchemaUri(eventEntry.getKey());
        } catch (final ParseException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }

    public static UUID getDestinationId(final String issuer) {
        final EventIssuer issuerType = resolveIssuerType(issuer);
        if (issuerType.equals(SUBMISSION_SERVICE) || issuerType.equals(UNDEFINED)) {
            return null;
        }
        try {
            return UUID.fromString(issuer);
        } catch (final IllegalArgumentException ex) {
            throw new EventLogException("Destination id '" + issuer + "' from issuer is no valid uuid");
        }
    }

    public static EventLogEntry eventToLogEntry(final SignedJWT signedJWT) {
        final JWTClaimsSet claimsSet = getClaimsSet(signedJWT);
        final Map events = getClaimsAsMap(signedJWT);
        final Map.Entry eventEntry = events.entrySet().iterator().next();
        final Payload eventPayload = getEventPayload(eventEntry.getValue());
        return EventLogEntry.builder()
                .event(Event.fromSchemaUri(eventEntry.getKey()))
                .issuer(claimsSet.getIssuer())
                .issueTime(claimsSet.getIssueTime())
                .eventId(getEventId(claimsSet))
                .caseId(getTransactionClaim(claimsSet))
                .submissionId(getIdFromJWTSubject(claimsSet))
                .problems(eventPayload.getProblems())
                .build();
    }

    public static List getJWTSFromEvents(final List events) {
        return events.stream()
                .map(EventLogUtil::getJWTFromEvent)
                .collect(Collectors.toList());
    }

    public static List getJWTSFromEvents(final List events, final EventSubjectType subjectFilter) {
        return events.stream()
                .map(EventLogUtil::getJWTFromEvent)
                .filter(eventSubjectEqualsType(subjectFilter))
                .collect(Collectors.toList());
    }

    public static UUID getIdFromJWTSubject(final JWTClaimsSet claimsSet) {
        return TypeAndUUID.fromString(claimsSet.getSubject()).getUuid();
    }

    public static UUID getIdFromJWTSubject(final SignedJWT signedJWT) {
        return TypeAndUUID.fromString(getClaimsSet(signedJWT).getSubject()).getUuid();
    }

    public static AuthenticationTags getAuthTags(final SignedJWT jwt) {
        return getAuthTags(getClaimsSet(jwt));
    }

    public static AuthenticationTags getAuthTags(final JWTClaimsSet claims) {
        try {
            final Map eventsClaim = (Map) claims.getClaim(EventClaimFields.CLAIM_EVENTS);
            final Map.Entry eventEntry = (Map.Entry) eventsClaim.entrySet().iterator().next();
            final Map events = eventEntry.getValue();
            final String authTags = MAPPER.writeValueAsString(events.get(EventClaimFields.AUTHENTICATION_TAGS));
            return MAPPER.readValue(authTags, AuthenticationTags.class);
        } catch (final JsonProcessingException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }


    public static String getAuthenticationTagFromEncryptedData(final String encryptedData) {
        try {
            final String[] parts = encryptedData.split(AUTH_TAG_SPLIT_TOKEN);
            return parts[parts.length - 1];
        } catch (final Exception e) {
            return null;
        }
    }

    public static List getFilteredJwtsFromEventLog(final UUID subjectFilterId, final Event event, final EventLog eventLog) {
        return getJWTSFromEvents(eventLog.getEventLogs()).stream()
                .filter(jwt -> getIdFromJWTSubject(jwt).equals(subjectFilterId))
                .filter(jwt -> getEventFromJWT(jwt).equals(event))
                .collect(Collectors.toList());
    }

    public static Predicate eventSubjectEqualsId(final UUID id) {
        return jwt -> {
            try {
                final String subject = jwt.getJWTClaimsSet().getSubject();
                return TypeAndUUID.fromString(subject).getUuid().equals(id);
            } catch (final ParseException e) {
                throw new EventLogException(e.getMessage(), e);
            }
        };
    }

    public static Predicate eventSubjectEqualsType(final EventSubjectType subjectType) {
        return jwt -> {
            try {
                final String subject = jwt.getJWTClaimsSet().getSubject();
                return TypeAndUUID.fromString(subject).getType().equals(subjectType.getName());
            } catch (final ParseException e) {
                throw new EventLogException(e.getMessage(), e);
            }
        };
    }

    private static UUID getEventId(final JWTClaimsSet claimsSet) {
        return UUID.fromString(claimsSet.getJWTID());
    }

    private static UUID getTransactionClaim(final JWTClaimsSet claimsSet) {
        try {
            return TypeAndUUID.fromString(claimsSet.getStringClaim(EventClaimFields.CLAIM_TXN)).getUuid();
        } catch (final ParseException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }

    private static JWTClaimsSet getClaimsSet(final SignedJWT signedJWT) {
        try {
            return signedJWT.getJWTClaimsSet();
        } catch (final ParseException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }

    private static Map getClaimsAsMap(final SignedJWT signedJWT) {
        try {
            return signedJWT.getJWTClaimsSet().getJSONObjectClaim(EventClaimFields.CLAIM_EVENTS);
        } catch (final ParseException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }

    private static SignedJWT getJWTFromEvent(final String event) {
        try {
            return SignedJWT.parse(event);
        } catch (final ParseException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }

    private static Payload getEventPayload(Object payload) {
        try {
            return MAPPER.readValue(MAPPER.writeValueAsString(payload), Payload.class);
        } catch (JsonProcessingException e) {
            throw new EventLogException(e.getMessage(), e);
        }
    }

    @Data
    @NoArgsConstructor
    private static class Payload {
        AuthenticationTags authenticationTags;
        List problems = new ArrayList<>();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy