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"
|
|
|
|
"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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
session := &Session{ID: sessionID}
|
|
|
|
err = loadSession(session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sess *Session) Flush() error {
|
|
|
|
return saveSession(sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveSession(sess *Session) error {
|
|
|
|
filename := generateFilename(sess.ID)
|
|
|
|
err := os.Mkdir("session", 0755)
|
|
|
|
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)
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// add defaults to session?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = json.NewDecoder(f).Decode(sess)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|