Installation
Installing the Go Enforcer
The Go Enforcer is used as a middleware inside a Go web application.
1. Import the HUMAN Go Package.
GitHub Repository Access
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.
2. Implement the Runtime interface.
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.
- PerformSyncRequest - Performs a synchronous HTTP request by sending the
*http.Requestargument and returning an*http.Response. If the providedtimeout(in millseconds) is reached before the*http.Responsecan be returned, then an error should be returned instead. - PerformAsyncRequest - Performs an asynchronous request. The simplest way to implement this function is to call
PerformSyncRequestfrom within a new goroutine. - Log - Logs the provided
messageargument. - GetOS - Returns the Operating System name as a string.
- GetHostName - Returns the host name as a string.
Removed GetConfig() Function
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.
3. Build the HUMAN configuration.
In order to initialize the enforcer, you must first create a PxConfig struct instance. There are three ways of doing this.
All possible configuration key names and value types are listed in the Configuration section of the documentation.
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.
4. Integrate the enforcer by adding a middleware.
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:
- create the
HumanSecurityEnforcerstruct once (not on every middleware invocation) by callingNewHumanSecurityEnforcer() - Call the
Enforce()function as soon as possible in the request handler and interpret the returned values - Set the PXHD cookie on the response if necessary by calling
perimeterx.ShouldSetPxhd()andperimeterx.GetPxhdCookie() - If
Enforce()returned a non-nilresponse, transfer it to thehttp.ResponseWriterand exit without invoking the providedhandler - If
Enforce()returned anilresponse, invoke the providedhandler
Using Credential Intelligence?
The 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.