jwt for webapp
This commit is contained in:
parent
7e77fd7d27
commit
9d332bbda4
5 changed files with 112 additions and 152 deletions
|
|
@ -2,33 +2,30 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"html/template"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/db"
|
||||
"git.giftfish.de/ston1th/jwt"
|
||||
)
|
||||
|
||||
const (
|
||||
keyLen = 32
|
||||
authLen = keyLen * 2
|
||||
cookieName = "gosession"
|
||||
)
|
||||
|
||||
type HTTPServer struct {
|
||||
Version string
|
||||
SecCookie bool
|
||||
Store *sessions.CookieStore
|
||||
DB *db.DB
|
||||
listener net.Listener
|
||||
srv *http.Server
|
||||
templ map[string]*template.Template
|
||||
res map[string][]byte
|
||||
Version string
|
||||
listener net.Listener
|
||||
srv *http.Server
|
||||
|
||||
DB *db.DB
|
||||
JWT *jwt.JWT
|
||||
|
||||
templ map[string]*template.Template
|
||||
res map[string][]byte
|
||||
}
|
||||
|
||||
func NewHTTPServer(l net.Listener, versin string, secCookie bool) (srv *HTTPServer) {
|
||||
|
|
@ -36,6 +33,7 @@ func NewHTTPServer(l net.Listener, versin string, secCookie bool) (srv *HTTPServ
|
|||
Version: version,
|
||||
SecCookie: secCookie,
|
||||
listener: l,
|
||||
JWT: jwt.New(time.Hour*12, nil, nil),
|
||||
}
|
||||
srv.cookieStore()
|
||||
srv.loadTemplates()
|
||||
|
|
@ -46,7 +44,7 @@ func NewHTTPServer(l net.Listener, versin string, secCookie bool) (srv *HTTPServ
|
|||
}
|
||||
|
||||
func (s *HTTPServer) Start() (err error) {
|
||||
s.BS, err = NewBoltStore()
|
||||
s.DB, err = db.New()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -81,37 +79,3 @@ func (s *HTTPServer) contextWrapper(h ctxHandler) http.HandlerFunc {
|
|||
h(newContext(w, r, s))
|
||||
}
|
||||
}
|
||||
func (s *HTTPServer) cookieStore() {
|
||||
options := &sessions.Options{
|
||||
Path: "/",
|
||||
MaxAge: 3600 * 12,
|
||||
Secure: s.SecCookie,
|
||||
HttpOnly: true,
|
||||
}
|
||||
authKey := genKey(authLen)
|
||||
encKey := genKey(keyLen)
|
||||
s.Store = sessions.NewCookieStore(authKey, encKey)
|
||||
s.Store.Options = options
|
||||
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Hour * 12)
|
||||
newAuthKey := genKey(authLen)
|
||||
newEncKey := genKey(keyLen)
|
||||
newStore := sessions.NewCookieStore(newAuthKey, newEncKey, authKey, encKey)
|
||||
newStore.Options = options
|
||||
|
||||
s.Store = newStore
|
||||
authKey = newAuthKey
|
||||
encKey = newEncKey
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func genKey(length int) (bytes []byte) {
|
||||
bytes = make([]byte, length)
|
||||
if _, err := io.ReadFull(rand.Reader, bytes); err != nil {
|
||||
log.Println("genKey:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue