go-agent: github.com/sqreen/go-agent/sdk/middleware/sqecho Index | Files

package sqecho

import "github.com/sqreen/go-agent/sdk/middleware/sqecho"

Index

Package Files

echo.go

func FromContext Uses

func FromContext(c echo.Context) *sdk.HTTPRequestRecord

FromContext allows to access the HTTPRequestRecord from Echo request handlers if present, and nil otherwise. The value is stored in handler contexts by the middleware function, and is of type *HTTPRequestRecord.

Note that Echo's context does not implement the `context.Context` interface, so `sdk.FromContext()` cannot be used with it, hence `FromContext()` in this package to access the SDK context value from Echo's context. `sdk.FromContext()` can still be used on the request context.

func Middleware Uses

func Middleware() echo.MiddlewareFunc

Middleware is Sqreen's middleware function for Echo to monitor and protect the requests Echo receives. In protection mode, it can block and redirect requests according to their IP addresses or identified users using Identify()` and `MatchSecurityResponse()` methods.

SDK methods can be called from request handlers by using the request event record. It can be accessed using `sdk.FromContext()` on a request context or this package's `FromContext()` on an Echo request context. The middleware function stores it into both of them.

Usage example:

e := echo.New()
e.Use(sqecho.Middleware())

e.GET("/", func(c echo.Context) error {
	// Accessing the SDK through Echo's context
	sqecho.FromContext(c).TrackEvent("my.event.one")
	foo(c.Request())
	return nil
}

func foo(req *http.Request) {
	// Accessing the SDK through the request context
	sdk.FromContext(req.Context()).TrackEvent("my.event.two")
}

e.GET("/", func(c echo.Context) {
	// Globally identifying a user and checking if the request should be
	// aborted.
	uid := sdk.EventUserIdentifiersMap{"uid": "my-uid"}
	sqUser := sqecho.FromContext(c).ForUser(uid)
	sqUser.Identify() // Globally associate this user to the current request
	if match, err := sqUser.MatchSecurityResponse(); match {
		// Return to stop further handling the request and let Sqreen's
		// middleware apply and abort the request.
		return err
	}
	// ... not blocked ...
	return nil
}

Package sqecho imports 3 packages (graph). Updated 2019-06-18. Refresh now. Tools for package owners.