apputil

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyOAuthProfileToUser

func ApplyOAuthProfileToUser(u *User, p oauth.Profile)

ApplyOAuthProfileToUser applies an oauth proflie.

func DeleteExpiredSessions

func DeleteExpiredSessions(mgr *ModelManager) cron.Job

DeleteExpiredSessions returns the job to delete expired sessions.

The default cutoff is 14 days but we can tweak that a bit.

func MigrationGroups

func MigrationGroups() []*migration.Group

MigrationGroups are the included migrations for the tables this package requires. You can use this array directly or indirectly through `Migrations`.

func MigrationSuite

func MigrationSuite(opts ...migration.SuiteOption) *migration.Suite

MigrationSuite returns a fully formed migration suite you can use to apply the `MigrationGroups` directly.

func UpgradeHTTPS

func UpgradeHTTPS(action web.Action) web.Action

UpgradeHTTPS upgrades http connections to https with a redirect in typical settings.

We do so through `X-Forwarded-Proto` header sniffs because many reverse-proxy hosted services forward all traffic to http and add forwarded headers so we know what the context of the original request was.

Normally we'd run a second http server on 80 (in addition to 443) and do this redirect, but because of the specific nature of how many systems host "https" services on http ports this is what we need to do.

Types

type Auth

type Auth struct {
	BaseController
	Config               Config
	AuthedRedirectPath   string
	CreateUserListener   func(context.Context, *User) error
	FetchSessionListener func(context.Context, *web.Session) error
	OAuth                *oauth.Manager
	DB                   *ModelManager
}

Auth is the auth controller.

func (Auth) FetchSession

func (a Auth) FetchSession(ctx context.Context, sessionID string) (*web.Session, error)

FetchSession implements web.FetchSessionHandler.

func (Auth) LoginRedirect

func (a Auth) LoginRedirect(ctx web.Context) *url.URL

LoginRedirect implements web.LoginRedirectHandler.

func (Auth) PersistSession

func (a Auth) PersistSession(ctx context.Context, session *web.Session) error

PersistSession implements web.PersistSessionHandler.

func (Auth) Register

func (a Auth) Register(app *web.App)

Register adds the controller routes to the application.

func (Auth) RemoveSession

func (a Auth) RemoveSession(ctx context.Context, sessionID string) error

RemoveSession implements web.RemoveSessionHandler.

func (Auth) SessionTimeout

func (a Auth) SessionTimeout(_ *web.Session) time.Time

SessionTimeout implements session timeout provider.

type BaseController

type BaseController struct{}

BaseController holds useful common methods for controllers.

func (BaseController) GetUserID

func (bc BaseController) GetUserID(r web.Context) uuid.UUID

GetUserID gets a userID from a given web context.

You can use this as a proxy for determining if a session is authenticated or not.

type Config

type Config struct {
	configmeta.Meta `yaml:",inline"`

	Logger logutil.Config `yaml:"logger"`
	OAuth  oauth.Config   `yaml:"oauth"`
	Web    web.Config     `yaml:"web"`
	DB     db.Config      `yaml:"db"`
}

func (Config) GetDB

func (c Config) GetDB() db.Config

GetDB returns the db config.

This is required for `EntrypointConfigProvider`

func (Config) GetLogger

func (c Config) GetLogger() logutil.Config

GetLogger returns the logger config.

This is required for `EntrypointConfigProvider`

func (Config) GetMeta

func (c Config) GetMeta() configmeta.Meta

GetMeta returns the meta config.

This is required for `EntrypointConfigProvider`

func (*Config) Resolve

func (c *Config) Resolve(ctx context.Context) (err error)

Resolve resolves the config.

func (*Config) ResolveOAuthRedirectURL

func (c *Config) ResolveOAuthRedirectURL(ctx context.Context) (*string, error)

ResolveOAuthRedirectURL resolves the oauth redirect url if it's not set.

type DBEntryPoint

type DBEntryPoint[T DBEntryPointConfigProvider] struct {
	Setup   func(context.Context, T) error
	Migrate func(context.Context, T, *db.Connection) error
	Start   func(context.Context, T, *db.Connection) error
	// contains filtered or unexported fields
}

EntryPoint is a top level handler for a database backed app.

It handles reading the config, setting up the logger, opening the database connection, and based on comandline flags handling calling specific handlers for initializing the database and applying migrations on start.

func (*DBEntryPoint[T]) Init

func (e *DBEntryPoint[T]) Init()

Init should be run before `Run` and registers flags and the like.

func (*DBEntryPoint[T]) Main

func (e *DBEntryPoint[T]) Main()

Main is the actual function that needs to be called in Main.

type DBEntryPointConfigProvider

type DBEntryPointConfigProvider interface {
	LoggerProvider
	DBProvider
	MetaProvider
}

DBEntryPointConfigProvider is a type that can provide an entrypoint config.

type DBProvider

type DBProvider interface {
	GetDB() db.Config
}

DBProvider is a type that provides a db config.

type EntryPoint

type EntryPoint[T EntryPointConfigProvider] struct {
	Setup func(context.Context, T) error
	Start func(context.Context, T) error
	// contains filtered or unexported fields
}

EntryPoint is a top level handler for a simple app.

It handles reading the config, and setting up the logger.

func (*EntryPoint[T]) Init

func (e *EntryPoint[T]) Init()

Init should be run before `Run` and registers flags and the like.

func (*EntryPoint[T]) Main

func (e *EntryPoint[T]) Main()

Main is the actual function that needs to be called in Main.

type EntryPointConfigProvider

type EntryPointConfigProvider interface {
	LoggerProvider
}

EntryPointConfigProvider is a type that can provide an entrypoint config.

type LoggerProvider

type LoggerProvider interface {
	GetLogger() logutil.Config
}

LoggerProvider is a type that provides a logger config.

type MetaProvider

type MetaProvider interface {
	GetMeta() configmeta.Meta
}

MetaProvider is a type that provides a db config.

type ModelManager

type ModelManager struct {
	dbutil.BaseManager
}

ModelManager implements database functions.

func NewModelManager

func NewModelManager(conn *db.Connection, opts ...db.InvocationOption) *ModelManager

NewModelManager returns a new model manager.

func NewTest

func NewTest(t *testing.T) (*ModelManager, func())

NewTest returns a new test context.

func (ModelManager) DeleteExpiredSessions

func (m ModelManager) DeleteExpiredSessions(ctx context.Context, oldest time.Time) error

DeleteExpiredSessions deletes sessions that are expired and older than 14 days.

func (ModelManager) GetUserByEmail

func (m ModelManager) GetUserByEmail(ctx context.Context, email string) (output User, found bool, err error)

GetUserByEmail gets a user by email.

type Session

type Session struct {
	SessionID   string    `db:"session_id,pk"`
	UserID      uuid.UUID `db:"user_id"`
	BaseURL     string    `db:"base_url"`
	CreatedUTC  time.Time `db:"created_utc"`
	ExpiresUTC  time.Time `db:"expires_utc"`
	LastSeenUTC time.Time `db:"last_seen_utc"`
	UserAgent   string    `db:"user_agent"`
	RemoteAddr  string    `db:"remote_addr"`
	Locale      string    `db:"locale"`
}

Session holds session information for a user.

func CreateTestSession

func CreateTestSession(t *testing.T, mgr *ModelManager, user *User) *Session

CreateTestSession creates a test session.

func NewTestSession

func NewTestSession(user *User) Session

NewTestSession returns a test session.

func (*Session) ApplyTo

func (s *Session) ApplyTo(webSession *web.Session)

ApplyTo sets a web session from the values on the type session.

func (*Session) IsZero

func (s *Session) IsZero() bool

IsZero returns if the object is set or not. It will return true if either the userID or the sessionID are unset.

func (Session) TableName

func (s Session) TableName() string

TableName implements db.TableNameProvider.

type SessionState

type SessionState struct {
	User *User
}

SessionState is the session state type.

type User

type User struct {
	ID           uuid.UUID `db:"id,pk"`
	CreatedUTC   time.Time `db:"created_utc"`
	LastLoginUTC time.Time `db:"last_login_utc"`
	LastSeenUTC  time.Time `db:"last_seen_utc"`
	ProfileID    string    `db:"profile_id"`
	GivenName    string    `db:"given_name"`
	FamilyName   string    `db:"family_name"`
	PictureURL   string    `db:"picture_url"`
	Locale       string    `db:"locale"`
	Email        string    `db:"email"`
}

User is a user

func CreateTestUser

func CreateTestUser(t *testing.T, mgr *ModelManager) *User

CreateTestUser creates a test user.

func NewTestUser

func NewTestUser() User

NewTestUser returns a test user.

func (User) TableName

func (u User) TableName() string

TableName returns the mapped table name.

func (User) Username

func (u User) Username() string

Username returns the user section of the email.

Jump to

Keyboard shortcuts

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