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

package sqecho

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


Package Files


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.GET("/", func(c echo.Context) error {
	// Accessing the SDK through Echo's context
	return nil

func foo(req *http.Request) {
	// Accessing the SDK through the request context

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 4 packages (graph). Updated 2019-07-26. Refresh now. Tools for package owners.