miniflux/internal/http/cookie/cookie.go

53 lines
1.1 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2017-12-17 03:07:53 +01: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.
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,
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),
SameSite: http.SameSiteLaxMode,
2017-12-17 03:07:53 +01:00
}
}
// Expired returns an expired cookie.
func Expired(name string, isHTTPS bool, path string) *http.Cookie {
2017-12-17 03:07:53 +01:00
return &http.Cookie{
Name: name,
Value: "",
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),
SameSite: http.SameSiteLaxMode,
2017-12-17 03:07:53 +01:00
}
}
func basePath(path string) string {
if path == "" {
return "/"
}
return path
}