auth

package
v1.3.2-0...-4312943 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadParams     = errors.New("bad parameters")
	ErrInternalError = errors.New("internal error")
	ErrNotFound      = errors.New("not found")

	ErrStoreError = errors.New("internal store error")
	ErrOrgExists  = errors.New("org already exists")
	ErrUserExists = errors.New("user already token")

	ErrInvalidToken = errors.New("invalid token")
	ErrTokenExpired = errors.New("token expired")
)

Exported errors.

Functions

This section is empty.

Types

type Config

type Config struct {
	Provider         Provider
	TokenExpieration time.Duration
	TokenSecret      []byte
}

Config defines the auth manager config.

type Manager

type Manager struct {
	Provider
	TokenExpieration time.Duration
	TokenSecret      []byte
}

Manager manages the authentication.

func New

func New(config Config) *Manager

New returns an auth manager with provider.

func (*Manager) IsOrgExists

func (m *Manager) IsOrgExists(ctx context.Context, org string) (
	exists bool, err error)

IsOrgExists returns if the org exists.

func (*Manager) IsUserExists

func (m *Manager) IsUserExists(ctx context.Context, email string) (
	exists bool, err error)

IsUserExists returns if the user exists.

func (*Manager) Login

func (m *Manager) Login(ctx context.Context, email string, password string) (
	token *Token, err error)

Login verify the user's email, password and returns the access token.

func (*Manager) Logout

func (m *Manager) Logout(ctx context.Context, token string) (err error)

Logout deletes the access token.

func (*Manager) RegisterOrg

func (m *Manager) RegisterOrg(ctx context.Context, org Organization) (
	err error)

RegisterOrg creates the org if org doesn't exist and admin holds no other org.

func (*Manager) RegisterOrgWithAdmin

func (m *Manager) RegisterOrgWithAdmin(
	ctx context.Context, org Organization, admin User) (err error)

RegisterOrgWithAdmin creates the org and the admin user.

func (*Manager) RegisterUser

func (m *Manager) RegisterUser(ctx context.Context, user User) (err error)

RegisterUser creates the user, ensuring the user does not exist and the org does exist if user.Organization is not empty.

func (*Manager) SetUserOrg

func (m *Manager) SetUserOrg(ctx context.Context, email, org string) error

SetUserOrg sets the org of user with email.

func (*Manager) Verify

func (m *Manager) Verify(ctx context.Context, tokenStr string) (
	claims *TokenClaims, err error)

Verify verifies the access token.

type Organization

type Organization struct {
	Name       string
	AdminEmail string
}

Organization defines the org model.

type Provider

type Provider interface {
	fmt.Stringer

	// users
	GetUser(ctx context.Context, email string) (User, error)
	GetUsers(ctx context.Context) ([]string, error)
	SetUser(ctx context.Context, user User) error
	DeleteUser(ctx context.Context, email string) error

	// organization
	GetOrg(ctx context.Context, name string) (Organization, error)
	GetOrgUsers(ctx context.Context, name string) ([]string, error)
	SetOrg(ctx context.Context, org Organization) error
	DeleteOrg(ctx context.Context, name string) error

	// tokens
	GetToken(ctx context.Context, token string) (Token, error)
	SetToken(ctx context.Context, token Token) error
	DeleteToken(ctx context.Context, token string) error
}

Provider defines the auth provider interface.

type Token

type Token struct {
	JWT string
}

Token defines the token model.

func NewToken

func NewToken(user User, secret []byte, expiration time.Duration) (
	token *Token, err error)

NewToken generates a new signed JWT token.

type TokenClaims

type TokenClaims struct {
	Email     string `json:"email"`
	Org       string `json:"org"`
	IssuedAt  int64  `json:"issued_at"`
	ExpiredAt int64  `json:"expired_at"`
}

TokenClaims defines the token claims struct.

func (*TokenClaims) Valid

func (c *TokenClaims) Valid() error

Valid implements the JWT interface.

type User

type User struct {
	Email        string
	PasswordHash []byte
	Organization string
}

User defines the user model.

func NewUser

func NewUser(email, password, org string) (*User, error)

NewUser returns a user with email, pwd and org.

func (*User) SetPassword

func (u *User) SetPassword(pwd string) error

SetPassword sets password hash with bcrypt.

func (*User) VerifyPassword

func (u *User) VerifyPassword(pwd string) error

VerifyPassword verifies password with bcrypt.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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