authn

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package authn provides a generic Kratos middleware for JWT-based authentication. It is engine-agnostic: any Authenticator implementation can be injected.

Example usage:

import (
    "github.com/Servora-Kit/servora/security/authn"
    "github.com/Servora-Kit/servora/security/authn/jwt"
)

mw = append(mw, authn.Server(
    jwt.NewAuthenticator(jwt.WithVerifier(km.Verifier())),
))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractBearerToken

func ExtractBearerToken(header string) string

ExtractBearerToken parses the Bearer token from an Authorization header value. Returns empty string if the header is absent or malformed.

func Server

func Server(authenticator Authenticator, opts ...Option) middleware.Middleware

Server returns a Kratos middleware that authenticates requests using the provided Authenticator. It extracts the Bearer token from the Authorization header, stores it in context via svrmw.NewTokenContext, then delegates to the Authenticator to produce an actor.Actor.

Behavior:

  • No transport in context → anonymous actor injected, handler called
  • No Authorization header → anonymous actor injected (authenticator may override)
  • Authenticator error + no error handler → error returned
  • Authenticator error + error handler → handler's return value used

Types

type Authenticator

type Authenticator interface {
	Authenticate(ctx context.Context) (actor.Actor, error)
}

Authenticator is the interface for authenticating incoming requests. Implementations receive the full request context (which may include the raw token stored by Server) and return an actor.Actor.

type Option

type Option func(*serverConfig)

Option configures the Server middleware.

func WithErrorHandler

func WithErrorHandler(h func(ctx context.Context, err error) error) Option

WithErrorHandler sets a custom error handler invoked when authentication fails.

Directories

Path Synopsis
Package jwt provides a JWT-based Authenticator implementation for pkg/authn.
Package jwt provides a JWT-based Authenticator implementation for pkg/authn.
Package noop provides a no-op Authenticator that always returns an anonymous actor.
Package noop provides a no-op Authenticator that always returns an anonymous actor.

Jump to

Keyboard shortcuts

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