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 ¶
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]
Types ¶
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator validates API keys against a Store.
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.