2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-17 03:07:53 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package cookie // import "miniflux.app/v2/internal/http/cookie"
|
2017-12-17 03:07:53 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cookie names.
|
|
|
|
const (
|
2019-01-22 05:26:46 +01:00
|
|
|
CookieAppSessionID = "MinifluxAppSessionID"
|
|
|
|
CookieUserSessionID = "MinifluxUserSessionID"
|
2017-12-23 01:42:17 +01:00
|
|
|
|
|
|
|
// Cookie duration in days.
|
|
|
|
cookieDuration = 30
|
2017-12-17 03:07:53 +01:00
|
|
|
)
|
|
|
|
|
2017-12-23 01:42:17 +01:00
|
|
|
// New creates a new cookie.
|
2018-02-04 00:33:17 +01:00
|
|
|
func New(name, value string, isHTTPS bool, path string) *http.Cookie {
|
2017-12-17 03:07:53 +01:00
|
|
|
return &http.Cookie{
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
2018-02-04 00:33:17 +01:00
|
|
|
Path: basePath(path),
|
2017-12-17 03:07:53 +01:00
|
|
|
Secure: isHTTPS,
|
|
|
|
HttpOnly: true,
|
2017-12-23 01:42:17 +01:00
|
|
|
Expires: time.Now().Add(cookieDuration * 24 * time.Hour),
|
2020-08-11 03:51:40 +02:00
|
|
|
SameSite: http.SameSiteLaxMode,
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expired returns an expired cookie.
|
2018-02-04 00:33:17 +01:00
|
|
|
func Expired(name string, isHTTPS bool, path string) *http.Cookie {
|
2017-12-17 03:07:53 +01:00
|
|
|
return &http.Cookie{
|
|
|
|
Name: name,
|
|
|
|
Value: "",
|
2018-02-04 00:33:17 +01:00
|
|
|
Path: basePath(path),
|
2017-12-17 03:07:53 +01:00
|
|
|
Secure: isHTTPS,
|
|
|
|
HttpOnly: true,
|
|
|
|
MaxAge: -1,
|
|
|
|
Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
|
2020-08-11 03:51:40 +02:00
|
|
|
SameSite: http.SameSiteLaxMode,
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 00:33:17 +01:00
|
|
|
|
|
|
|
func basePath(path string) string {
|
|
|
|
if path == "" {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|