models

package
v0.0.0-...-a57f64e Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation     = "activation"
	ScopeAuthentication = "authentication"
)

Define constants for the token scope. For now we just define the scope "activation" but we'll add additional scopes later in the book.

Variables

View Source
var AnonymousUser = &User{}

Declare a new AnonymousUser variable.

View Source
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")

Define an error that our UnmarshalJSON() method can return if we're unable to parse or convert the JSON string successfully.

Functions

This section is empty.

Types

type Movie

type Movie struct {
	ID        int64     `json:"id"`
	CreatedAt time.Time `json:"-"`
	Title     string    `json:"title"`
	Year      int32     `json:"year,omitempty"`
	Runtime   Runtime   `json:"runtime,omitempty"`
	Genres    []string  `json:"genres,omitempty"`
	Version   int32     `json:"version"`
}

type Password

type Password struct {
	Plaintext *string
	Hash      []byte
}

Create a custom password type which is a struct containing the plaintext and hashed versions of the password for a user. The plaintext field is a *pointer* to a string, so that we're able to distinguish between a plaintext password not being present in the struct at all, versus a plaintext password which is the empty string "".

func (*Password) Matches

func (p *Password) Matches(plaintextPassword string) (bool, error)

The Matches() method checks whether the provided plaintext password matches the hashed password stored in the struct, returning true if it matches and false otherwise.

func (*Password) Set

func (p *Password) Set(plaintextPassword string) error

The Set() method calculates the bcrypt hash of a plaintext password, and stores both the hash and the plaintext versions in the struct.

type Permissions

type Permissions []string

func (Permissions) Include

func (p Permissions) Include(code string) bool

Add a helper method to check whether the Permissions slice contains a specific permission code.

type Runtime

type Runtime int32

Declare a custom Runtime type, which has the underlying type int32 (the same as our Movie struct field).

func (Runtime) MarshalJSON

func (r Runtime) MarshalJSON() ([]byte, error)

Implement a MarshalJSON() method on the Runtime type so that it satisfies the json.Marshaler interface. This should return the JSON-encoded value for the movie runtime (in our case, it will return a string in the format "<runtime> mins").

func (*Runtime) UnmarshalJSON

func (r *Runtime) UnmarshalJSON(jsonValue []byte) error

Implement a UnmarshalJSON() method on the Runtime type so that it satisfies the json.Unmarshaler interface. IMPORTANT: Because UnmarshalJSON() needs to modify the receiver (our Runtime type), we must use a pointer receiver for this to work correctly. Otherwise, we will only be modifying a copy (which is then discarded when this method returns).

type Token

type Token struct {
	Plaintext string    `json:"token"`
	Hash      []byte    `json:"-" db:hash`
	UserID    int64     `json:"-" db:user_id`
	Expiry    time.Time `json:"expiry" db:expiry`
	Scope     string    `json:"-" db:scope`
}

Define a Token struct to hold the data for an individual token. This includes the plaintext and hashed versions of the token, associated user ID, expiry time and scope.

func GenerateToken

func GenerateToken(userID int64, ttl time.Duration, scope string) (*Token, error)

type User

type User struct {
	ID        int64     `json:"id"`
	CreatedAt time.Time `json:"created_at"`
	Name      string    `json:"name"`
	Email     string    `json:"email"`
	Password  `json:"-"`
	Activated bool `json:"activated"`
	Version   int  `json:"-"`
}

Define a User struct to represent an individual user. Importantly, notice how we are using the json:"-" struct tag to prevent the Password and Version fields appearing in any output when we encode it to JSON. Also notice that the Password field uses the custom password type defined below.

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

Check if a User instance is the AnonymousUser.

Jump to

Keyboard shortcuts

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