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

id.unum.utils.Utils Maven / Gradle / Ivy

The newest version!
package id.unum.utils;

import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;
import id.unum.crossPlatformInterfaces.Encoding;
import id.unum.error.UnumError;
import id.unum.protos.credential.v1.Credential;
import id.unum.protos.crypto.v1.KeyPair;
import id.unum.protos.crypto.v1.KeyPairSet;
import id.unum.protos.presentation.v1.Presentation;
import id.unum.types.PublicKeyInfo;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.google.protobuf.util.Timestamps.fromMillis;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.in;

public class Utils {

    public static void requireAuth(String auth) throws UnumError {
        if (auth == null) {
            throw new UnumError(403, "Auth is required");
        }
    }

    public static List getVersionList() {
        return Arrays.asList("3.0.0");
    }

    public static List extractPublicKeyInfo(KeyPairSet keyPairSet, Encoding encoding) {
        PublicKeyInfo signingPublicKeyInfo = constructKeyInfo(keyPairSet.getSigning(), "secp256r1", encoding);
        PublicKeyInfo encryptionPublicKeyInfo = constructKeyInfo(keyPairSet.getEncryption(), "RSA", encoding);

        return Arrays.asList(signingPublicKeyInfo, encryptionPublicKeyInfo);
    }

    public static boolean isDeclinedPresentation(Presentation presentation) {
        return presentation.getVerifiableCredentialCount() == 0;
    }

    public static boolean isCredentialExpired(Credential credential) {
        Timestamp expirationDate = credential.getExpirationDate();

        // Note the default timestamp value is 1970-01-01, midnight UTC. ref: https://github.com/OpenObservability/OpenMetrics/issues/183#:~:text=1970-01-01%2C%20midnight%20UTC%2C
        Timestamp now = fromMillis(currentTimeMillis());
        return Timestamps.compare(expirationDate, now) < 0;
    }

    /**
     * Gets the actual credential type.
     * Note: thanks to following W3C spec, credential types have to start with "VerifiableCredential"
     * ref: https://www.w3.org/TR/vc-data-model/#credentials
     * @param credential
     * @return
     */
    public static String getCredentialType(Credential credential) {
        if (credential.getTypeCount() < 2) {
            throw new UnumError(500, "Credential type is not W3C compliant " + credential.getTypeList());
        }

        // The "real" type always ought to be after VerifiableCredential in the array.
        return credential.getType(1);
    }

    public static String handleAuthToken(String input) {
        // If authToken is undefined see if the input existing auth token is a valid Bearer token (not an admin key), if an admin key just return undefined, otherwise return a properly formatted Bearer token for use in subsequent requests or the existing, inputting token.
        return input != null ? (input.startsWith("Bearer ") ? input : "Bearer " + input) : input;
    }

    private static PublicKeyInfo constructKeyInfo(KeyPair keyPair, String type, Encoding encoding) {
        Date now = new Date();
        UUID uuid = UUID.randomUUID();
        PublicKeyInfo result = new PublicKeyInfo();
        result.setId(uuid.toString());
        result.setPublicKey(keyPair.getPublicKey());
        result.setEncoding(encoding.toString().toLowerCase());
        result.setType(type);
        result.setStatus("valid");
        result.setCreatedAt(now.toString());
        result.setUpdatedAt(now.toString());

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy