Documentation
¶
Overview ¶
Package bearer provides validation adapters for opaque bearer tokens.
Index ¶
Examples ¶
Constants ¶
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 ¶
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 ¶
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.
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 ¶
ValidatorFunc adapts a function to Validator.
func (ValidatorFunc) ValidateBearer ¶
func (f ValidatorFunc) ValidateBearer(ctx context.Context, token string) (authentication.Principal, error)
ValidateBearer calls f.