database

package
v0.3.0-beta Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseHandler

type DatabaseHandler struct {
	sync.RWMutex
	Users *UserTable      `json:"users"`
	Pmods map[string]pmod `json:"pmods"`
	// contains filtered or unexported fields
}

userSessions should be indexed by cookie value to prevent users from being logged out by overwrites

func CreateDatabaseHandler

func CreateDatabaseHandler() *DatabaseHandler

func (*DatabaseHandler) CreateSession

func (db *DatabaseHandler) CreateSession(username string, permissionLevel int) (string, error)

Returns the cookie value and an error value

func (*DatabaseHandler) CreateUser

func (db *DatabaseHandler) CreateUser(username string, password string, permissionLevel int) error

Adds user to in memory database and triggers a file dump to store state

func (*DatabaseHandler) DeleteSession

func (db *DatabaseHandler) DeleteSession(cookieValue string)

func (*DatabaseHandler) DeleteUser

func (db *DatabaseHandler) DeleteUser(username string) error

Delete user from memory database and triggers a file dump to store state

func (*DatabaseHandler) GetUserList

func (db *DatabaseHandler) GetUserList() []string

func (*DatabaseHandler) InitEmptyDatabase

func (db *DatabaseHandler) InitEmptyDatabase()

func (*DatabaseHandler) IsValidCredentials

func (db *DatabaseHandler) IsValidCredentials(username string, password string) (bool, int)

func (*DatabaseHandler) LoadDatabase

func (db *DatabaseHandler) LoadDatabase(file string) error

Loads existing database from JSON file

func (*DatabaseHandler) LoginUserHandler

func (db *DatabaseHandler) LoginUserHandler(w http.ResponseWriter, r *http.Request)

HTTP Handler for login route

func (*DatabaseHandler) SaveDatabase

func (db *DatabaseHandler) SaveDatabase() error

func (*DatabaseHandler) ValidateSession

func (db *DatabaseHandler) ValidateSession(cookieValue string) (bool, int)

Checks database for session Purges expired sessions if value is found in database but is expired. This is kind of a lazy approach to cleaning the database. A secondary trigger is needed to prevent the database from exploding in size in ideal operating conditions (when all cookies are time valid).

type SessionsTable

type SessionsTable struct {
	sync.RWMutex
	Sessions map[string]session

	SessionLifetime int64
}

func CreateSessionsTable

func CreateSessionsTable() *SessionsTable

func (*SessionsTable) CleanSessions

func (st *SessionsTable) CleanSessions()

Removes expired sessions from the database Called when a session value queried is found to be invalid and once an hour

func (*SessionsTable) CreateSession

func (st *SessionsTable) CreateSession(username string, permissionLevel int) (string, error)

func (*SessionsTable) DeleteSession

func (st *SessionsTable) DeleteSession(cookieValue string)

func (*SessionsTable) ValidateSession

func (st *SessionsTable) ValidateSession(cookieValue string) (bool, int)

Returns a bool for session validity and returns permission level of found session.

type UserTable

type UserTable struct {
	sync.RWMutex
	Users map[string]user `json:"users"`
}

func CreateUserTable

func CreateUserTable() *UserTable

func (*UserTable) CreateUser

func (ut *UserTable) CreateUser(username string, password string, permissionLevel int) error

func (*UserTable) DeleteUser

func (ut *UserTable) DeleteUser(username string) error

func (*UserTable) GetUserList

func (ut *UserTable) GetUserList() []string

func (*UserTable) IsValidCredentials

func (ut *UserTable) IsValidCredentials(username string, password string) (bool, int)

func (*UserTable) MarshalJSON

func (ut *UserTable) MarshalJSON() ([]byte, error)

This is critical for making the database JSON Marshalable

func (*UserTable) UnmarshalJSON

func (ut *UserTable) UnmarshalJSON(data []byte) error

This is critical for making the database JSON Unmarshalable

Jump to

Keyboard shortcuts

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