auth

package
v0.0.0-...-5d7b32d Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyTypeUUIDString   = "uuid"
	KeyTypeHex16String  = "hex16"
	KeyTypeHex32String  = "hex32"
	KeyTypeHex64String  = "hex64"
	KeyTypeHex128String = "hex128"
	KeyTypeHex256String = "hex256"
)

string key types

Variables

View Source
var (
	ErrCheckingAuth = errors.New("unable to check auth")
	ErrUnauthorized = errors.New("unauthorized")
	ErrInvalidKey   = errors.New("invalid key")
)

Functions

func GenerateKey

func GenerateKey(keyType KeyType) (string, error)

func IsValidAuthLevel

func IsValidAuthLevel(lvl AuthLevel) bool

func NewSqlDB

func NewSqlDB(uri string) *bun.DB

Types

type AuthLevel

type AuthLevel int

auth levels

const (
	AuthLevelUnauthorized AuthLevel = iota
	AuthLevelBasic
	AuthLevelMaster
)

type AuthProvider

type AuthProvider string
const (
	AuthProviderNoAuth AuthProvider = "noauth"
	AuthProviderMemory AuthProvider = "memory"
	AuthProviderSql    AuthProvider = "sql"
)

func ToProvider

func ToProvider(s string) AuthProvider

type CtxAuthKey

type CtxAuthKey int

context key for middleware

const AuthKey CtxAuthKey = 0

type KeyType

type KeyType int

key types

const (
	KeyTypeUUID KeyType = iota
	KeyTypeHex16
	KeyTypeHex32
	KeyTypeHex64
	KeyTypeHex128
	KeyTypeHex256
)

func ToKeyType

func ToKeyType(s string) KeyType

take string of key type to int KeyType

type KeyUsage

type KeyUsage struct {
	IP          string
	AccessedAt  int64
	Requests    int64
	MethodUsage map[string]int64
}

type MemoryProvider

type MemoryProvider struct {
	nocopy.NoCopy

	// keys is a map of API keys to their corresponding secret.
	Keys map[string]*memoryKey
	// contains filtered or unexported fields
}

func NewMemoryProvider

func NewMemoryProvider() *MemoryProvider

func (*MemoryProvider) Authenticate

func (a *MemoryProvider) Authenticate(r *http.Request) (AuthLevel, error)

func (*MemoryProvider) Register

func (a *MemoryProvider) Register() (string, error)

func (*MemoryProvider) UpdateUsage

func (a *MemoryProvider) UpdateUsage(key string, usageDelta KeyUsage) error

func (*MemoryProvider) WithDefaultExpiry

func (a *MemoryProvider) WithDefaultExpiry(defaultExpiry time.Duration) *MemoryProvider

func (*MemoryProvider) WithKeyType

func (a *MemoryProvider) WithKeyType(keyType KeyType) *MemoryProvider

type NoAuthProvider

type NoAuthProvider struct{}

func NewNoAuthProvider

func NewNoAuthProvider() *NoAuthProvider

func (*NoAuthProvider) Authenticate

func (a *NoAuthProvider) Authenticate(r *http.Request) (AuthLevel, error)

with no auth, highest auth level is defualt

func (*NoAuthProvider) Register

func (a *NoAuthProvider) Register() (string, error)

func (*NoAuthProvider) UpdateUsage

func (a *NoAuthProvider) UpdateUsage(key string, usageDelta KeyUsage) error

type Provider

type Provider interface {
	// Authenticate authenticates a request.
	Authenticate(r *http.Request) (AuthLevel, error)
	Register() (key string, err error)
	UpdateUsage(key string, usageDelta KeyUsage) error
}

type SqlAuthProvider

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

func NewSqlAuthProvider

func NewSqlAuthProvider(db *bun.DB) *SqlAuthProvider

func (*SqlAuthProvider) Authenticate

func (a *SqlAuthProvider) Authenticate(r *http.Request) (AuthLevel, error)

func (*SqlAuthProvider) Register

func (a *SqlAuthProvider) Register() (string, error)

func (*SqlAuthProvider) UpdateUsage

func (a *SqlAuthProvider) UpdateUsage(key string, usageDelta KeyUsage) error

func (*SqlAuthProvider) WithKeyType

func (a *SqlAuthProvider) WithKeyType(keyType KeyType) *SqlAuthProvider

Jump to

Keyboard shortcuts

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