Implement sessions in LevelDB driver
This commit is contained in:
parent
868a4e3abd
commit
53e6b315c7
|
@ -101,7 +101,7 @@ func apiAuthHandler(f func(*config.Options, *happydns.User, httprouter.Params, i
|
|||
err: err,
|
||||
status: http.StatusUnauthorized,
|
||||
}.WriteResponse(w)
|
||||
} else if session, err := storage.MainStore.GetSession(sessionid); err != nil {
|
||||
} else if session, err := storage.UsersStore.GetSession(sessionid); err != nil {
|
||||
APIErrorResponse{
|
||||
err: err,
|
||||
status: http.StatusUnauthorized,
|
||||
|
|
|
@ -64,7 +64,7 @@ func dummyAuth(_ httprouter.Params, body io.Reader) Response {
|
|||
}
|
||||
}
|
||||
|
||||
if err := storage.MainStore.CreateSession(session); err != nil {
|
||||
if err := storage.UsersStore.CreateSession(session); err != nil {
|
||||
return APIErrorResponse{
|
||||
err: err,
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ func checkAuth(_ httprouter.Params, body io.Reader) Response {
|
|||
}
|
||||
}
|
||||
|
||||
if err := storage.MainStore.CreateSession(session); err != nil {
|
||||
if err := storage.UsersStore.CreateSession(session); err != nil {
|
||||
return APIErrorResponse{
|
||||
err: err,
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ type Storage interface {
|
|||
CreateSession(session *happydns.Session) error
|
||||
UpdateSession(session *happydns.Session) error
|
||||
DeleteSession(session *happydns.Session) error
|
||||
ClearSession() error
|
||||
ClearSessions() error
|
||||
|
||||
GetUsers() (happydns.Users, error)
|
||||
GetUser(id int) (*happydns.User, error)
|
||||
|
@ -38,6 +38,12 @@ type UserStorage interface {
|
|||
DoMigration() error
|
||||
Close() error
|
||||
|
||||
GetSession(id []byte) (*happydns.Session, error)
|
||||
CreateSession(session *happydns.Session) error
|
||||
UpdateSession(session *happydns.Session) error
|
||||
DeleteSession(session *happydns.Session) error
|
||||
ClearSessions() error
|
||||
|
||||
GetUsers() (happydns.Users, error)
|
||||
GetUser(id int) (*happydns.User, error)
|
||||
GetUserByEmail(email string) (*happydns.User, error)
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package database // import "happydns.org/storage/leveldb"
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
mrand "math/rand"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
|
@ -53,10 +54,10 @@ func (s *LevelDBStorage) put(key string, v interface{}) error {
|
|||
return s.db.Put([]byte(key), data, nil)
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) findKey(prefix string) (key string, id int64, err error) {
|
||||
func (s *LevelDBStorage) findInt63Key(prefix string) (key string, id int64, err error) {
|
||||
found := true
|
||||
for found {
|
||||
id = rand.Int63()
|
||||
id = mrand.Int63()
|
||||
key = fmt.Sprintf("%s%d", prefix, id)
|
||||
|
||||
found, err = s.db.Has([]byte(key), nil)
|
||||
|
@ -67,6 +68,23 @@ func (s *LevelDBStorage) findKey(prefix string) (key string, id int64, err error
|
|||
return
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) findBytesKey(prefix string, len int) (key string, id []byte, err error) {
|
||||
id = make([]byte, len)
|
||||
found := true
|
||||
for found {
|
||||
if _, err = crand.Read(id); err != nil {
|
||||
return
|
||||
}
|
||||
key = fmt.Sprintf("%s%x", prefix, id)
|
||||
|
||||
found, err = s.db.Has([]byte(key), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) delete(key string) error {
|
||||
return s.db.Delete([]byte(key), nil)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb/util"
|
||||
|
||||
"git.happydns.org/happydns/model"
|
||||
)
|
||||
|
||||
func (s *LevelDBStorage) GetSession(id []byte) (session *happydns.Session, err error) {
|
||||
session = &happydns.Session{}
|
||||
err = s.get(fmt.Sprintf("user.session-%x", id), &session)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) CreateSession(session *happydns.Session) error {
|
||||
key, id, err := s.findBytesKey("user.session-", 255)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
session.Id = id
|
||||
return s.put(key, session)
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) UpdateSession(session *happydns.Session) error {
|
||||
return s.put(fmt.Sprintf("user.session-%x", session.Id), session)
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) DeleteSession(session *happydns.Session) error {
|
||||
return s.delete(fmt.Sprintf("user.session-%x", session.Id))
|
||||
}
|
||||
|
||||
func (s *LevelDBStorage) ClearSessions() error {
|
||||
tx, err := s.db.OpenTransaction()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
iter := tx.NewIterator(util.BytesPrefix([]byte("user.session-")), nil)
|
||||
defer iter.Release()
|
||||
|
||||
for iter.Next() {
|
||||
err = tx.Delete(iter.Key(), nil)
|
||||
if err != nil {
|
||||
tx.Discard()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Discard()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -65,7 +65,7 @@ func (s *LevelDBStorage) UserExists(email string) bool {
|
|||
}
|
||||
|
||||
func (s *LevelDBStorage) CreateUser(u *happydns.User) error {
|
||||
key, id, err := s.findKey("user-")
|
||||
key, id, err := s.findInt63Key("user-")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -83,6 +83,10 @@ func (s *LevelDBStorage) DeleteUser(u *happydns.User) error {
|
|||
}
|
||||
|
||||
func (s *LevelDBStorage) ClearUsers() error {
|
||||
if err := s.ClearSessions(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := s.db.OpenTransaction()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -34,7 +34,7 @@ func (s *MySQLStorage) DeleteSession(session *happydns.Session) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) ClearSession() error {
|
||||
func (s *MySQLStorage) ClearSessions() error {
|
||||
_, err := s.db.Exec("DELETE FROM user_sessions")
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue