signedcookie

package
v0.0.51 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package signedcookie provides a secure cookie manager for Go web applications.

Index

Constants

View Source
const SecretSize = 32 // SecretSize is the size, in bytes, of a cookie secret.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseCookie added in v0.0.45

type BaseCookie *http.Cookie

BaseCookie is a type alias for http.Cookie. It is used for providing and overriding default cookie settings. Note that the Name, HttpOnly, and Secure fields are ignored. The expires field is not ignored, but it will be overridden by an explicitly set TTL.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles the creation, signing, and verification of secure cookies.

func NewManager

func NewManager(secrets Secrets) (*Manager, error)

NewManager creates a new Manager instance with the provided secrets. It returns an error if no secrets are provided or if any secret is invalid.

func (Manager) NewDeletionCookie added in v0.0.45

func (m Manager) NewDeletionCookie(cookie *http.Cookie) *http.Cookie

NewDeletionCookie creates a new cookie that will delete the specified cookie when sent to the client.

func (Manager) NewSignedCookie added in v0.0.45

func (m Manager) NewSignedCookie(unsignedCookie *http.Cookie) (*http.Cookie, error)

NewSignedCookie creates a new cookie with a signed value based on the provided unsigned cookie.

func (Manager) VerifyAndReadCookieValue added in v0.0.45

func (m Manager) VerifyAndReadCookieValue(r *http.Request, key string) (string, error)

VerifyAndReadCookieValue retrieves and verifies the value of a signed cookie. It returns an error if the cookie is not found or is invalid.

type Secrets added in v0.0.2

type Secrets []string

Secrets is a latest-first list of 32-byte, base64-encoded secrets.

type SignedCookie added in v0.0.20

type SignedCookie[T any] struct {
	Manager    *Manager
	TTL        time.Duration
	BaseCookie BaseCookie
}

SignedCookie provides methods for working with signed cookies of a specific type T.

func (*SignedCookie[T]) NewDeletionCookie added in v0.0.45

func (sc *SignedCookie[T]) NewDeletionCookie() *http.Cookie

NewDeletionCookie creates a new cookie that will delete the current cookie when sent to the client.

func (*SignedCookie[T]) NewSignedCookie added in v0.0.45

func (sc *SignedCookie[T]) NewSignedCookie(unsignedValue *T, overrideBaseCookie BaseCookie) (*http.Cookie, error)

NewSignedCookie creates a new signed cookie with the provided value and optional override settings.

func (*SignedCookie[T]) VerifyAndReadCookieValue added in v0.0.45

func (sc *SignedCookie[T]) VerifyAndReadCookieValue(r *http.Request) (*T, error)

VerifyAndReadCookieValue retrieves and verifies the value of the signed cookie from the request. It returns the decoded value of type T or an error if retrieval or verification fails.

Jump to

Keyboard shortcuts

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