Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Apache Paimon AuthProviderFactory

From Leeroopedia


Knowledge Sources
Domains Authentication, Factory Pattern, Plugin Architecture
Last Updated 2026-02-08 00:00 GMT

Overview

AuthProviderFactory is a factory interface for creating AuthProvider instances using Paimon's plugin discovery mechanism.

Description

AuthProviderFactory extends Paimon's Factory interface to provide a standardized way of creating AuthProvider instances through a plugin-based discovery system. This design enables the authentication system to be extensible without requiring changes to the core catalog code.

The factory interface defines a single creation method that takes Options as input, allowing each factory implementation to extract its required configuration parameters from the options. The identifier() method (inherited from Factory) is used by the discovery mechanism to match the factory to the configured token provider name.

A key feature is the static helper method createAuthProvider(), which encapsulates the entire factory discovery and instantiation process. This method reads the token.provider configuration option, uses Java's ServiceLoader mechanism (via FactoryUtil.discoverFactory()) to find the matching factory implementation on the classpath, and then calls the factory's create method to produce the configured AuthProvider instance.

This pattern follows the service provider interface (SPI) design, allowing new authentication providers to be added by simply implementing the AuthProviderFactory interface, registering it in META-INF/services, and making it available on the classpath. No code changes to the catalog are required.

Usage

Use AuthProviderFactory when implementing new authentication providers for the REST catalog. Implement this interface and register it as a service provider to make your authentication mechanism discoverable and usable through configuration alone.

Code Reference

Source Location

Signature

public interface AuthProviderFactory extends Factory {

    AuthProvider create(Options options);

    static AuthProvider createAuthProvider(Options options) {
        String tokenProvider = options.get(TOKEN_PROVIDER);
        if (StringUtils.isEmpty(tokenProvider)) {
            throw new IllegalArgumentException("token.provider is not set.");
        }
        AuthProviderFactory factory =
                FactoryUtil.discoverFactory(
                        AuthProviderFactory.class.getClassLoader(),
                        AuthProviderFactory.class,
                        tokenProvider);
        return factory.create(options);
    }
}

Import

import org.apache.paimon.rest.auth.AuthProviderFactory;

I/O Contract

Inputs

Name Type Required Description
options Options Yes Configuration options containing authentication parameters
tokenProvider String Yes Token provider identifier (from token.provider option)

Outputs

Name Type Description
create() AuthProvider Returns configured AuthProvider instance
createAuthProvider() AuthProvider Static helper that discovers and creates AuthProvider from options

Usage Examples

import org.apache.paimon.rest.auth.AuthProvider;
import org.apache.paimon.rest.auth.AuthProviderFactory;
import org.apache.paimon.options.Options;

// Example 1: Implement a custom AuthProviderFactory
public class CustomAuthProviderFactory implements AuthProviderFactory {

    @Override
    public String identifier() {
        return "custom";  // Used in token.provider configuration
    }

    @Override
    public AuthProvider create(Options options) {
        String apiKey = options.get("custom.api-key");
        String apiSecret = options.get("custom.api-secret");

        if (apiKey == null || apiSecret == null) {
            throw new IllegalArgumentException(
                "custom.api-key and custom.api-secret are required");
        }

        return new CustomAuthProvider(apiKey, apiSecret);
    }
}

// Example 2: Register the factory in META-INF/services
// Create file: META-INF/services/org.apache.paimon.rest.auth.AuthProviderFactory
// Content:
// com.example.CustomAuthProviderFactory

// Example 3: Use the factory through configuration
Options options = new Options();
options.set("token.provider", "custom");
options.set("custom.api-key", "my-api-key");
options.set("custom.api-secret", "my-api-secret");

AuthProvider provider = AuthProviderFactory.createAuthProvider(options);

// Example 4: DLF factory usage (built-in)
Options dlfOptions = new Options();
dlfOptions.set("token.provider", "dlf");
dlfOptions.set("dlf.region", "cn-hangzhou");
dlfOptions.set("dlf.access-key-id", "LTAI***");
dlfOptions.set("dlf.access-key-secret", "secret***");

AuthProvider dlfProvider = AuthProviderFactory.createAuthProvider(dlfOptions);

// Example 5: Factory with optional parameters
public class FlexibleAuthProviderFactory implements AuthProviderFactory {

    @Override
    public String identifier() {
        return "flexible";
    }

    @Override
    public AuthProvider create(Options options) {
        String mode = options.getOptional("flexible.mode").orElse("basic");

        switch (mode) {
            case "basic":
                String token = options.get("flexible.token");
                return new BasicAuthProvider(token);

            case "advanced":
                String clientId = options.get("flexible.client-id");
                String clientSecret = options.get("flexible.client-secret");
                return new AdvancedAuthProvider(clientId, clientSecret);

            default:
                throw new IllegalArgumentException("Unknown mode: " + mode);
        }
    }
}

// Example 6: Factory with validation
public class ValidatingAuthProviderFactory implements AuthProviderFactory {

    @Override
    public String identifier() {
        return "validated";
    }

    @Override
    public AuthProvider create(Options options) {
        String endpoint = options.get("validated.endpoint");
        String credentials = options.get("validated.credentials");

        // Validate configuration
        validateEndpoint(endpoint);
        validateCredentials(credentials);

        return new ValidatedAuthProvider(endpoint, credentials);
    }

    private void validateEndpoint(String endpoint) {
        if (!endpoint.startsWith("https://")) {
            throw new IllegalArgumentException(
                "Endpoint must use HTTPS: " + endpoint);
        }
    }

    private void validateCredentials(String credentials) {
        if (credentials.length() < 32) {
            throw new IllegalArgumentException("Credentials too short");
        }
    }
}

// Example 7: Error handling
public void createAuthProviderSafely(Options options) {
    try {
        AuthProvider provider = AuthProviderFactory.createAuthProvider(options);
        System.out.println("Auth provider created: " + provider.getClass().getName());
    } catch (IllegalArgumentException e) {
        System.err.println("Configuration error: " + e.getMessage());
    } catch (Exception e) {
        System.err.println("Failed to create auth provider: " + e.getMessage());
    }
}

// Example 8: Multiple provider support
public class MultiProviderExample {
    private final Map<String, AuthProvider> providers = new HashMap<>();

    public void initializeProviders(List<Options> optionsList) {
        for (Options options : optionsList) {
            String providerName = options.get("token.provider");
            AuthProvider provider = AuthProviderFactory.createAuthProvider(options);
            providers.put(providerName, provider);
        }
    }

    public AuthProvider getProvider(String name) {
        return providers.get(name);
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment