42 lines
857 B
Go
42 lines
857 B
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
sessionCookie = "camper-session"
|
|
)
|
|
|
|
func SetSessionCookie(w http.ResponseWriter, cookie string) {
|
|
http.SetCookie(w, createSessionCookie(cookie, 8766*24*time.Hour))
|
|
}
|
|
|
|
func GetSessionCookie(r *http.Request) string {
|
|
if cookie, err := r.Cookie(sessionCookie); err == nil {
|
|
return cookie.Value
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func DeleteSessionCookie(w http.ResponseWriter) {
|
|
http.SetCookie(w, createSessionCookie("", -24*time.Hour))
|
|
}
|
|
|
|
func createSessionCookie(value string, duration time.Duration) *http.Cookie {
|
|
return &http.Cookie{
|
|
Name: sessionCookie,
|
|
Value: value,
|
|
Path: "/",
|
|
Expires: time.Now().Add(duration),
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteLaxMode,
|
|
}
|
|
}
|