bearer

package
v1.0.0-beta.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package bearer provides Bearer token authentication support for Aegis.

This plugin enables Bearer token authentication by activating token extraction from the Authorization header in the core AuthMiddleware. Without this plugin, Aegis only supports cookie-based session authentication.

Bearer Authentication Flow:

  1. Client obtains session token (via login, OAuth, etc.)
  2. Client includes token in Authorization header: "Authorization: Bearer <token>"
  3. AuthMiddleware extracts token from header (if bearer plugin is registered)
  4. SessionService validates token against database + Redis cache
  5. User is authenticated and injected into request context

Use Cases:

  • Mobile apps: No cookie support, need token-based auth
  • API clients: cURL, Postman, automated scripts
  • Server-to-server: Microservices, webhooks, integrations
  • Single-page apps (SPAs): JavaScript clients with fetch/axios

Security Notes:

  • Tokens are the same session tokens used for cookie auth (shared infrastructure)
  • Tokens should be stored securely (iOS keychain, Android keystore, secure storage)
  • Use HTTPS to prevent token interception
  • Tokens don't have CSRF protection (unlike cookies), but not vulnerable to CSRF

Example:

package main

import (
	"context"
	"github.com/theinventorylib/aegis"
	"github.com/theinventorylib/aegis/plugins/bearer"
)

func main() {
	a, _ := aegis.New(context.Background(), ...)

	// Enable Bearer token authentication
	a.Use(context.Background(), bearer.New(nil))

	a.MountRoutes("/auth")
	// Now clients can use: Authorization: Bearer <session_token>
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
}

Config holds Bearer plugin configuration.

Currently unused - reserved for future extensions:

  • Custom token extraction patterns
  • Token validation hooks
  • Header name customization
  • Token prefix customization (currently hardcoded to "Bearer ")

type Plugin

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

Plugin represents the Bearer token authentication plugin.

This is a lightweight feature toggle plugin that enables Authorization header token extraction in the core AuthMiddleware. It doesn't add routes, migrations, or database tables - it simply activates existing session validation for Bearer tokens.

Architecture:

  • No database schema: Uses existing session tables
  • No HTTP routes: Token validation is handled by core middleware
  • No configuration: Zero-config feature toggle
  • No dependencies: Works with any session backend

When NOT to use this plugin:

  • Cookie-only authentication: Don't register this plugin
  • Custom token formats: Use JWT plugin for custom token structure
  • OAuth only: OAuth plugin handles its own token formats

func New

func New(_ *Config) *Plugin

New creates a new Bearer authentication plugin.

The config parameter is currently unused (reserved for future features). Pass nil for now.

Example:

bearerPlugin := bearer.New(nil)
aegis.Use(ctx, bearerPlugin)

func (*Plugin) Dependencies

func (p *Plugin) Dependencies() []plugins.Dependency

Dependencies returns plugin dependencies.

Bearer plugin has no dependencies - it works standalone with core Aegis. It doesn't require any other plugins to function.

Returns empty slice.

func (*Plugin) Description

func (p *Plugin) Description() string

Description returns a human-readable description for documentation and introspection.

func (*Plugin) GetMigrations

func (p *Plugin) GetMigrations() []plugins.Migration

GetMigrations returns the plugin database migrations.

Bearer plugin has no migrations because it doesn't introduce new database tables. It reuses the existing session infrastructure.

Returns empty slice.

func (*Plugin) GetSchemas

func (p *Plugin) GetSchemas() []plugins.Schema

GetSchemas returns database schemas for all supported dialects.

Bearer plugin has no schema because it doesn't add database tables. It relies entirely on the core session tables.

Returns empty slice.

func (*Plugin) Init

func (p *Plugin) Init(_ context.Context, aegis plugins.Aegis) error

Init initializes the plugin and enables Bearer token authentication.

This is called during plugin registration (aegis.Use) and performs the key activation: enabling Bearer token support in the SessionService.

After Init completes:

  • AuthMiddleware checks Authorization header for "Bearer <token>"
  • Session tokens can be passed via header instead of cookies
  • Mobile apps and API clients can authenticate

Parameters:

  • ctx: Context for initialization (can be canceled)
  • aegis: Framework instance providing access to services

Returns:

  • error: Always nil (initialization cannot fail for this plugin)

func (*Plugin) MountRoutes

func (p *Plugin) MountRoutes(_ router.Router, _ string)

MountRoutes registers HTTP routes for the plugin.

Bearer plugin has no routes because token validation is handled by the core AuthMiddleware automatically. The plugin is purely a feature toggle.

Future considerations:

  • Token introspection endpoint: /bearer/introspect
  • Token refresh endpoint: /bearer/refresh
  • Token revocation endpoint: /bearer/revoke

For now, these features are available through core routes (/auth/logout, etc.).

func (*Plugin) Name

func (p *Plugin) Name() string

Name returns the plugin identifier used for registration and lookup.

This name is used when:

  • Registering the plugin with Aegis
  • Looking up the plugin via GetPlugin("bearer")
  • Logging and debugging

func (*Plugin) ProvidesAuthMethods

func (p *Plugin) ProvidesAuthMethods() []string

ProvidesAuthMethods returns authentication methods provided by this plugin.

Returns ["bearer"] to indicate Bearer token authentication is available. This is used for introspection and capability discovery.

func (*Plugin) RequiresTables

func (p *Plugin) RequiresTables() []string

RequiresTables returns required database tables for schema validation.

Bearer plugin requires the core session table to exist for token validation. This is validated during plugin initialization.

func (*Plugin) Version

func (p *Plugin) Version() string

Version returns the plugin version following semantic versioning.

Jump to

Keyboard shortcuts

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