1.3 KiB
1.3 KiB
cookies v1.0.0
HTTP cookie utilities for Go web applications with security best practices.
Features
- Secure cookie setting with HttpOnly flag
- Cookie deletion with proper expiration
- Pagefrom tracking for post-login redirects
- Host validation for referer-based redirects
- Full test coverage
Installation
go get git.haelnorr.com/h/golib/cookies
Quick Start
package main
import (
"net/http"
"git.haelnorr.com/h/golib/cookies"
)
func handler(w http.ResponseWriter, r *http.Request) {
// Set a secure cookie
cookies.SetCookie(w, "session", "/", "abc123", 3600)
// Delete a cookie
cookies.DeleteCookie(w, "old_session", "/")
// Handle pagefrom for redirects
if r.URL.Path == "/login" {
cookies.SetPageFrom(w, r, "example.com")
}
// Check pagefrom after login
redirectTo := cookies.CheckPageFrom(w, r)
http.Redirect(w, r, redirectTo, http.StatusFound)
}
Documentation
See the wiki documentation for detailed usage information and examples.
License
MIT License
Contributing
Please see the main golib repository for contributing guidelines.
Related Projects
This package is part of the golib collection of utilities for Go applications and integrates well with other golib packages.