hmac is a Go module that offers services for HTTP HMAC authentication.
Use the Go CLI tool go to install hmac.
go get github.com/pascalallen/hmac
...
import "github.com/pascalallen/hmac"
...
publicKey := hmac.GenerateSecureRandom(16)
privateKey := hmac.GenerateSecureRandom(16)
var timeTolerance int64 = 300
request, _ := http.NewRequest(
http.MethodPost,
"http://localhost:8080?abc=xyz",
bytes.NewReader([]byte(`{"foo": "bar"}`)),
)
// create request service and sign request
requestService, _ := hmac.NewRequestService(publicKey, privateKey)
signedRequest := requestService.SignRequest(request)
// create authenticator and validate signed request
authenticator, _ := hmac.NewAuthenticator(publicKey, privateKey, timeTolerance)
isValid := authenticator.Validate(*signedRequest)
...
Run tests and create coverage profile:
go test -covermode=count -coverprofile=coverage.out
View test coverage profile:
go tool cover -html=coverage.out
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.