mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
set secure cookies by default.
This commit is contained in:
parent
c6a183f766
commit
fa16b28ac0
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"demodesk/neko/internal/http/auth"
|
"demodesk/neko/internal/http/auth"
|
||||||
@ -9,6 +10,9 @@ import (
|
|||||||
"demodesk/neko/internal/utils"
|
"demodesk/neko/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var CookieExpirationDate = time.Now().Add(365 * 24 * time.Hour)
|
||||||
|
var UnsecureCookies = os.Getenv("DISABLE_SECURE_COOKIES") == "true"
|
||||||
|
|
||||||
type SessionLoginPayload struct {
|
type SessionLoginPayload struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Secret string `json:"secret"`
|
Secret string `json:"secret"`
|
||||||
@ -32,17 +36,26 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sameSite := http.SameSiteNoneMode
|
||||||
|
if UnsecureCookies {
|
||||||
|
sameSite = http.SameSiteDefaultMode
|
||||||
|
}
|
||||||
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "neko-id",
|
Name: "neko-id",
|
||||||
Value: session.ID(),
|
Value: session.ID(),
|
||||||
Expires: time.Now().Add(365 * 24 * time.Hour),
|
Expires: CookieExpirationDate,
|
||||||
|
Secure: !UnsecureCookies,
|
||||||
|
SameSite: sameSite,
|
||||||
HttpOnly: false,
|
HttpOnly: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "neko-secret",
|
Name: "neko-secret",
|
||||||
Value: data.Secret,
|
Value: data.Secret,
|
||||||
Expires: time.Now().Add(365 * 24 * time.Hour),
|
Expires: CookieExpirationDate,
|
||||||
|
Secure: !UnsecureCookies,
|
||||||
|
SameSite: sameSite,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -54,10 +67,17 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *ApiManagerCtx) Logout(w http.ResponseWriter, r *http.Request) {
|
func (api *ApiManagerCtx) Logout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sameSite := http.SameSiteNoneMode
|
||||||
|
if UnsecureCookies {
|
||||||
|
sameSite = http.SameSiteDefaultMode
|
||||||
|
}
|
||||||
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "neko-id",
|
Name: "neko-id",
|
||||||
Value: "",
|
Value: "",
|
||||||
Expires: time.Unix(0, 0),
|
Expires: time.Unix(0, 0),
|
||||||
|
Secure: !UnsecureCookies,
|
||||||
|
SameSite: sameSite,
|
||||||
HttpOnly: false,
|
HttpOnly: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -65,6 +85,8 @@ func (api *ApiManagerCtx) Logout(w http.ResponseWriter, r *http.Request) {
|
|||||||
Name: "neko-secret",
|
Name: "neko-secret",
|
||||||
Value: "",
|
Value: "",
|
||||||
Expires: time.Unix(0, 0),
|
Expires: time.Unix(0, 0),
|
||||||
|
Secure: !UnsecureCookies,
|
||||||
|
SameSite: sameSite,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user