private CreateHashedApiKeyResponse createApiKey(final Namespace namespace,
                                                    final UserRef userRef) {
        final String apiKeyName = namespace.getString(API_KEY_NAME_ARG_NAME);
        if (NullSafe.isBlankString(apiKeyName)) {
            throw new RuntimeException("A name must be provided for the API key");
        }
        final Integer lifetimeDays = namespace.getInt(EXPIRY_DAYS_ARG_NAME);
        if (lifetimeDays != null && lifetimeDays <= 0) {
            throw new RuntimeException(EXPIRY_DAYS_ARG_NAME + " must be greater than zero.");
        }

        final HashAlgorithm hashAlgorithm = Objects.requireNonNullElse(
                namespace.get(HASH_ALGORITHM_ARG_NAME),
                HashAlgorithm.DEFAULT);

        LOGGER.info("Creating API key for user '{}' using algorithm '{}'",
                userRef.toInfoString(),
                hashAlgorithm.getDisplayValue());

        // Service will give us a default expire time if null
        final Long expireTimeEpochMs = NullSafe.get(
                lifetimeDays,
                days -> Duration.ofDays(days),
                duration -> Instant.now().plus(duration).toEpochMilli());

        final String comments = Objects.requireNonNullElse(
                namespace.getString(COMMENTS_ARG_NAME),
                "Created by 'create_api_key' command.");

        return apiKeyService.create(new CreateHashedApiKeyRequest(
                userRef,
                expireTimeEpochMs,
                apiKeyName,
                comments,
                true,
                hashAlgorithm));
    }
Creates an API key for a user the internal identity provider
stroom-app/src/main/java/stroom/app/commands/CreateApiKeyCommand.java