-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
50 lines (42 loc) · 1.18 KB
/
cookie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2022 by Chris Palmer (https://noncombatant.org)
// SPDX-License-Identifier: GPL-3.0
package main
import (
"encoding/base64"
"net/http"
"os"
"path"
"time"
)
var (
cookieLifetime, _ = time.ParseDuration("24000h")
)
func getCookieLifetime() time.Time {
return (time.Now()).Add(cookieLifetime)
}
func getCookie(token string) *http.Cookie {
return &http.Cookie{Name: "token", Value: token, Secure: true, HttpOnly: true, Expires: getCookieLifetime(), Path: "/", SameSite: http.SameSiteDefaultMode}
}
func checkToken(token string, sessionsDirectoryPathname string) bool {
if len(token) != encodedTokenLength {
return false
}
if data, e := base64.URLEncoding.DecodeString(token); e != nil || len(data) != tokenLength {
return false
}
_, e := os.Stat(path.Join(sessionsDirectoryPathname, token))
return e == nil
}
func createToken(sessionsDirectoryPathname string) (string, error) {
bytes := getRandomBytes(tokenLength)
token := base64.URLEncoding.EncodeToString(bytes)
pathname := path.Join(sessionsDirectoryPathname, token)
if file, e := os.Create(pathname); e != nil {
return "", e
} else {
if e := file.Close(); e != nil {
return "", e
}
}
return token, nil
}