utils

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OAuth2StateCookieName is the name of the cookie storing OAuth2 state.
	OAuth2StateCookieName = "oauth2_state"
	// OAuth2StateExpiration is the expiration time for OAuth2 state cookie in seconds.
	OAuth2StateExpiration = 300 // 5 minutes
)
View Source
const StateByteLength = 32

Variables

View Source
var (
	ErrCookieNotFound = errors.New("cookie not found")
)
View Source
var ErrInvalidStateInfo = errors.New("invalid state info")

Functions

func ClearAuthCookie

func ClearAuthCookie(w http.ResponseWriter)

ClearAuthCookie clears the authentication cookie.

func ClearCookie

func ClearCookie(w http.ResponseWriter, name string)

ClearCookie clears a cookie by setting its MaxAge to -1.

func ClearOAuth2StateCookie

func ClearOAuth2StateCookie(w http.ResponseWriter)

ClearOAuth2StateCookie clears the OAuth2 state cookie.

func FromRequestContext

func FromRequestContext[T any](r *http.Request, key any) (T, bool)

func GetCookie

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

GetCookie retrieves the value of a cookie by name.

func GetJWTFromCookie

func GetJWTFromCookie(r *http.Request) (string, error)

GetJWTFromCookie extracts JWT token from the request cookie.

func InputFromContext

func InputFromContext[T any](r *http.Request) (T, bool)

func SetAuthCookie

func SetAuthCookie(w http.ResponseWriter, tokenString string, maxAge int)

SetAuthCookie sets the JWT token as a secure httpOnly cookie.

func SetCookie

func SetCookie(w http.ResponseWriter, name, value string, maxAge int)

SetCookie sets a cookie with the given name, value, and max age.

func SetOAuth2StateCookie

func SetOAuth2StateCookie(w http.ResponseWriter, state string)

SetOAuth2StateCookie stores the OAuth2 state parameter in a cookie for verification.

func VerifyOAuth2State

func VerifyOAuth2State(r *http.Request, state string) error

VerifyOAuth2State verifies the state parameter against the stored cookie.

Types

type State

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

func NewState

func NewState() (*State, error)

func NewStateFromEncode

func NewStateFromEncode(stateStr string) (*State, error)

func (*State) AddInfo

func (s *State) AddInfo(key, value string)

func (*State) GetInfo

func (s *State) GetInfo(key string) (string, bool)

func (*State) String

func (s *State) String() string

Jump to

Keyboard shortcuts

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