Documentation ¶
Overview ¶
Package web contains utilities to help build out a web service.
Index ¶
- Constants
- func HandleError(w http.ResponseWriter, err error, logger utils.ZapCompatibleLogger, ...) bool
- func InstallFusionAuth(ctx context.Context, mux *goji.Mux, sessions *SessionManager, ...) (io.Closer, error)
- func IsValidBacktoURL(path string) bool
- type APIHandler
- type APIMiddleware
- type AuthProvider
- type AuthProviderConfig
- type ErrorResponse
- type PanicCapture
- type Session
- type SessionManager
- type Store
- type Template
- type TemplateHandler
- type TemplateHandlerFunc
- type TemplateManager
- func NewTemplateManagerEmbed(fs fs.ReadDirFS, srcDir string) (TemplateManager, error)
- func NewTemplateManagerEmbedWithOptions(fs fs.ReadDirFS, srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)
- func NewTemplateManagerFS(srcDir string) (TemplateManager, error)
- func NewTemplateManagerFSWithOptions(srcDir string, opts protojson.MarshalingOptions) (TemplateManager, error)
- type TemplateMiddleware
- type UserInfo
Constants ¶
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
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".
- https://example.com -> false
- http://app.viam.com/path/name -> false
- https://app.viam.com/path/name -> true
- http://localhost/path/name -> true
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
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 ¶
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 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) 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 ¶
NewMongoDBSessionStore new MongoDB backed store.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template specifies which template to render.
func DirectTemplate ¶
DirectTemplate creates a template to say use this specific template.
func NamedTemplate ¶
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 ¶
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) GetEmailVerified ¶
GetEmailVerified if the email h as been verified.
func (*UserInfo) GetUpdatedAt ¶ added in v0.1.39
GetUpdatedAt gets the timestamp indicating when the user's profile was last updated/modified.