web

package
v0.1.117 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Package web contains utilities to help build out a web service.

Index

Constants

View Source
const (
	// ViamTokenCookie is the cookie name for an authenticated access token
	//nolint:gosec
	ViamTokenCookie string = "viam.auth.token"
	// ViamRefreshCookie is the cookie name for an authenticated refresh token.
	ViamRefreshCookie string = "viam.auth.refresh"
	// ViamExpiryCookie is the cookie name for an authenticated token's expiry.
	ViamExpiryCookie string = "viam.auth.expiry"
)

Variables

This section is empty.

Functions

func HandleError

func HandleError(w http.ResponseWriter, err error, logger utils.ZapCompatibleLogger, context ...string) bool

HandleError returns true if there was an error and you should stop.

func InstallFusionAuth added in v0.1.55

func InstallFusionAuth(
	ctx context.Context,
	mux *goji.Mux,
	sessions *SessionManager,
	config AuthProviderConfig,
	logger utils.ZapCompatibleLogger,
) (io.Closer, error)

InstallFusionAuth does initial setup and installs routes for FusionAuth.

func IsValidBacktoURL added in v0.1.117

func IsValidBacktoURL(path string) bool

IsValidBacktoURL returns true if the passed string is a secure URL to a whitelisted hostname. The whitelisted hostnames are: "localhost", "app.viam.dev", and "app.viam.com".

Types

type APIHandler

type APIHandler interface {
	// return (result, error)
	// if both are null, do nothing
	ServeAPI(w http.ResponseWriter, r *http.Request) (interface{}, error)
}

APIHandler what a user has to implement to use APIMiddleware.

type APIMiddleware

type APIMiddleware struct {
	protojson.MarshalingOptions

	Handler APIHandler
	Logger  utils.ZapCompatibleLogger

	// Recover from panics with a proper error logs.
	PanicCapture
}

APIMiddleware simple layer between http.Handler interface that does json marshalling and error handling.

func NewAPIMiddleware added in v0.0.6

func NewAPIMiddleware(h APIHandler, logger utils.ZapCompatibleLogger) *APIMiddleware

NewAPIMiddleware returns a configured APIMiddleware with a panic capture configured.

func (*APIMiddleware) ServeHTTP

