Documentation ¶
Index ¶
- Constants
- Variables
- func CreateJWT(userID int64, username, name, secret string, isAdmin bool) (string, error)
- func DeleteDB(filename string)
- func GenDBFolder() string
- func IsErrNotUnique(err error) bool
- func RefreshJWT(claims *Claims, secret string) (string, error)
- func SetAuthSecret(secret string) error
- func SetDBRoot(rootDir string) error
- func TokensMigrate(db *SqliteDB) error
- func UserGroupsMigrate(db *SqliteDB) error
- type AuthDB
- type AuthMethodData
- type Cache
- func (c *Cache) Delete(key string)
- func (c *Cache) Get(key string) (interface{}, bool)
- func (c *Cache) GetAndRefresh(key string) (interface{}, bool)
- func (c *Cache) Refresh(key string) error
- func (c *Cache) Set(key string, value interface{})
- func (c *Cache) SetWithExpire(key string, value interface{}, expire time.Duration)
- type Claims
- type CuttleDB
- type SqliteDB
- func (db *SqliteDB) AuthMigrate() error
- func (db *SqliteDB) Close() error
- func (db *SqliteDB) CuttleMigrate() error
- func (db *SqliteDB) Exec(query string, args ...any) (sql.Result, error)
- func (db *SqliteDB) IsUnique(table string, where string, args ...any) error
- func (db *SqliteDB) Open() error
- func (db *SqliteDB) Query(query string, args ...any) (*sql.Rows, error)
- func (db *SqliteDB) QueryRow(query string, args ...any) (*sql.Row, error)
- func (db *SqliteDB) TokenClean() error
- func (db *SqliteDB) TokenCreate(userID int64, username, name string, isAdmin bool) (string, error)
- func (db *SqliteDB) TokenDelete(bearer string) error
- func (db *SqliteDB) TokenGet(bearer string) (*Claims, error)
- func (db *SqliteDB) TokenUpdate(bearer string, claims *Claims) error
- func (db *SqliteDB) UserCreate(username, name, pwHash, groups string) (UserData, error)
- func (db *SqliteDB) UserDelete(id int64) error
- func (db *SqliteDB) UserGet(id int64) (UserData, error)
- func (db *SqliteDB) UserGetByUsername(username string) (UserData, error)
- func (db *SqliteDB) UserGroupCreate(name, members, profiles string) (UserGroupData, error)
- func (db *SqliteDB) UserGroupDelete(id int64) error
- func (db *SqliteDB) UserGroupGet(id int64) (UserGroupData, error)
- func (db *SqliteDB) UserGroupGetByName(name string) (UserGroupData, error)
- func (db *SqliteDB) UserGroupGetGroups(gids []int64) ([]UserGroupData, error)
- func (db *SqliteDB) UserGroupIsUnique(name string) error
- func (db *SqliteDB) UserGroupUpdate(data UserGroupData) (UserGroupData, error)
- func (db *SqliteDB) UserIsUnique(username string) error
- func (db *SqliteDB) UserUpdate(user UserData) (UserData, error)
- func (db *SqliteDB) UsersMigrate() error
- type TokenData
- type UserData
- type UserGroupData
Constants ¶
const ( CACHE_DEFAULT_EXPIRE = 5 * time.Minute CACHE_DEFAULT_PURGE = 10 * time.Minute )
const ( JWT_DEFAULT_PATH = "/" JWT_DEFAULT_SESSION_EXPIRES = time.Hour * 3 JWT_DEFAULT_MAX_SESSION_EXPIRES = time.Hour * 6 JWT_COOKIE_NAME = "session_token" )
const (
DefaultDBFolder = "db"
)
Variables ¶
var ( ErrRecordExists = fmt.Errorf("record exists") // Invalid parameters ErrInvalidID = fmt.Errorf("invalid ID") ErrInvalidAuthType = fmt.Errorf("invalid auth type") ErrInvalidPassphrase = fmt.Errorf("invalid passphrase") // Users ErrUserNotFound = fmt.Errorf("user not found") ErrUserExists = fmt.Errorf("user exists") // User Groups ErrUserGroupNotFound = fmt.Errorf("user group not found") ErrUserGroupExists = fmt.Errorf("user group exists") // Tokens ErrTokenNotFound = fmt.Errorf("token not found") ErrTokenExpired = fmt.Errorf("token has expired") )
var ( TestDBRoot = "/tmp/cuttle/db" TestCuttleDBName = "test_cuttle.db" TestAuthDBName = "test_auth.db" )
var ( ErrExpiredCookie = fmt.Errorf("cookie has expired") ErrSessionExpired = fmt.Errorf("session has expired") )
var ErrKeyNotFound = fmt.Errorf("key not found")
Functions ¶
func GenDBFolder ¶
func GenDBFolder() string
func IsErrNotUnique ¶
IsErrNotUnique checks if the error is due to a unique constraint violation.
func SetAuthSecret ¶
SetAuthSecret sets the secret used to sign JWTs.
func SetDBRoot ¶
SetDBRoot sets the root directory for the database. If this is not set, the default behavior is to create a directory called "db" in the current working directory. Example:
db.InitDB("/path/to/db")
Expected Behavior:
db.InitDB("/tmp/db") db.NewSqliteDB("mydb.db") db.Open() `ls /tmp/db/` mydb.db
func TokensMigrate ¶
UserGroupsMigrate creates the 'user_groups' table if it does not exist.
func UserGroupsMigrate ¶
UserGroupsMigrate creates the 'user_groups' table if it does not exist.
Types ¶
type AuthDB ¶
type AuthDB interface { Open() error AuthMigrate() error Close() error // AddRepo(file, alias string, migrate migrater) error // Attach(filename, alias string) error // Users UserIsUnique(username string) error UserCreate(username, name, password, groups string) (UserData, error) UserGet(id int64) (UserData, error) UserGetByUsername(username string) (UserData, error) UserUpdate(user UserData) (UserData, error) UserDelete(id int64) error // UserGroups UserGroupIsUnique(name string) error UserGroupCreate(name, members, profiles string) (UserGroupData, error) UserGroupGet(id int64) (UserGroupData, error) UserGroupGetByName(name string) (UserGroupData, error) UserGroupGetGroups(ids []int64) ([]UserGroupData, error) UserGroupUpdate(group UserGroupData) (UserGroupData, error) UserGroupDelete(id int64) error // Tokens TokenCreate(userID int64, username, name string, isAdmin bool) (string, error) TokenGet(bearer string) (*Claims, error) TokenUpdate(bearer string, claims *Claims) error TokenDelete(bearer string) error }
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a wrapper around the go-cache library.
func DefaultCache ¶
func DefaultCache() *Cache
DefaultCache returns a new Cache instance with default expiration and purge times.
func NewCache ¶
NewCache returns a new Cache instance with the specified expiration and purge times.
func (*Cache) GetAndRefresh ¶
Get the value for the key and refresh the expiration time to the instance's DefaultExpiration.
func (*Cache) Refresh ¶
Refresh the expiration time for the key to the instance's DefaultExpiration.
type Claims ¶
type SqliteDB ¶
type SqliteDB struct { Name string // DB file name. *sql.DB // contains filtered or unexported fields }
SqliteDB is a wrapper around the sqlite3 database. It also holds the db filename and context.
func NewSqliteDB ¶
NewSqliteDB creates a new Sqlite3 DB instance.
func TestSqliteAuthDBSetup ¶
TestSqliteAuthDBSetup creates a new SqliteDB instance for testing. You will still need to run AuthMigrate.
func TestSqliteCuttleDBSetup ¶
TestSqliteCuttleDBSetup creates a new SqliteDB instance for testing. You will still need to run CuttleMigrate.
func (*SqliteDB) AuthMigrate ¶
AuthMigrate runs the migrations for each table in the auth database.
func (*SqliteDB) CuttleMigrate ¶
CuttleMigrate runs the migrations for each table in the main cuttle database.
func (*SqliteDB) IsUnique ¶
IsUnique returns nil if no records exist in the table that match the where clause. If a record exists, it returns an ErrRecordExists error. The 'where' clause should not include the "WHERE" keyword but may include multiple column queries which are comma separated: 'col1 = ?, col2 = ?'. The 'args' are the values to be used in the where clause.
Example: db.IsUnique("users", "username = ?", "myusername")
func (*SqliteDB) Open ¶
Open opens the database with WAL and foreign_keys enabled. Open then pings the database. If the database does not exist, it will be created.
func (*SqliteDB) TokenClean ¶
func (*SqliteDB) TokenCreate ¶
func (*SqliteDB) TokenDelete ¶
func (*SqliteDB) UserCreate ¶
UserCreate creates a new user in the database and returns the new user data. A password should never be provided in plain text. UserCreate will check for hash formatting.
func (*SqliteDB) UserDelete ¶
UserDelete deletes a user from the database by ID.
func (*SqliteDB) UserGetByUsername ¶
UserGetByUsername retrieves a user from the database by username.
func (*SqliteDB) UserGroupCreate ¶
func (db *SqliteDB) UserGroupCreate(name, members, profiles string) (UserGroupData, error)
Create a new user group. Returns the new user group data.
Members should be a JSON array of user IDs. An ID of 0 is invalid: `[1,5,28,349]`
Profiles should be a JSON object of profile names and permissions: `{"profile_id": {"method": bool, ...}}`
Profiles Example: {124: {"POST": false, "GET": true, "PUT": false, "DELETE": false}, 5462: {"POST": false, "GET": true, "PUT": true, "DELETE": false}}
func (*SqliteDB) UserGroupDelete ¶
UserGroupDelete deletes a user group from the database by ID.
func (*SqliteDB) UserGroupGet ¶
func (db *SqliteDB) UserGroupGet(id int64) (UserGroupData, error)
UserGroupGet retrieves a user group from the database by ID.
func (*SqliteDB) UserGroupGetByName ¶
func (db *SqliteDB) UserGroupGetByName(name string) (UserGroupData, error)
UserGroupGetByName retrieves a user group from the database by name.
func (*SqliteDB) UserGroupGetGroups ¶
func (db *SqliteDB) UserGroupGetGroups(gids []int64) ([]UserGroupData, error)
UserGroupGetGroups retrieves multiple user groups from the database by ID.
func (*SqliteDB) UserGroupIsUnique ¶
UserGroupIsUnique checks if the user group name is unique in the database. If the name is not unique, it returns an ErrUserGroupExists error.
func (*SqliteDB) UserGroupUpdate ¶
func (db *SqliteDB) UserGroupUpdate(data UserGroupData) (UserGroupData, error)
UserGroupUpdate updates a user group in the database and returns the updated user group data.
func (*SqliteDB) UserIsUnique ¶
UserIsUnique checks if the username is unique in the database. If the username is not unique, it returns an ErrUserExists error.
func (*SqliteDB) UserUpdate ¶
UserUpdate updates a user in the database and returns the updated user data.
func (*SqliteDB) UsersMigrate ¶
UsersMigrate creates the 'users' table if it does not exist.
type TokenData ¶
type TokenData struct { Bearer string JWT string // Group name. Created time.Time // Time created. Expires time.Time // Time last updated. }
UserGroupData represents a user group in the database.
type UserData ¶
type UserData struct { ID int64 Username string // Username to login with. Name string // Name to show in app. Hash string // Hashed password. Groups string // JSON string of group IDs. Empty should be "[]". IsAdmin bool // Is the user an admin. Created time.Time // Time created. Updated time.Time // Time last updated. }
UserData represents a user in the database.
type UserGroupData ¶
type UserGroupData struct { ID int64 Name string // Group name. Members string // JSON string of user IDs. Empty should be "[]". "[1,5,28,349]" Profiles string // JSON string of profile IDs. Empty should be "{}". "{profile_id:{method: bool...}". Created time.Time // Time created. Updated time.Time // Time last updated. }
UserGroupData represents a user group in the database.