wiki/session.go

120 lines
2.8 KiB
Go
Raw Normal View History

2021-08-07 17:13:10 +00:00
/*
* Wiki - A wiki with editor
* Copyright (c) 2021 Peter Stuifzand
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2018-11-24 12:34:51 +00:00
package main
import (
"encoding/json"
2021-01-17 16:36:17 +00:00
"errors"
2018-11-24 12:34:51 +00:00
"fmt"
"net/http"
"os"
2021-08-18 18:11:32 +00:00
"sync"
2018-11-24 12:34:51 +00:00
"time"
)
type Session struct {
ID string
LoggedIn bool
Me string
AuthorizationEndpoint string
RedirectURI string
State string
NextURI string
}
func NewSession(w http.ResponseWriter, r *http.Request) (*Session, error) {
sessionID, err := getSessionCookie(w, r)
if err != nil {
2021-08-18 18:11:32 +00:00
return nil, fmt.Errorf("getSessionCookie failed: %w", err)
2018-11-24 12:34:51 +00:00
}
session := &Session{ID: sessionID}
err = loadSession(session)
if err != nil {
2021-08-18 18:11:32 +00:00
return nil, fmt.Errorf("loadSession failed: %w" , err)
2018-11-24 12:34:51 +00:00
}
return session, nil
}
func (sess *Session) Flush() error {
return saveSession(sess)
}
2021-08-18 18:11:32 +00:00
var fileMutex sync.RWMutex
2018-11-24 12:34:51 +00:00
func saveSession(sess *Session) error {
filename := generateFilename(sess.ID)
err := os.Mkdir("session", 0755)
2021-08-18 18:11:32 +00:00
fileMutex.Lock()
defer fileMutex.Unlock()
2018-11-24 12:34:51 +00:00
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(sess)
return err
}
func loadSession(sess *Session) error {
filename := generateFilename(sess.ID)
err := os.Mkdir("session", 0755)
2021-08-18 18:11:32 +00:00
fileMutex.RLock()
defer fileMutex.RUnlock()
2018-11-24 12:34:51 +00:00
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
// add defaults to session?
return nil
}
2021-08-18 18:11:32 +00:00
return fmt.Errorf("while opening file %s: %w", filename, err)
2018-11-24 12:34:51 +00:00
}
defer f.Close()
err = json.NewDecoder(f).Decode(sess)
2021-08-18 18:11:32 +00:00
if err != nil {
return fmt.Errorf("while decoding json from file %s: %w", filename, err)
}
return nil
2018-11-24 12:34:51 +00:00
}
func generateFilename(id string) string {
return fmt.Sprintf("session/%s.json", id)
}
func getSessionCookie(w http.ResponseWriter, r *http.Request) (string, error) {
c, err := r.Cookie("session")
var sessionVar string
2021-01-17 16:36:17 +00:00
if err != nil && errors.Is(err, http.ErrNoCookie) {
sessionVar = RandStringBytes(16)
c = &http.Cookie{
Name: "session",
Value: sessionVar,
Expires: time.Now().Add(24 * time.Hour),
Path: "/",
2018-11-24 12:34:51 +00:00
}
} else {
sessionVar = c.Value
2021-01-17 16:36:17 +00:00
c.Expires = time.Now().Add(24 * time.Hour)
2018-11-24 12:34:51 +00:00
}
2021-01-17 16:36:17 +00:00
http.SetCookie(w, c)
2018-11-24 12:34:51 +00:00
return sessionVar, nil
}