func (am *APIMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP call the api.

type AuthProvider added in v0.1.55

type AuthProvider struct {
	io.Closer
	// contains filtered or unexported fields
}

AuthProvider should include all state that we need to share with auth callbacks or to make customizations on the internals of the specific auth mechanisms we implement for a particular provider.

func (*AuthProvider) Close added in v0.1.55

func (s *AuthProvider) Close() error

Close called by io.Closer.

type AuthProviderConfig added in v0.1.55

type AuthProviderConfig struct {
	Domain                string
	ClientID              string
	Secret                string
	BaseURL               string
	PostLogoutRedirectURL string
	EnableTest            bool
}

AuthProviderConfig config options with constants that will probably need to be manually configured after retrieval from the auth provider web UI or API (e.g. for FusionAuth).

type ErrorResponse

type ErrorResponse interface {
	Error() string
	Status() int
}

ErrorResponse lets you specify a status code.

func ErrorResponseStatus

func ErrorResponseStatus(code int) ErrorResponse

ErrorResponseStatus creates an error response with a specific code.

type PanicCapture added in v0.0.6

type PanicCapture struct {
	Logger utils.ZapCompatibleLogger
}

PanicCapture allows recovery during a request handler from panics. It prints a formatted log to the underlying logger.

func (*PanicCapture) Recover added in v0.0.6

func (p *PanicCapture) Recover(w http.ResponseWriter, r *http.Request)

Recover captures and prints the error if recover() has an error.

type Session

type Session struct {
	Data bson.M
	// contains filtered or unexported fields
}

Session representation of a session.

func (*Session) Save

Save saves a session back to the store it came freom.

type SessionManager

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

SessionManager handles working with sessions from http.

func NewSessionManager

func NewSessionManager(theStore Store, logger utils.ZapCompatibleLogger) *SessionManager

NewSessionManager creates a new SessionManager.

func (*SessionManager) DeleteSession

func (sm *SessionManager) DeleteSession(ctx context.Context, r *http.Request, w http.ResponseWriter)

DeleteSession deletes a session.

func (*SessionManager) Get

func (sm *SessionManager) Get(r *http.Request, createIfNotExist bool) (*Session, error)

Get get a session from the request via cookies.

func (*SessionManager) HasSessionWithAccessToken added in v0.1.102

func (sm *SessionManager) HasSessionWithAccessToken(ctx context.Context, token string) bool

HasSessionWithAccessToken returns true if there is an active session associated with that access token.

type Store

type Store interface {
	Delete(ctx context.Context, id string) error
	Get(ctx context.Context, id string) (*Session, error)
	Save(ctx context.Context, s *Session) error
	SetSessionManager(*SessionManager)
	HasSessionWithToken(ctx context.Context, token string) (bool, error)
}

Store actually stores raw data somewhere.

func NewMemorySessionStore

func NewMemorySessionStore() Store

NewMemorySessionStore creates a new memory session store.

func NewMongoDBSessionStore

func NewMongoDBSessionStore(ctx context.Context, coll *mongo.Collection) (Store, error)

NewMongoDBSessionStore new MongoDB backed store.

type Template

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

Template specifies which template to render.

func DirectTemplate

func DirectTemplate(t *template.Template) *Template

DirectTemplate creates a template to say use this specific template.

func NamedTemplate

func NamedTemplate(called string) *Template

NamedTemplate creates a Template with a name.

type TemplateHandler

type TemplateHandler interface {
	// return (template name, thing to pass to template, error)
	Serve(w http.ResponseWriter, r *http.Request) (*Template, interface{}, error)
}

TemplateHandler implement this to be able to use middleware.

type TemplateHandlerFunc

type TemplateHandlerFunc func(w http.ResponseWriter, r *http.Request) (*Template, interface{}, error)

TemplateHandlerFunc the func version of the handler.

func (TemplateHandlerFunc) Serve

func (f TemplateHandlerFunc) Serve(w http.ResponseWriter, r *http.Request) (*Template, interface{}, error)

Serve does the work.

type TemplateManager

type TemplateManager interface {
	LookupTemplate(name string) (*template.Template, error)
}

TemplateManager responsible for managing, caching, finding templates.

func NewTemplateManagerEmbed

func NewTemplateManagerEmbed(fs fs.ReadDirFS, srcDir string) (TemplateManager, error)

NewTemplateManagerEmbed creates a TemplateManager from an embedded file system.

func NewTemplateManagerEmbedWithOptions added in v0.0.6

func NewTemplateManagerEmbedWithOptions(fs fs.ReadDirFS, srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)

NewTemplateManagerEmbedWithOptions creates a TemplateManager from an embedded file system. Allows optional protojson.MarshalingOptions.

func NewTemplateManagerFS

func NewTemplateManagerFS(srcDir string) (TemplateManager, error)

NewTemplateManagerFS creates a new TemplateManager from the file system.

func NewTemplateManagerFSWithOptions added in v0.0.6

func NewTemplateManagerFSWithOptions(srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)

NewTemplateManagerFSWithOptions creates a new TemplateManager from the file system. Allows optional protojson.MarshalingOptions.

type TemplateMiddleware

type TemplateMiddleware struct {
	Templates TemplateManager
	Handler   TemplateHandler
	Logger    utils.ZapCompatibleLogger

	// Recover from panics with a proper error logs.
	PanicCapture
}

TemplateMiddleware handles the rendering of the template from the data and finding of the template.

func NewTemplateMiddleware added in v0.0.6

func NewTemplateMiddleware(template TemplateManager, h TemplateHandler, logger utils.ZapCompatibleLogger) *TemplateMiddleware

NewTemplateMiddleware returns a configured TemplateMiddleWare with a panic capture configured.

func (*TemplateMiddleware) ServeHTTP

func (tm *TemplateMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

type UserInfo

type UserInfo struct {
	LoggedIn   bool                   `json:"loggedIn"`
	Properties map[string]interface{} `json:"properties"`
}

UserInfo basic info about a user from a session.

func GetLoggedInUserInfo

func GetLoggedInUserInfo(sessions *SessionManager, r *http.Request) (UserInfo, error)

GetLoggedInUserInfo figures out if the session is associated with a user.

func (*UserInfo) GetBool

func (u *UserInfo) GetBool(name string) bool

GetBool gets an attribute as a bool.

func (*UserInfo) GetEmail

func (u *UserInfo) GetEmail() string

GetEmail the email.

func (*UserInfo) GetEmailVerified

func (u *UserInfo) GetEmailVerified() bool

GetEmailVerified if the email h as been verified.

func (*UserInfo) GetUpdatedAt added in v0.1.39

func (u *UserInfo) GetUpdatedAt() string

GetUpdatedAt gets the timestamp indicating when the user's profile was last updated/modified.

Directories

Path Synopsis
Package cors wraps the cors package with needed defaults.
Package cors wraps the cors package with needed defaults.
Package protojson provides helpers to marshal proto.Message to json
Package protojson provides helpers to marshal proto.Message to json

Jump to

Keyboard shortcuts

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