db

package
v0.0.0-...-44b14cd Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package db defines the requirements for our database, via the Store interface, and implements it for postgres.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUsernameExists is returned when the unique requirement of a username is violated
	ErrAlreadyExists = errors.New("DB: Username exists")
	// ErrNotFound is returned when the requested value isn't found
	ErrNotFound = errors.New("DB: Not Found")
)

Functions

This section is empty.

Types

type Group

type Group struct {
	Name string
	UUID string // should this be type uuid.UUID?
}

A Group is the unit of sharing - All of the users in the group can see the content shared with that group

type GroupMember

type GroupMember struct {
	Username string
	Admin    bool
}

type PGStore

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

A PGStore implements storage against PostGres

func NewPGStore

func NewPGStore(user, password, database string) (*PGStore, error)

NewPGStore connects to a postgresql database

func (*PGStore) AddGroup

func (pg *PGStore) AddGroup(u User, groupname string) error

AddGroup creates a group and adds the creating user to it as an admin

func (*PGStore) AddUser

func (pg *PGStore) AddUser(username string, hash []byte) error

AddUser adds a user to the users table of the database, hashing the password with bcrypt

func (*PGStore) ExecuteSchema

func (pg *PGStore) ExecuteSchema(filename string) error

ExecuteSchema runs all the sql commands in the given file to initialize the database

func (*PGStore) GetGroupByID

func (pg *PGStore) GetGroupByID(id string) (Group, []GroupMember, error)

GetGroupByID returns a group and a list of all its members

func (*PGStore) GetGroupsForUser

func (pg *PGStore) GetGroupsForUser(u User) ([]Group, error)

GetGroupsForUser returns all the groups a user can access

func (*PGStore) GetUserByName

func (pg *PGStore) GetUserByName(username string) (*User, error)

GetUserByName returns the DB user information for a user if that user exists

func (*PGStore) Query

func (pg *PGStore) Query(query string, args ...interface{}) (*sql.Rows, error)

Query executes a raw query against the DB and returns the result

func (*PGStore) RevokeSession

func (pg *PGStore) RevokeSession(session string) error

RevokeSession removes a user's session from the sessions table

func (*PGStore) SessionGet

func (pg *PGStore) SessionGet(session string, newExpiration time.Time) (*Session, error)

SessionGet checks the database for a session and returns it if found If the session is absent, an error is returned. SessionGet will not return an expired session.

func (*PGStore) UserAddSession

func (pg *PGStore) UserAddSession(user User, session string, expires time.Time) error

UserAddSession stores a session in the database

type Session

type Session struct {
	User    User
	Expires time.Time
}

A Session is a view into a session

func (Session) IsExpired

func (s Session) IsExpired() bool

IsExpired returns true if a session has expired

type Store

type Store interface {
	ExecuteSchema(filename string) error
	AddUser(username string, hash []byte) error
	GetUserByName(username string) (*User, error)
	UserAddSession(user User, session string, expires time.Time) error

	AddGroup(u User, name string) error
	GetGroupsForUser(u User) ([]Group, error)
	GetGroupByID(id string) (Group, []GroupMember, error)

	// SessionGet returns a valid session if one exists.
	// Guaranteed to not return expired sessions.
	// If a valid session is found, extend it! I don't recommend passing in a
	// time that's past, though.
	SessionGet(session string, newExpiration time.Time) (*Session, error)
	RevokeSession(session string) error
}

A Store provides the methods required to access the database.

type User

type User struct {
	Username string
	Hash     []byte
	// contains filtered or unexported fields
}

A User is a view into the details for a given user The intent is that the Username is how external users ask for a user, but in order to support changing a username, the DB should always store references to users by their UUID.

Jump to

Keyboard shortcuts

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