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

package sqgin

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

Index

Package Files

gin.go

func Middleware Uses

func Middleware() gingonic.HandlerFunc

Middleware is Sqreen's middleware function for Gin to monitor and protect the requests Gin receives. In protection mode, it can block and redirect requests according to its IP address or identified user using `Identify()` and `MatchSecurityResponse()` methods from the request handler.

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 on a Gin request context. The middleware function stores it into both of them. Note that Gin's context implements the `context.Context` interface which allows `sdk.FromContext()` to be used with both of them.

Usage example:

router := gin.Default()
router.Use(sqgin.Middleware())

router.GET("/", func(c *gin.Context) {
	// Accessing the SDK through Gin's context
	sdk.FromContext(c).TrackEvent("my.event.one")
	foo(c.Request)
}

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

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

Package sqgin imports 2 packages (graph). Updated 2019-04-17. Refresh now. Tools for package owners.