memoryauth

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memoryauth provides an in-memory auth.UserDriver for tests, local development and single-process applications.

Seed users with Driver.Put, mark them with Driver.Disable and remove them with Driver.Delete. Find returns auth.ErrInvalidSubject for an invalid subject, auth.ErrUserNotFound for an unknown one and auth.ErrUserDisabled for a disabled user. Claims maps are copied on the way in and out, so callers cannot change stored state.

Example:

driver := memoryauth.New()
if err := driver.Put(auth.UserRecord{Subject: "42"}); err != nil {
	return err
}
users, err := auth.NewUserProvider(driver)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

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

Driver is an in-memory auth.UserDriver. The zero value is not usable; create it with New. It is safe for concurrent use.

Example:

driver := memoryauth.New()
_ = driver.Put(auth.UserRecord{Subject: "42", Claims: map[string]any{"role": "admin"}})
users, err := auth.NewUserProvider(driver)
Example
package main

import (
	"context"
	"fmt"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/memoryauth"
)

func main() {
	driver := memoryauth.New()
	_ = driver.Put(auth.UserRecord{Subject: "42", Claims: map[string]any{"role": "admin"}})

	users, _ := auth.NewUserProvider(driver)
	record, err := users.Find(context.Background(), "42")

	fmt.Println(record.Claims["role"], err)
}
Output:
admin <nil>

func New

func New() *Driver

New returns an empty driver.

Example:

driver := memoryauth.New()
Example
package main

import (
	"context"
	"errors"
	"fmt"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/memoryauth"
)

func main() {
	driver := memoryauth.New()

	_, err := driver.Find(context.Background(), "42")

	fmt.Println(errors.Is(err, auth.ErrUserNotFound))
}
Output:
true

func (*Driver) Delete

func (driver *Driver) Delete(subject string)

Delete removes the user. Deleting a missing user succeeds.

Example:

driver.Delete("42")
Example
package main

import (
	"context"
	"errors"
	"fmt"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/memoryauth"
)

func main() {
	driver := memoryauth.New()
	_ = driver.Put(auth.UserRecord{Subject: "42"})
	driver.Delete("42")

	_, err := driver.Find(context.Background(), "42")

	fmt.Println(errors.Is(err, auth.ErrUserNotFound))
}
Output:
true

func (*Driver) Disable

func (driver *Driver) Disable(subject string) error

Disable marks an existing user disabled, so Driver.Find returns auth.ErrUserDisabled. It returns auth.ErrUserNotFound when no user has the subject.

Example:

err := driver.Disable("42")
Example
package main

import (
	"context"
	"errors"
	"fmt"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/memoryauth"
)

func main() {
	driver := memoryauth.New()
	_ = driver.Put(auth.UserRecord{Subject: "42"})
	_ = driver.Disable("42")

	_, err := driver.Find(context.Background(), "42")

	fmt.Println(errors.Is(err, auth.ErrUserDisabled))
}
Output:
true

func (*Driver) Find

func (driver *Driver) Find(ctx context.Context, subject string) (auth.UserRecord, error)

Find implements auth.UserDriver. The returned Claims are a copy.

Example:

record, err := driver.Find(ctx, "42")
Example
package main

import (
	"context"
	"fmt"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/memoryauth"
)

func main() {
	driver := memoryauth.New()
	_ = driver.Put(auth.UserRecord{Subject: "42", Version: "v1"})

	record, err := driver.Find(context.Background(), "42")

	fmt.Println(record.Version, err)
}
Output:
v1 <nil>

func (*Driver) Put

func (driver *Driver) Put(record auth.UserRecord) error

Put stores record under its Subject, replacing any previous user and making the user enabled. The Claims map is copied; Value is stored as is. It returns an error wrapping auth.ErrInvalidSubject when the subject is invalid.

Example:

err := driver.Put(auth.UserRecord{Subject: "42", Version: "v1"})
Example
package main

import (
	"errors"
	"fmt"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/memoryauth"
)

func main() {
	driver := memoryauth.New()

	fmt.Println(driver.Put(auth.UserRecord{Subject: "42", Version: "v1"}))
	fmt.Println(errors.Is(driver.Put(auth.UserRecord{Subject: ""}), auth.ErrInvalidSubject))
}
Output:
<nil>
true

Jump to

Keyboard shortcuts

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