model

package
v0.0.0-...-b9a316c Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package model defines all the models.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DummyCheckPassword

func DummyCheckPassword()

DummyCheckPassword is a fake password check to prevent timing attacks.

func SortAuths

func SortAuths(aa []*Auth)

SortAuths sorts the given auths in reverse order based on their method (so the password method is last).

Types

type Auth

type Auth struct {
	ID     string   `json:"id" valid:"-"`
	Method Method   `json:"method" valid:"-"`
	Values []string `json:"values" valid:"-"`
}

Auth represents a form of authentication for the link.

func NewAuth

func NewAuth(method string, values string) (*Auth, error)

NewAuth creates a new auth, converting and validating the method and values.

func (*Auth) Validate

func (a *Auth) Validate() error

Validate validates the auth.

type Link struct {
	Slug    string    `json:"slug" valid:"stringlength(6|10),required"`
	Title   string    `json:"title" valid:"required"`
	URL     string    `json:"url" valid:"url,required"`
	Created time.Time `json:"created" valid:"-"`
	Count   int       `json:"count" valid:"-"`
	AuthIDs []string  `json:"auth_ids" valid:"-"`
}

Link represents a shortened link.

func NewLink(url string) (*Link, error)

NewLink creates a new link with the given URL.

Title generation is attempted, by visiting the URL and extracting the <title>. The link will default to 'Untitled' if this does not succeed.

func (*Link) Validate

func (l *Link) Validate() error

Validate validates the link.

type Method

type Method int

Method is a method of authentication (e.g. Google, GitHub or password).

const (
	MethodPassword Method = iota // password (stored in plaintext)
	MethodGoogle                 // email
	MethodJabba                  // username
)

Supported methods of authentication.

type User

type User struct {
	Username   string    `json:"username" valid:"stringlength(2|20)"`
	Registered bool      `json:"registered" valid:"-"`
	Email      string    `json:"email" valid:"email,required"`
	Password   string    `json:"password" valid:"-"`
	Joined     time.Time `json:"joined" valid:"-"`
	LastVisit  time.Time `json:"last_visit" valid:"-"`
	LinkSlugs  []string  `json:"link_slugs" valid:"-"`
}

User represents a registered user.

func NewAnonymousUser

func NewAnonymousUser() *User

NewAnonymousUser creates a new anonymous user.

Username is a random alphabetic string.

func NewUser

func NewUser(username string, email string) (*User, error)

NewUser creates a new user.

func (*User) CheckPassword

func (u *User) CheckPassword(password string) error

CheckPassword checks whether the given password is correct.

func (*User) FindLinkSlug

func (u *User) FindLinkSlug(slug string) (int, bool)

FindLinkSlug searches for a slug that belongs to the user and returns its index and flag to indicate whether it exists.

func (*User) SetPassword

func (u *User) SetPassword(password string, confirmPassword string) error

SetPassword ensures the passwords are equal, generates the hash and then sets it on the user.

func (*User) Validate

func (u *User) Validate() error

Validate validates the user.

Jump to

Keyboard shortcuts

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