cookie

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: BSD-3-Clause Imports: 13 Imported by: 0

README

Go package for working with HTTP cookies.

Example

Error handling omitted for the sake of brevity.

package main

import (
	"fmt"
	"github.com/aaronland/go-http/v3/cookie"	
)

func main() {

	name := "c"
	secret := "s33kret"
	salt := "s4lty"
	
	cookie_uri := fmt.Sprintf("encrypted://?name=%s&secret=%s&salt=%s", name, secret, salt)
	ck, _ := cookie.NewCookie(ctx, cookie_uri)
}

See also

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRandomEncryptedCookieSalt

func NewRandomEncryptedCookieSalt() (string, error)

NewRandomEncryptedCookieSalt returns a random salt value suitable for `encrypted://` cookie URIs.

func NewRandomEncryptedCookieSecret

func NewRandomEncryptedCookieSecret() (string, error)

NewRandomEncryptedCookieSecret returns a random salt value suitable for `encrypted://` cookie URIs.

func NewRandomEncryptedCookieURI

func NewRandomEncryptedCookieURI(name string) (string, error)

NewRandomEncryptedCookieURI() returns a new URI for creating an `EncryptedCookie` instance for 'name' with random values for the required secret and salt.

func RegisterCookie

func RegisterCookie(ctx context.Context, scheme string, f CookieInitializeFunc) error

RegisterCookie() associates 'scheme' with 'f' in an internal list of avilable `Cookie` implementations.

func Schemes

func Schemes() []string

Schemes() returns the list of schemes that have been "registered".

Types

type Config

type Config struct {
	Name     string
	Domain   string
	Path     string
	Secure   bool
	SameSite http.SameSite
	TTL      *duration.Duration
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(ctx context.Context, uri string) (*Config, error)

func (*Config) NewCookie

func (cfg *Config) NewCookie(ctx context.Context, value string) (*http.Cookie, error)
type Cookie interface {
	// Get() returns the value of a HTTP cookie as a `awnumar/memguard.LockedBuffer` instance.
	Get(*http.Request) (*memguard.LockedBuffer, error)
	// GetString() returns the value of a HTTP cookie as a string.
	GetString(*http.Request) (string, error)
	// Get() assigned the value of a HTTP cookie from a `awnumar/memguard.LockedBuffer` instance.
	Set(http.ResponseWriter, *memguard.LockedBuffer) error
	// Get() assigned the value of a HTTP cookie from a string.
	SetString(http.ResponseWriter, string) error
	// SetWithCookie() assigned the value of a specific HTTP cookie from a `awnumar/memguard.LockedBuffer` instance.
	SetWithCookie(http.ResponseWriter, *memguard.LockedBuffer, *http.Cookie) error
	// SetStringWithCookie() assigned the value of a specific HTTP cookie from a string.
	SetStringWithCookie(http.ResponseWriter, string, *http.Cookie) error
	// Delete() removes the cookie from an HTTP response.
	Delete(http.ResponseWriter) error
}

Cookie is an interface for working with HTTP cookies.

func NewCookie

func NewCookie(ctx context.Context, uri string) (Cookie, error)

NewCookie() returns a new instance of `Cookie` for the scheme associated with 'uri'. It is assumed that this scheme will have previously been "registered" with the `RegisterCookie` method.

func NewEncryptedCookie

func NewEncryptedCookie(ctx context.Context, uri string) (Cookie, error)

NewEncryptedCookie() returns a new `EncryptedCookie` instance derived from 'uri' which is expected to take the form of:

encrypted://?name={NAME}&secret={SECRET}&salt={SECRET}

Where `{NAME}` is the name of the cookie for all subsequent operations; `{SECRET}` is the secret key used to encrypt the value of the cookie; `{SALT}` is the salt used to encrypt the cookie.

type CookieInitializeFunc

type CookieInitializeFunc func(context.Context, string) (Cookie, error)

CookieInitializeFunc is a function used to initialize an implementation of the `Cookie` interface.

type EncryptedCookie

type EncryptedCookie struct {
	Cookie
	// contains filtered or unexported fields
}

EncryptedCookie implements the `Cookie` interface for working with cookies whose values have been encrypted.

func (*EncryptedCookie) Delete

func (c *EncryptedCookie) Delete(rsp http.ResponseWriter) error

Delete removes the cookie associated with 'c' from 'rsp'.

func (*EncryptedCookie) Get

GetString returns the value of the cookie associated with 'c' in 'req' as an unencrypted `memguard.LockedBuffer` instance.

func (*EncryptedCookie) GetString

func (c *EncryptedCookie) GetString(req *http.Request) (string, error)

GetString returns the value of the cookie associated with 'c' in 'req' as an unencrypted string.

func (*EncryptedCookie) Set

Set assigns 'buf' as an encrypted string to a cookie associated with 'c' to 'rsp'.

func (*EncryptedCookie) SetString

func (c *EncryptedCookie) SetString(rsp http.ResponseWriter, value string) error

SetString assigns 'value' as an encrypted string to a cookie associated with 'c' to 'rsp'.

func (*EncryptedCookie) SetStringWithCookie

func (c *EncryptedCookie) SetStringWithCookie(rsp http.ResponseWriter, value string, http_cookie *http.Cookie) error

SetStringWithCookie assigns 'value' as an encrypted string to 'http_cookie' which is assigned to 'rsp'.

func (*EncryptedCookie) SetWithCookie

func (c *EncryptedCookie) SetWithCookie(rsp http.ResponseWriter, buf *memguard.LockedBuffer, http_cookie *http.Cookie) error

SetWithCookie assigns 'buf' as an encrypted string to 'http_cookie' which is assigned to 'rsp'.

Jump to

Keyboard shortcuts

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