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 ¶
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 ¶
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 ¶
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