cookie

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package cookie provides functions for securely setting and retrieving HTTP cookies using the SecureCookie library.

See the SecureCookie README for more information on how this library works.

This package provides the following functions and types: - Set: Set a cookie value using base64 encoding and append the HMAC signature. - Get: Retrieve a cookie value and verify its HMAC signature. - SetSigned: Set a cookie value using base64 encoding and append the HMAC signature, then sign the entire value with the secret key. - GetSigned: Retrieve a cookie value and verify its HMAC signature, then decode the value using the secret key.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValueTooLong = errors.New("cookie value too long")
	ErrInvalidValue = errors.New("invalid cookie value")
)

Functions

func Delete

func Delete(ctx *xun.Context, v http.Cookie)

Delete deletes a cookie by setting the MaxAge to -1 and setting the value to an empty string.

func Get

func Get(ctx *xun.Context, name string) (string, error)

Get retrieves a cookie value using base64 decoding

If the length of the resulting cookie is longer than 4096 bytes, an ErrValueTooLong error is returned.

func GetSigned

func GetSigned(ctx *xun.Context, name string, secretKey []byte) (string, *time.Time, error)

GetSigned retrieves a cookie value using HMAC-SHA256 verification

This function takes a secret key as an argument and uses it to calculate a HMAC signature of the cookie name and value. This signature is compared to the signature stored in the cookie. If the two signatures match, the original cookie value is returned. Otherwise, an ErrInvalidValue error is returned.

If the length of the resulting cookie value is longer than 4096 bytes, an ErrValueTooLong error is returned.

func Set

func Set(ctx *xun.Context, v http.Cookie) error

Set sets a cookie value using base64 encoding

If the length of the resulting cookie is longer than 4096 bytes, an ErrValueTooLong error is returned.

func SetSigned

func SetSigned(ctx *xun.Context, v http.Cookie, secretKey []byte) (time.Time, error)

SetSigned sets a cookie value using HMAC-SHA256 and appends the signature to the value.

This function takes a secret key as an argument and uses it to calculate a HMAC signature of the cookie name and value. This signature is prepended to the value before setting the cookie.

If the length of the resulting cookie value is longer than 4096 bytes, an ErrValueTooLong error is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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