github.com/Axway/agent-sdk@v1.1.101/pkg/authz/oauth/idpregistry.go (about)

     1  package oauth
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/Axway/agent-sdk/pkg/config"
     9  	corecfg "github.com/Axway/agent-sdk/pkg/config"
    10  )
    11  
    12  type ConfigOption func(config.IDPConfig) error
    13  
    14  type IdPRegistry interface {
    15  	// RegisterProvider - registers the provider using the config
    16  	RegisterProvider(ctx context.Context, idp corecfg.IDPConfig, tlsCfg corecfg.TLSConfig, proxyURL string, clientTimeout time.Duration) error
    17  	// UnregisterProvider - un-registers the provider
    18  	UnregisterProvider(ctx context.Context, provider Provider) error
    19  	// GetProviderByName - returns the provider from registry based on the name
    20  	GetProviderByName(ctx context.Context, name string, opts ...ConfigOption) (Provider, error)
    21  	// GetProviderByIssuer - returns the provider from registry based on the IDP issuer
    22  	GetProviderByIssuer(ctx context.Context, issuer string, opts ...ConfigOption) (Provider, error)
    23  	// GetProviderByTokenEndpoint - returns the provider from registry based on the IDP token endpoint
    24  	GetProviderByTokenEndpoint(ctx context.Context, tokenEndpoint string, opts ...ConfigOption) (Provider, error)
    25  	// GetProviderByAuthorizationEndpoint - returns the provider from registry based on the IDP authorization endpoint
    26  	GetProviderByAuthorizationEndpoint(ctx context.Context, authEndpoint string, opts ...ConfigOption) (Provider, error)
    27  	// GetProviderByMetadataURL - returns the provider from registry based on the IDP metadata URL
    28  	GetProviderByMetadataURL(ctx context.Context, metadataURL string, opts ...ConfigOption) (Provider, error)
    29  }
    30  
    31  type idpRegistry struct {
    32  	registry ProviderRegistry
    33  }
    34  type IdpRegistryOption func(r *idpRegistry)
    35  
    36  func WithProviderRegistry(providerRegistry ProviderRegistry) IdpRegistryOption {
    37  	return func(r *idpRegistry) {
    38  		r.registry = providerRegistry
    39  	}
    40  }
    41  
    42  // NewProviderRegistry - create a new provider registry
    43  func NewIdpRegistry(opts ...IdpRegistryOption) IdPRegistry {
    44  	r := &idpRegistry{
    45  		registry: NewProviderRegistry(),
    46  	}
    47  	for _, o := range opts {
    48  		o(r)
    49  	}
    50  	return r
    51  }
    52  
    53  func (p *idpRegistry) RegisterProvider(_ context.Context, idp corecfg.IDPConfig, tlsCfg corecfg.TLSConfig, proxyURL string, clientTimeout time.Duration) error {
    54  	return p.registry.RegisterProvider(idp, tlsCfg, proxyURL, clientTimeout)
    55  }
    56  
    57  func (p *idpRegistry) UnregisterProvider(_ context.Context, provider Provider) error {
    58  	return fmt.Errorf("not implemented")
    59  }
    60  
    61  func (p *idpRegistry) GetProviderByName(_ context.Context, name string, _ ...ConfigOption) (Provider, error) {
    62  	return p.registry.GetProviderByName(name)
    63  }
    64  
    65  func (p *idpRegistry) GetProviderByIssuer(_ context.Context, issuer string, _ ...ConfigOption) (Provider, error) {
    66  	return p.registry.GetProviderByIssuer(issuer)
    67  }
    68  
    69  func (p *idpRegistry) GetProviderByTokenEndpoint(_ context.Context, tokenEndpoint string, _ ...ConfigOption) (Provider, error) {
    70  	return p.registry.GetProviderByTokenEndpoint(tokenEndpoint)
    71  }
    72  
    73  func (p *idpRegistry) GetProviderByAuthorizationEndpoint(_ context.Context, authEndpoint string, _ ...ConfigOption) (Provider, error) {
    74  	return p.registry.GetProviderByAuthorizationEndpoint(authEndpoint)
    75  }
    76  
    77  func (p *idpRegistry) GetProviderByMetadataURL(_ context.Context, metadataURL string, _ ...ConfigOption) (Provider, error) {
    78  	return p.registry.GetProviderByMetadataURL(metadataURL)
    79  }