cookie

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 3 Imported by: 0

README

Handling Cookies

  • Cookies in cyclops are described using the structure CyclopsCookie
  • The fields in the structure are as follows:
Attribute Optional
Name No
Value No
Path Yes
Domain No
Secure Yes
HttpOnly Yes
SameSite Yes
Expires Yes
MaxAge Yes
import "github.com/flannel-dev-lab/cyclops/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cookieObj := cookie.CyclopsCookie{
            Name:           "test",
            Value:          "test",
            Secure:         false,
            HttpOnly:       true,
            StrictSameSite: false,
        }
    
    cookieObj.SetCookie(w)
}

Once the cookie is created, we use the method SetCookie which takes in a http.ResponseWriter to write the cookie

import "github.com/flannel-dev-lab/cyclops/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cookieObj := cookie.CyclopsCookie{}

    fmt.Println(cookieObj.GetCookie(r, "test"))
}

To read a cookie, we create an empty cookie object and call the GetCookie method which takes in *http.Request, the Name of the cookie and returns a *http.Cookie

Reading all cookies

import "github.com/flannel-dev-lab/cyclops/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cookieObj := cookie.CyclopsCookie{}

    fmt.Println(cookieObj.GetAll(r, "test"))
}

To read a cookie, we create an empty cookie object and call the GetAll method which takes in *http.Request and returns a array of *http.Cookie

Deletes a cookie by setting max-age to 0

import "github.com/flannel-dev-lab/cyclops/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cyclopsCookie := cookie.CyclopsCookie{}

    cookie, _ := cookieObj.GetCookie(r, "test")
    
    cyclopsCookie.Delete(w, cookie)
}

Documentation

Overview

Package cookie deals with set, get and deleting of HTTP Cookies

Index

Constants

View Source
const (
	// DefaultExpiry is the default expiry for a cookie if a user does not sets it
	DefaultExpiry = 3600
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CyclopsCookie

type CyclopsCookie struct {
	// Name of cookie
	Name string
	// Value of cookie
	Value string
	// The Domain and Path attributes define the scope of the cookie. They essentially tell the browser what website
	// the cookie belongs to
	Path   string
	Domain string
	// Secure attribute is meant to keep cookie communication limited to encrypted transmission, directing browsers
	// to use cookies only via secure/encrypted connections
	Secure bool
	// HttpOnly attribute directs browsers not to expose cookies through channels other than HTTP (and HTTPS) requests.
	// This means that the cookie cannot be accessed via client-side scripting languages (notably JavaScript),
	// and therefore cannot be stolen easily via cross-site scripting
	HttpOnly bool
	// SameSite when enabled, a cookie can only be sent in requests originating from the same origin as the target
	// domain, it helps to prevent XSRF attacks
	SameSite http.SameSite
	// Expires specifies time in seconds for a cookie to expire
	Expires time.Duration
	MaxAge  int
}

CyclopsCookie is a struct to hold cookie info

func (CyclopsCookie) Delete added in v1.1.14

func (cyclopsCookie CyclopsCookie) Delete(w http.ResponseWriter, cookie *http.Cookie)

Delete is used to delete a cookie from the browser by setting the expires attribute to 0

func (CyclopsCookie) GetAll

func (cyclopsCookie CyclopsCookie) GetAll(r *http.Request) []*http.Cookie

GetAll returns array of HTTP cookies

func (CyclopsCookie) GetCookie

func (cyclopsCookie CyclopsCookie) GetCookie(r *http.Request, name string) (*http.Cookie, error)

GetCookie retrieves a cookie based on the cookie name from request, returns error if cookie does not exist

func (CyclopsCookie) SetCookie

func (cyclopsCookie CyclopsCookie) SetCookie(w http.ResponseWriter)

SetCookie is used to sets a cookie to the responseWriter

Jump to

Keyboard shortcuts

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