apikey

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package apikey provides API-key authentication for Fiber, distinct from the JWT-bearer auth package: a presented key is hashed and looked up in a pluggable store to resolve its identity and scopes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate() (key, hash string, err error)

Generate returns a new random API key and its hash. Give the key to the client and persist the hash.

Example

Generate a key for a client, store its hash, then authenticate with it.

package main

import (
	"context"
	"fmt"

	"github.com/rahmadafandi/fibr/apikey"
)

func main() {
	key, hash, _ := apikey.Generate()

	// Persist hash (here in a MapStore); hand `key` to the client.
	store := apikey.MapStore(map[string]apikey.Identity{
		hash: {ID: "service-a", Scopes: []string{"read"}},
	})

	// On a request, hash the presented key and look it up.
	id, _ := store.Lookup(context.Background(), apikey.Hash(key))
	fmt.Println(id.ID, id.Scopes)
}
Output:
service-a [read]

func HasScope

func HasScope(c *fiber.Ctx, scope string) bool

HasScope reports whether the request's Identity has scope.

func Hash

func Hash(key string) string

Hash returns the SHA-256 hex of a raw key, which is what a Store keys on.

Types

type Authenticator

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

Authenticator validates API keys against a Store.

func New

func New(cfg Config) *Authenticator

New returns an Authenticator. Store is required.

func (*Authenticator) Middleware

func (a *Authenticator) Middleware() fiber.Handler

Middleware reads the key from the configured header, hashes it, looks it up, and on success stores the Identity in locals and calls Next; otherwise it returns 401 (or 500 on a store error).

func (*Authenticator) RequireScope

func (a *Authenticator) RequireScope(scope string) fiber.Handler

RequireScope is middleware that returns 403 unless the request's Identity has scope. It must run after Middleware.

type Config

type Config struct {
	Store  Store
	Header string // default "X-API-Key"
}

Config configures an Authenticator.

type Identity

type Identity struct {
	ID     string
	Scopes []string
	Meta   map[string]any
}

Identity is who an API key belongs to.

func FromContext

func FromContext(c *fiber.Ctx) (*Identity, bool)

FromContext returns the authenticated identity stored by Middleware.

type Store

type Store interface {
	Lookup(ctx context.Context, keyHash string) (*Identity, error)
}

Store resolves a key's SHA-256 hash to an Identity. It returns (nil, nil) when the key is unknown.

func MapStore

func MapStore(m map[string]Identity) Store

MapStore returns an in-memory Store keyed by key hash (use Hash to compute the keys). The map is copied.

Jump to

Keyboard shortcuts

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