The Go Enforcer is used as a middleware inside a Go web application.
If you do not have access the Go SDK GitHub Repository, please reach out to our Customer Support team.
To install the HUMAN Go Enforcer package, run the following command from within your Go project.
The HUMAN Go Enforcer package requires you to implement a Runtime interface, which the Enforcer uses to perform HTTP requests, log messages, and obtain metadata about the server.
*http.Request argument and returning an *http.Response. If the provided timeout (in milliseconds) is reached before the *http.Response can be returned, then an error should be returned instead.PerformSyncRequest from within a new goroutine.message argument.In previous versions of the HUMAN Go Enforcer, the Runtime interface required the implementation of a GetConfig() function that returned a PxConfig struct. This function is no longer necessary and will not be used.
The ExampleRuntime struct below implements the perimeterx.Runtime interface using a single http.Client. However, you can implement this with a sync.Pool of http.Client structs as well.
In order to initialize the enforcer, you must first create a PxConfig struct instance. There are three ways of doing this.
Directly
The first way to do this is to use the BuildPXConfig() function. This function accepts a map[string]interface{} argument representing the configuration, and returns a pointer to a PxConfig struct (or an error if something went wrong).
JSON File
You may store your configuration in a JSON file and load it into a PxConfig struct using the GetPxConfigFromJson() function. This function accepts a string representing the path to the JSON file, and returns a pointer to a PxConfig struct (or an error if something went wrong).
The minimum JSON file should look like this:
Builder
You may call NewPXConfigBuilder() to initialize a PXConfigBuilder struct, which has setters for all possible configuration values. Once all the desired configurations have been set, call the Build() function to receive a pointer to a PxConfig struct.
Using the Default HUMAN Security Middleware
For an out-of-the-box middleware function, simply call the CreateDefaultHumanSecurityMiddleware function and pass in the PxConfig struct and Runtime implementation.
Creating a Customized Middleware
For a more customized solution, initialize a HumanSecurityEnforcer struct and use it in your custom middleware function.
The recommended usage is to:
HumanSecurityEnforcer struct once (not on every middleware invocation) by calling NewHumanSecurityEnforcer()Enforce() function as soon as possible in the request handler and interpret the returned valuesperimeterx.ShouldSetPxhd() and perimeterx.GetPxhdCookie()Enforce() returned a non-nil response, transfer it to the http.ResponseWriter and exit without invoking the provided handlerEnforce() returned a nil response, invoke the provided handlerThe Credential Intelligence product also requires the sending of an additional_s2s activity, which indicates whether the login attempt was successful. To properly determine whether the login attempt was successful and send the additional_s2s activity, use the perimeterx.NewResponseWriterWrapper() and PostEnforce() functions. See code sample below.