CipherPayload middleware for Fiber that use AES Algorithm for encrypt and decrypt payload in request and response body.
go get -u github.com/owlsome-official/cipherPayload
func New(config ...Config) fiber.Handler
Import the middleware package that is part of the Fiber web framework
import (
"github.com/gofiber/fiber/v2"
"github.com/owlsome-official/cipherPayload"
)
After you initiate your Fiber app, you can use the following possibilities:
// Default middleware config
app.Use(cipherPayload.New(cipherPayload.Config{
KeyPairs: cipherPayload.KeyPairs{
AESKeyForEncrypt: []byte("AES_KEY_FOR_ENCRYPT"),
AESIVForEncrypt: []byte("AES_IV_FOR_ENCRYPT"),
AESKeyForDecrypt: []byte("AES_KEY_FOR_DECRYPT"),
AESIVForDecrypt: []byte("AES_IV_FOR_DECRYPT"),
},
}))
// Or extend your config for customization
app.Use(cipherPayload.New(cipherPayload.Config{
KeyPairs: cipherPayload.KeyPairs{
AESKeyForEncrypt: []byte("AES_KEY_FOR_ENCRYPT"),
AESIVForEncrypt: []byte("AES_IV_FOR_ENCRYPT"),
AESKeyForDecrypt: []byte("AES_KEY_FOR_DECRYPT"),
AESIVForDecrypt: []byte("AES_IV_FOR_DECRYPT"),
},
AllowMethod: []string{"POST", "OPTIONS"},
DebugMode: true,
}))
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Required. Default: KeyPairs{}
KeyPairs KeyPairs
// Optional. Default: ["OPTIONS", "POST", "PUT", "DELETE"]
AllowMethod []string
// Optional. Default: false
DebugMode bool
// Optional. [Default: false]
StrictMode bool
// Optional. Default: true
ExcludeHealthAPI bool
// Optional. Default: BadRequestResponse
FailResponse func(c *fiber.Ctx, msg string) error
// Optional. Default: InternalServerErrorResponse
ErrorResponse func(c *fiber.Ctx, msg string) error
}
var ConfigDefault = Config{
Next: nil,
KeyPairs: KeyPairs{},
AllowMethod: []string{
fiber.MethodOptions,
fiber.MethodPost,
fiber.MethodPut,
fiber.MethodDelete,
},
DebugMode: false,
StrictMode: false,
ExcludeHealthAPI: true,
FailResponse: BadRequestResponse,
ErrorResponse: InternalServerErrorResponse,
}
func BadRequestResponse(c *fiber.Ctx, msg string) error { // 400
if msg == "" {
msg = "Bad Request"
}
res := fiber.Map{
"status": "bad_request",
"message": msg,
}
return c.Status(fiber.StatusBadRequest).JSON(res)
}
func InternalServerErrorResponse(c *fiber.Ctx, msg string) error { // 500
if msg == "" {
msg = "Internal Server Error"
}
res := fiber.Map{
"status": "internal_server_error",
"message": msg,
}
return c.Status(fiber.StatusInternalServerError).JSON(res)
}
type KeyPairs struct {
AESKeyForEncrypt []byte
AESIVForEncrypt []byte
AESKeyForDecrypt []byte
AESIVForDecrypt []byte
}
An example of payload template (see more how to work in Example)
{
"payload": "FDp1Dl31zGx5nRXFNKihB+k3ly/L7HI9tlHycbKVRwhaf3RRdyFGviuntEZqst0/"
}
which can be decrypt to:
{
"firstname": "Chinnawat",
"lastname": "Chimdee"
}
{
"payload": "tpkWPEI6F/nfgUjjtwyKSUf1erxPL6rQt8jG3RitQ1KpvRALfR5YAgQ0CXYkrwLfTid6VdK3SNlffuu/kvI7Hj7br0ur01TUFUWxQ9cl+8U="
}
which encrypted from:
{
"firstname": "Chinnawat [Modified]",
"lastname": "Chimdee [Modified]"
}
Note: This payload using
- AESKeyForEncrypt (used in encrypting response body):
67890123456789012345678901234567
- AESIVForEncrypt (used in encrypting response body):
6789012345678901
- AESKeyForDecrypt (used in decrypting request body):
12345678901234567890123456789012
- AESIVForDecrypt (used in decrypting request body):
1234567890123456
Please go to example/README.md