auth

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 10 Imported by: 6

README

Auth

Authenticating and authorizing client/server applications.

Usage

go get github.com/worldline-go/auth

Check http example: example/http

Client

Client is usefull to send request with oauth2 token.

First set a provider.

var providerClient = auth.Provider{
	Keycloak: &providers.KeyCloak{
		ClientID:     "test",
		ClientSecret: "GbkxWi8ZBJvMv2Wsh03JbX183xKAPrEs",
        // Keycloak server url
		BaseURL:      "http://localhost:8080",
		Realm:        "finops",
        // Scopes is optional
		Scopes:       []string{"openid", "profile", "email", "offline_access"},
	},
}

Then when you create a http.Client you can use the oauth2 transport.

client := &http.Client{
    Transport: providerClient.RoundTripperMust(ctx, http.DefaultTransport),
}

Now you can make request with this client.

Server

Check the token in the request. Just need to url of keycloak server and the realm.

var providerServer = auth.Provider{
	Keycloak: &providers.KeyCloak{
        // Keycloak server url
		BaseURL: "http://localhost:8080",
		Realm:   "finops",
	},
}

Then you can check the token in the request.

This is the http based, very simple function but check the our echo middleware to much more advanced operations.

provider := providerServer.ActiveProvider()
if provider == nil {
	return fmt.Errorf("no active provider")
}

keyFunc, err := provider.JWTKeyFunc(ctx)
if err != nil {
	return fmt.Errorf("creating parser: %w", err)
}

defer keyFunc.EndBackground()

// Check the token in the request
claimsValue := claims.Custom{}
token, err := keyFunc.Parser(tokenToCheck, &claimsValue)
if err != nil {
    return fmt.Errorf("token 👎: %w", err)
}

Redirection Flow

When enabled redirection in the middleware, the user will be redirected to the oauth2 login page.

This is not a standard flow and we can change update it any time.
Code for echo middleware is here.

Redirection Flow

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultExpireDuration = time.Second * 10

DefaultExpireDuration is the default duration to check if the access token is about to expire.

View Source
var ErrTokenInvalid = fmt.Errorf("token is invalid")

Functions

func IsRefreshNeed added in v0.4.1

func IsRefreshNeed(accessToken string) (bool, error)

IsRefreshNeed checks if the access token is about to expire.

Types

type InfJWTKeyFunc added in v0.4.0

type InfJWTKeyFunc interface {
	Keyfunc(token *jwt.Token) (interface{}, error)
	EndBackground()
	Parser(tokenString string, claims jwt.Claims) (*jwt.Token, error)
}

type InfProvider added in v0.4.0

type InfProvider interface {
	ClientConfig() (*clientcredentials.Config, error)

	GetCertURL() string
	GetTokenURL() string
	GetAuthURL() string
	GetClientID() string
	GetClientSecret() string
}

type InfProviderExtra added in v0.4.0

type InfProviderExtra interface {
	InfProvider
	// JWTKeyFunc returns the JWT key used to verify the token.
	JWTKeyFunc(ctx context.Context, opts ...OptionJWK) (InfJWTKeyFunc, error)
	IsNoop() bool
}

type JWTKeyFunc added in v0.4.0

type JWTKeyFunc struct {
	*keyfunc.JWKS
}

func (*JWTKeyFunc) Parser added in v0.4.0

func (j *JWTKeyFunc) Parser(tokenString string, claims jwt.Claims) (*jwt.Token, error)

type Noop added in v0.4.0

type Noop struct{}

func (Noop) ClientConfig added in v0.4.0

func (Noop) ClientConfig() (*clientcredentials.Config, error)

func (Noop) GetAuthURL added in v0.4.0

func (Noop) GetAuthURL() string

func (Noop) GetCertURL added in v0.4.0

func (Noop) GetCertURL() string

func (Noop) GetClientID added in v0.4.0

func (Noop) GetClientID() string

func (Noop) GetClientSecret added in v0.4.0

func (Noop) GetClientSecret() string

func (Noop) GetTokenURL added in v0.4.0

func (Noop) GetTokenURL() string

func (Noop) IsNoop added in v0.4.0

func (Noop) IsNoop() bool

func (Noop) JWTKeyFunc added in v0.4.0

func (Noop) JWTKeyFunc(ctx context.Context, opts ...OptionJWK) (InfJWTKeyFunc, error)

type NoopJWTKey added in v0.4.0

type NoopJWTKey struct{}

func (NoopJWTKey) EndBackground added in v0.4.0

func (NoopJWTKey) EndBackground()

func (NoopJWTKey) Keyfunc added in v0.4.0

func (NoopJWTKey) Keyfunc(token *jwt.Token) (interface{}, error)

func (NoopJWTKey) Parser added in v0.4.0

func (NoopJWTKey) Parser(tokenString string, claims jwt.Claims) (*jwt.Token, error)

type OptionActiveProvider added in v0.4.0

type OptionActiveProvider func(options *optionsActiveProvider)

func WithNoop added in v0.4.0

func WithNoop(v bool) OptionActiveProvider

WithNoop sets the active provider to noop.

type OptionJWK

type OptionJWK func(options *optionsJWK)

func WithClient added in v0.2.2

func WithClient(client *http.Client) OptionJWK

WithClient is used to set the http.Client used to fetch the JWKs.

func WithRefreshErrorHandler

func WithRefreshErrorHandler(fn func(err error)) OptionJWK

WithRefreshErrorHandler sets the refresh error handler for the jwt.Key.

func WithRefreshInterval added in v0.2.0

func WithRefreshInterval(d time.Duration) OptionJWK

WithRefreshInterval sets the refresh interval for the jwt.Keyfunc default is 5 minutes.

func WithRefreshUnknownKID added in v0.2.0

func WithRefreshUnknownKID(v bool) OptionJWK

WithRefreshUnknownKID sets the refresh unknown KID for the jwt.Key, default is false.

type Provider

type Provider struct {
	Active   string              `cfg:"active"`
	Keycloak *providers.KeyCloak `cfg:"keycloak"`
}

func (*Provider) ActiveProvider

func (p *Provider) ActiveProvider(opts ...OptionActiveProvider) (ret InfProviderExtra)

ActiveProvider returns the active provider or the first provider if none is active.

Returns nil if no provider is configured.

func (*Provider) RoundTripper

func (p *Provider) RoundTripper(ctx context.Context, transport http.RoundTripper) (http.RoundTripper, error)

RoundTripper returns a new RoundTripper that adds an OAuth2 Transport.

Uses active provider's ClientConfig.

func (*Provider) RoundTripperMust

func (p *Provider) RoundTripperMust(ctx context.Context, transport http.RoundTripper) http.RoundTripper

RoundTripperMust panic if RoundTripper return error.

func (Provider) SetActiveProvider

func (p Provider) SetActiveProvider(name string) *Provider

SetActiveProvider return the provider with the given name as active without modifying the original provider.

type ProviderExtra added in v0.4.0

type ProviderExtra struct {
	InfProvider
	// contains filtered or unexported fields
}

func (*ProviderExtra) IsNoop added in v0.4.0

func (p *ProviderExtra) IsNoop() bool

func (*ProviderExtra) JWTKeyFunc added in v0.4.0

func (p *ProviderExtra) JWTKeyFunc(ctx context.Context, opts ...OptionJWK) (InfJWTKeyFunc, error)

JWTKeyFunc returns a jwt.Keyfunc.

Need GetCertURL in provider.

Directories

Path Synopsis
example
http module
middlewares
authecho module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL