bearer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package bearer provides validation adapters for opaque bearer tokens.

Index

Examples

Constants

View Source
const MaxEntries = 256

MaxEntries bounds active static bearer candidates and per-request work.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator struct {
	// contains filtered or unexported fields
}

Authenticator validates opaque bearer credentials through a callback or interface.

func New

func New(validator Validator, options ...Option) (*Authenticator, error)

New creates an opaque bearer authenticator.

Example
package main

import (
	"context"
	"fmt"

	authentication "github.com/faustbrian/go-authentication"
	"github.com/faustbrian/go-authentication/bearer"
)

func main() {
	authenticator, _ := bearer.New(bearer.ValidatorFunc(
		func(_ context.Context, token string) (authentication.Principal, error) {
			if token != "opaque-token" {
				return authentication.Principal{}, authentication.NewFailure(authentication.FailureRejected)
			}
			return authentication.NewPrincipal(authentication.PrincipalSpec{
				Subject: "service", Method: "bearer",
			})
		},
	))
	result, err := authenticator.Authenticate(
		context.Background(),
		authentication.NewBearerCredential("opaque-token"),
	)
	principal, authenticated := result.Principal()
	fmt.Println(err, authenticated, principal.Subject())
}
Output:
<nil> true service

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Result, error)

Authenticate validates one bounded bearer credential.

type Entry

type Entry struct {
	Token     string
	Principal authentication.PrincipalSpec
}

Entry configures one active static bearer token and its principal.

type Option

type Option func(*config)

Option configures an Authenticator.

func WithMaxTokenBytes

func WithMaxTokenBytes(maximum int) Option

WithMaxTokenBytes sets the inclusive token-size bound.

type Static

type Static struct {
	// contains filtered or unexported fields
}

Static validates bearer tokens against an atomically replaceable bounded token set. Every well-formed credential is compared with every active entry. A Static must not be copied after first use.

func NewStatic

func NewStatic(entries []Entry) (*Static, error)

NewStatic validates and copies the initial active token set.

Example
package main

import (
	"context"
	"fmt"

	authentication "github.com/faustbrian/go-authentication"
	"github.com/faustbrian/go-authentication/bearer"
)

func main() {
	authenticator, _ := bearer.NewStatic([]bearer.Entry{
		{Token: "current", Principal: authentication.PrincipalSpec{Subject: "service"}},
		{Token: "previous", Principal: authentication.PrincipalSpec{Subject: "service"}},
	})
	result, err := authenticator.Authenticate(
		context.Background(),
		authentication.NewBearerCredential("previous"),
	)
	principal, authenticated := result.Principal()
	fmt.Println(err, authenticated, principal.Subject())
}
Output:
<nil> true service

func (*Static) Authenticate

func (s *Static) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Result, error)

Authenticate validates one bounded bearer credential against a single immutable token-set snapshot.

func (*Static) Replace

func (s *Static) Replace(entries []Entry) error

Replace atomically replaces all active tokens after validating the complete candidate set. A failed replacement leaves the previous set active.

type Validator

type Validator interface {
	ValidateBearer(context.Context, string) (authentication.Principal, error)
}

Validator validates an opaque bearer token and returns an immutable principal.

type ValidatorFunc

type ValidatorFunc func(context.Context, string) (authentication.Principal, error)

ValidatorFunc adapts a function to Validator.

func (ValidatorFunc) ValidateBearer

func (f ValidatorFunc) ValidateBearer(ctx context.Context, token string) (authentication.Principal, error)

ValidateBearer calls f.

Jump to

Keyboard shortcuts

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