auth

package
v0.0.0-...-28a0867 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleKey     = "role"
	UserNameKey = "user_name"
)

Variables

This section is empty.

Functions

func GetRole

func GetRole(ctx context.Context) (string, bool)

func GetUserID

func GetUserID(ctx context.Context) (entity.UserID, bool)

func IsAdmin

func IsAdmin(ctx context.Context) bool

func SetRole

func SetRole(ctx context.Context, tok jwt.Token) context.Context

func SetUserID

func SetUserID(ctx context.Context, userID entity.UserID) context.Context

Types

type JWTer

type JWTer struct {
	PrivateKey, PublicKey jwk.Key
	Store                 Store
	Clocker               clock.Clocker
}

func NewJWTer

func NewJWTer(s Store, c clock.Clocker) (*JWTer, error)

func (*JWTer) FillContext

func (j *JWTer) FillContext(r *http.Request) (*http.Request, error)

func (*JWTer) GenerateToken

func (j *JWTer) GenerateToken(ctx context.Context, u entity.User) ([]byte, error)

func (*JWTer) GetToken

func (j *JWTer) GetToken(ctx context.Context, r *http.Request) (jwt.Token, error)

type Store

type Store interface {
	Save(ctx context.Context, key string, userID entity.UserID) error
	Load(ctx context.Context, key string) (entity.UserID, error)
}

type StoreMock

type StoreMock struct {
	// LoadFunc mocks the Load method.
	LoadFunc func(ctx context.Context, key string) (entity.UserID, error)

	// SaveFunc mocks the Save method.
	SaveFunc func(ctx context.Context, key string, userID entity.UserID) error
	// contains filtered or unexported fields
}

StoreMock is a mock implementation of Store.

func TestSomethingThatUsesStore(t *testing.T) {

	// make and configure a mocked Store
	mockedStore := &StoreMock{
		LoadFunc: func(ctx context.Context, key string) (entity.UserID, error) {
			panic("mock out the Load method")
		},
		SaveFunc: func(ctx context.Context, key string, userID entity.UserID) error {
			panic("mock out the Save method")
		},
	}

	// use mockedStore in code that requires Store
	// and then make assertions.

}

func (*StoreMock) Load

func (mock *StoreMock) Load(ctx context.Context, key string) (entity.UserID, error)

Load calls LoadFunc.

func (*StoreMock) LoadCalls

func (mock *StoreMock) LoadCalls() []struct {
	Ctx context.Context
	Key string
}

LoadCalls gets all the calls that were made to Load. Check the length with:

len(mockedStore.LoadCalls())

func (*StoreMock) Save

func (mock *StoreMock) Save(ctx context.Context, key string, userID entity.UserID) error

Save calls SaveFunc.

func (*StoreMock) SaveCalls

func (mock *StoreMock) SaveCalls() []struct {
	Ctx    context.Context
	Key    string
	UserID entity.UserID
}

SaveCalls gets all the calls that were made to Save. Check the length with:

len(mockedStore.SaveCalls())

Jump to

Keyboard shortcuts

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