Code Monkey home page Code Monkey logo

cipherpayload's Introduction

CipherPayload

made-with-Go Go Reference GitHub issues GitHub forks GitHub stars

CipherPayload middleware for Fiber that use AES Algorithm for encrypt and decrypt payload in request and response body.

Table of Contents

Installation

  go get -u github.com/buildingwatsize/cipherPayload

Signatures

func New(config ...Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

import (
  "github.com/gofiber/fiber/v2"
  "github.com/buildingwatsize/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

// 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
}

Default Config

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,
}

Default Response

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)
}

KeyPairs Property

type KeyPairs struct {
  AESKeyForEncrypt []byte
  AESIVForEncrypt  []byte
  AESKeyForDecrypt []byte
  AESIVForDecrypt  []byte
}

Payload Template

An example of payload template (see more how to work in Example)

Request

{
  "payload": "FDp1Dl31zGx5nRXFNKihB+k3ly/L7HI9tlHycbKVRwhaf3RRdyFGviuntEZqst0/"
}

which can be decrypt to:

{
  "firstname": "Chinnawat",
  "lastname": "Chimdee"
}

Response

{
  "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

Example Usage

Please go to example/README.md

[NEW!] AES Encryption Tools (useful for debugging)

https://buildingwatsize.github.io/encrypt-tools/

cipherpayload's People

Contributors

buildingwatsize avatar imgbotapp avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

cipherpayload's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.