middleware

package
v0.0.0-...-5f8c1fa Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthMap = map[string]AuthMetadata{}/* 112 elements not displayed */

AuthMap maps HTTP handler names to the permissions required to access that handler. We want to be explicit about permissions because explicitness is much easier to debug than assumptions and magic methods.

When permissions are explicitly defined, we can check them in middleware instead of polluting request handler code with lots of security logic. We know that middleware always runs and cannot be accidentally omitted by the developer.

We also have a failsafe built in to our Authorize middleware that raises an error if permissions are not checked and explicitly granted. If we forget to map a controller action to permission metadata below, requests that hit an unguarded route will return an internal server error.

Functions

func AddTokenToContext

func AddTokenToContext(c *gin.Context, cookieToken string)

AddTokenToContext adds an xor'ed version of the CSRF token to the context, so we can pass it into forms. This is to thwart BREACH attacks.

See http://breachattack.com/

func AssertSameOrigin

func AssertSameOrigin(c *gin.Context) error

func Authenticate

func Authenticate() gin.HandlerFunc

Authenticate ensures the current user is logged in for all requests other than those going to "/" or static resources.

func Authorize

func Authorize() gin.HandlerFunc

Authorize ensures that a user is authorized to commit a specific action on a specific resource. This function uses a ResourceAuthorization struct to figure out what's being requested, what action the user wants to take on the resource, and whether the user has sufficient permissions.

With the exception of the login page and static resources such as images, scripts, and stylesheets, all endpoints require an authorization check. Failure to perform the check is itself an error.

func CSRF

func CSRF() gin.HandlerFunc

func CompareCSRFTokens

func CompareCSRFTokens(requestToken, cookieToken string) error

func ExemptFromAuth

func ExemptFromAuth(c *gin.Context) bool

func GetCSRFCookieToken

func GetCSRFCookieToken(c *gin.Context) (string, error)

GetCSRFCookieToken returns the csrf token set in the cookie.

func GetCSRFRequestToken

func GetCSRFRequestToken(c *gin.Context) string

GetCSRFRequestToken returns the token set in the request form or header.

func GetUser

func GetUser(c *gin.Context) (user *pgmodels.User, err error)

func GetUserFromAPIHeaders

func GetUserFromAPIHeaders(c *gin.Context) (user *pgmodels.User, err error)

GetUserFromAPIHeaders returns the current user based on the API auth headers.

func GetUserFromSession

func GetUserFromSession(c *gin.Context) (user *pgmodels.User, err error)

GetUserFromSession returns the User for the current session.

func IsAPIRequest

func IsAPIRequest(c *gin.Context) bool

func IsAPIRoute

func IsAPIRoute(c *gin.Context) bool

IsAPIRoute returns true if the requested route matches one of our API prefixes. This uses c.Request.URL.Path because c.FullPath() can return an empty string if the path does not match any known routes.

func IsCSRFSafeMethod

func IsCSRFSafeMethod(method string) bool

func LoadCookie

func LoadCookie(c *gin.Context, name string) error

LoadCookie loads a cookie's value into the request context.

func LoadCookies

func LoadCookies(c *gin.Context) error

LoadCookies loads the user's flash and preference cookes into the request context.

func SetDefaultHeaders

func SetDefaultHeaders(c *gin.Context)

SetDefaultHeaders sets headers that we want to include with every response. Note that it's OK for client to cache and store static resources such as images, scripts and stylesheets. Those are public resources containing no sensitive info. All other resources must use no-cache/no-store.

func XorStrings

func XorStrings(input, key string) string

XorStrings scrambles the CSRF token that appears in the header and in forms on each request. This is for BREACH attack prevention.

Types

type AuthMetadata

type AuthMetadata struct {
	// ResourceType is the type of resource the user is requesting.
	// E.g. "IntellectualObject", "GenericFile", etc.
	ResourceType string
	// Permission is the name of the permission required to access
	// the requested resources. E.g. "PremisEventCreate".
	Permission constants.Permission
}

AuthMetadata contains information about what type of resource is being requested, and what action the user wants to take on that resource.

type ResourceAuthorization

type ResourceAuthorization struct {
	Handler            string
	ResourceID         int64
	ResourceIdentifier string
	ResourceInstID     int64
	ResourceType       string
	Permission         constants.Permission
	Checked            bool
	Approved           bool
	Error              error
	// contains filtered or unexported fields
}

ResourceAuthorization contains information about the current request handler, the resource and action being requested, and whether the current user is authorized to do what they're trying to do.

func AuthorizeResource

func AuthorizeResource(c *gin.Context) *ResourceAuthorization

AuthorizeResource returns a ResourceAuthorization struct describing what is being authorized and whether the current user is allowed to do what they're trying to do.

func (*ResourceAuthorization) CurrentUser

func (r *ResourceAuthorization) CurrentUser() *pgmodels.User

func (*ResourceAuthorization) GetError

func (r *ResourceAuthorization) GetError() string

GetError returns an error message with detailed information. This is primarily for logging.

func (*ResourceAuthorization) GetNotAuthorizedMessage

func (r *ResourceAuthorization) GetNotAuthorizedMessage() string

GetNotAuthorizedMessage returns a message describing what was not authorized, and for whom.

func (*ResourceAuthorization) NonAdminIsRequestingAdminAPI

func (r *ResourceAuthorization) NonAdminIsRequestingAdminAPI() bool

NonAdminIsRequestingAdminAPI returns true if a non-admin user is requesting a resource from the admin API. Although the admin and member APIs share some common handlers, we want to force members to access features through member-api endpoints.

This test is a shortcut that allows us to skip more complicated checks.

func (*ResourceAuthorization) String

func (r *ResourceAuthorization) String() string

String returns this object in string format, suitable for debugging.

Jump to

Keyboard shortcuts

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