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

package sqgin

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


Package Files


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 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 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.GET("/", func(c *gin.Context) {
	// Accessing the SDK through Gin's context

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

router.GET("/", func(c *gin.Context) {
	// Example of 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.
	// ... not blocked ...

Package sqgin imports 4 packages (graph). Updated 2019-07-26. Refresh now. Tools for package owners.