cookiecode

package module
v0.0.0-...-4f918c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2022 License: MIT Imports: 4 Imported by: 0

README

:cookie: Cookiecode :cookie:


Cookiecode is a package that allows you to encrypt and decrypt your cookies for production grade use. Keep your cookies secure.

Installation

go get -u github.com/4thabang/go-cookiecode

Demo Usage

.env variables

WIP

Encode Code Snippet

import (
  "time"
  "log"
  "net/http"

  cookiecode "github.com/4thabang/go-cookiecode"
)

func CookieEncoder(w http.ResponseWriter, r *http.Request) {
  // TODO: Add godotenv .env examples

  value := map[string]string{
    "key":   cookieKey,
    "value": cookievalue,
  }

  encoded, err := cookiecode.Encode(value)
  if err != nil {
    log.Printf("error: %v\n", err)
  }

  cookie := &http.Cookie{
    Name:    value.Key,
    Value:   encoded,
    Expires: time.Time,
  }

  // Set our encoded cookie value here
  http.SetCookie(w, cookie)
}

Decode Code Snippet

import (
  "log"
  "net/http"

  cookiecode "github.com/4thabang/go-cookiecode"
)

func CookieDecoer(w http.ResponseWriter, r *http.Request) {
  cookie, err := r.Cookie("key")
  if err != nil {
    log.Printf("error: %v\n", err)
  }

  value, err := cookiecode.Decode(cookie.Name, cookie.Value)
  if err != nil {
    log.Printf("error: %v\n", err)
  }

  // Print the cookie value
  fmt.Println(value["value"])
}

License

WIP

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(key, cookie string) (map[string]string, error)

Decode allows us to decode our cookie in order to consume it safely, awaay from prying eyes.

Types

type EncodeType

type EncodeType struct {
	Value string
	Key   string
}

EncodeType is a struct that houses the value of our key which will be determined by the user at runtime.

type Encoder

type Encoder interface {
	Encode(v map[string]string) (string, error)
}

Encoder is an interface that allows us to implement our Encode function in order to fully encode our cookie values.

func (*Encoder) Encode

func (e *Encoder) Encode(v map[string]string) (string, error)

Encode allows us to encode our cookie in order to keep it secure, safe and unexposed.

type Encrypt

type Encrypt struct {
	HashKey  []byte // AES 256-bit
	BlockKey []byte // AES 128-bit
}

Encrypt holds the values for our 'HashKey' and 'BlockKey' as a struct. These values must be within the Advanced Encryption Standard.

func (*Encrypt) Keys

func (e *Encrypt) Keys(v vObject) (*securecookie.SecureCookie, error)

Keys allows us to store our encryption keys securely for re-use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL