ovpn-certman/services/sessions.go

124 lines
2.5 KiB
Go
Raw Normal View History

2018-01-26 14:49:03 +01:00
package services
import (
2018-01-29 09:18:19 +01:00
"encoding/gob"
"fmt"
"html/template"
"log"
"net/http"
"time"
"github.com/alexedwards/scs"
2018-01-26 14:49:03 +01:00
)
2018-01-29 09:18:19 +01:00
var (
// FlashesKey is the key used for the flashes in the cookie
FlashesKey = "_flashes"
// UserEmailKey is the key used to reference usernames
UserEmailKey = "_user_email"
)
func init() {
// Register the Flash message type, so gob can serialize it
gob.Register(Flash{})
}
type SessionsConfig struct {
SessionName string
CookieKey string
HttpOnly bool
Secure bool
Lifetime time.Duration
}
2018-01-26 14:49:03 +01:00
// Sessions is a wrapped scs.Store in order to implement custom logic
type Sessions struct {
2018-01-29 09:18:19 +01:00
*scs.Manager
}
// NewSessions populates the default sessions Store
func NewSessions(conf *SessionsConfig) *Sessions {
2018-01-29 09:18:19 +01:00
store := scs.NewCookieManager(
conf.CookieKey,
2018-01-26 14:49:03 +01:00
)
store.Name(conf.SessionName)
2018-01-29 09:18:19 +01:00
store.HttpOnly(true)
store.Lifetime(conf.Lifetime)
store.Secure(conf.Secure)
2018-01-29 09:18:19 +01:00
return &Sessions{store}
2018-01-29 09:18:19 +01:00
}
2018-02-01 09:31:06 +01:00
func (store *Sessions) GetUsername(req *http.Request) string {
2018-01-29 09:18:19 +01:00
if store == nil {
// if store was not initialized, all requests fail
2018-02-03 18:14:47 +01:00
log.Println("Nil pointer when checking session for username")
2018-01-29 09:18:19 +01:00
return ""
}
sess := store.Load(req)
email, err := sess.GetString(UserEmailKey)
if err != nil {
// Username found
return ""
}
// User is logged in
return email
}
2018-02-01 09:31:06 +01:00
func (store *Sessions) SetUsername(w http.ResponseWriter, req *http.Request, username string) {
2018-01-29 09:18:19 +01:00
if store == nil {
// if store was not initialized, do nothing
return
}
sess := store.Load(req)
// renew token to avoid session pinning/fixation attack
sess.RenewToken(w)
2018-02-01 09:31:06 +01:00
sess.PutString(w, UserEmailKey, username)
2018-01-29 09:18:19 +01:00
}
type Flash struct {
Message template.HTML
Type string
}
// Render renders the flash message as a notification box
func (flash Flash) Render() template.HTML {
return template.HTML(
fmt.Sprintf(
"<div class=\"notification is-radiusless is-%s\"><div class=\"container has-text-centered\">%s</div></div>",
flash.Type, flash.Message,
),
)
}
// Flash add flash message to session data
func (store *Sessions) Flash(w http.ResponseWriter, req *http.Request, flash Flash) error {
2018-01-29 09:18:19 +01:00
var flashes []Flash
sess := store.Load(req)
if err := sess.GetObject(FlashesKey, &flashes); err != nil {
return err
}
flashes = append(flashes, flash)
return sess.PutObject(w, FlashesKey, flashes)
}
2018-01-26 14:49:03 +01:00
2018-01-29 09:18:19 +01:00
// Flashes returns a slice of flash messages from session data
func (store *Sessions) Flashes(w http.ResponseWriter, req *http.Request) []Flash {
2018-01-29 09:18:19 +01:00
var flashes []Flash
sess := store.Load(req)
sess.PopObject(w, FlashesKey, &flashes)
return flashes
2018-01-26 14:49:03 +01:00
}