token

package
v0.0.0-...-a7c217f Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const TokenLen = 32

Variables

View Source
var (
	ErrInvalidToken     = errors.New("invalid token")
	ErrInvalidNamespace = errors.New("invalid namespace")
)

Functions

This section is empty.

Types

type Mock

type Mock struct {
	mock.Mock
}

Mock implementation of Service.

func NewMock

func NewMock(t *testing.T) *Mock

NewMock instantiates a new Mock.

func (*Mock) GenerateAndSave

func (m *Mock) GenerateAndSave(db prefixer.Prefixer, op Operation, resource string, lifetime time.Duration) (string, error)

GenerateAndSave mock method.

func (*Mock) Validate

func (m *Mock) Validate(db prefixer.Prefixer, op Operation, resource, token string) error

Validate mock method.

type Operation

type Operation string
var (
	EmailUpdate Operation = "email_update"
	MagicLink   Operation = "magic_link"
)

type Service

type Service interface {
	GenerateAndSave(db prefixer.Prefixer, op Operation, resource string, lifetime time.Duration) (string, error)
	Validate(db prefixer.Prefixer, op Operation, resource, token string) error
}

Service used to generate token to protect and authenticate an operation outside cozy.

This service is used to protect unauthenticated operation done oustide the stack. For example when we need to validate an email with an URL link. Without this random token inside the url anyone could validate the email for anyone as user come unauthenticated.

type TokenService

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

TokenService is a Service implementation based on cache.Cache.

Note: Depending on the cache implementation setup the storage can be store in-memory a reboot would invalidate all the existing tokens.

This can be the case for the self-hosted stacks using cache.InMemory.

func NewService

func NewService(cache cache.Cache) *TokenService

NewService instantiates a new [CacheService].

func (*TokenService) GenerateAndSave

func (s *TokenService) GenerateAndSave(db prefixer.Prefixer, op Operation, resource string, lifetime time.Duration) (string, error)

GenerateAndSave generate a random token and save it into the storage for the specified duration.

Once the lifetime is expired, the token is deleted and will be never valid.

func (*TokenService) Validate

func (s *TokenService) Validate(db prefixer.Prefixer, op Operation, resource, token string) error

Validate will validate that the user as a matching token.

If the token doesn't match any content or an expired content the error ErrInvalidToken is returned.

Jump to

Keyboard shortcuts

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