Import sources from Kontor
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/dao"
|
||||
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/properties"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var sessionDao = SessionDAO{Db: dao.KalorienmanagerDb}
|
||||
|
||||
// EnsureLoggedIn ensures that a request will be aborted with an error
|
||||
// if the user is not logged in
|
||||
func EnsureLoggedIn() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// If there's an error or if the token is empty
|
||||
// the user is not logged in
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
if session, err := sessionDao.GetSession(sessionID); err != nil || session.Username == "" {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
//c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnsureAdminStatus ensures that a request will be aborted with an error
|
||||
// if the user is not logged in
|
||||
func EnsureAdminStatus() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// If there's an error or if the token is empty
|
||||
// the user is not logged in
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
if session, err := sessionDao.GetSession(sessionID); err != nil || !session.IsAdmin {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
//c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnsureNotLoggedIn ensures that a request will be aborted with an error
|
||||
// if the user is already logged in
|
||||
func EnsureNotLoggedIn() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// If there's no error or if the token is not empty
|
||||
// the user is already logged in
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
if session, err := sessionDao.GetSession(sessionID); err != nil || session.Username != "" {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
//c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetSessionStatus reads sessionId from cookie if available or create new session object
|
||||
// and sets cookie accordingly.
|
||||
func SetSessionStatus() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if sessionID, err := c.Cookie("session"); err == nil || sessionID != "" {
|
||||
c.Set("session", sessionID)
|
||||
} else {
|
||||
session, _ := sessionDao.GetSession(bson.NewObjectId().Hex())
|
||||
sessionID := session.ID.Hex()
|
||||
c.Set("session", sessionID)
|
||||
c.SetCookie("session", sessionID, 3600, "", "", false, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetSessionData populates sesion information with username, admin status of user and
|
||||
// application version.
|
||||
func SetSessionData(c *gin.Context, data gin.H) {
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
// TODO move PrintDebug("setSessionData(): %v", sessionId)
|
||||
session, _ := sessionDao.GetSession(sessionID)
|
||||
// TODO move PrintDebug("setSessionData(): %v", *session)
|
||||
data["is_logged_in"] = (session.Username != "")
|
||||
data["is_admin"] = session.IsAdmin
|
||||
data["version"] = properties.Version
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package auth
|
||||
|
||||
import "gopkg.in/mgo.v2/bson"
|
||||
|
||||
// Session defines the data model for sessions with id,user name and admin status.
|
||||
type Session struct {
|
||||
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"`
|
||||
Username string `json:"username" bson:"username,omitempty"`
|
||||
IsAdmin bool `json:"is_admin" bson:"is_admin,omitempty"`
|
||||
Model string `json:"model" bson:"model,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/dao"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// SessionDAO extends the type BaseDAO.
|
||||
type SessionDAO struct {
|
||||
Db dao.BaseDAO
|
||||
}
|
||||
|
||||
const (
|
||||
// SESSIONCOLLECTION defines the collection name for storing session data.
|
||||
SESSIONCOLLECTION = "session"
|
||||
// SESSIONMODEL defines the name of the session data model.
|
||||
SESSIONMODEL = "kalorienmanager.admin.session"
|
||||
)
|
||||
|
||||
// FindAll retrieves the list of sessions from the database.
|
||||
func (m *SessionDAO) FindAll() ([]Session, error) {
|
||||
m.Db.Connect()
|
||||
var sessions []Session
|
||||
err := m.Db.MongoDb.C(SESSIONCOLLECTION).Find(bson.M{"model": SESSIONMODEL}).All(&sessions)
|
||||
return sessions, err
|
||||
}
|
||||
|
||||
// FindByID returns a session with given id or returns the error.
|
||||
func (m *SessionDAO) FindByID(id string) (Session, error) {
|
||||
m.Db.Connect()
|
||||
var session Session
|
||||
err := m.Db.MongoDb.C(SESSIONCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&session)
|
||||
return session, err
|
||||
}
|
||||
|
||||
// Insert a session into database.
|
||||
func (m *SessionDAO) Insert(session Session) error {
|
||||
m.Db.Connect()
|
||||
session.Model = SESSIONMODEL
|
||||
err := m.Db.MongoDb.C(SESSIONCOLLECTION).Insert(&session)
|
||||
//log.PrintDebug("Insert: %v, %v\n", session, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Upsert a session into database.
|
||||
func (m *SessionDAO) Upsert(session Session) (*mgo.ChangeInfo, error) {
|
||||
m.Db.Connect()
|
||||
session.Model = SESSIONMODEL
|
||||
info, err := m.Db.MongoDb.C(SESSIONCOLLECTION).Upsert(bson.M{"_id": session.ID}, &session)
|
||||
return info, err
|
||||
}
|
||||
|
||||
// Update an existing session.
|
||||
func (m *SessionDAO) Update(session Session) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(SESSIONCOLLECTION).UpdateId(session.ID, &session)
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete an existing session.
|
||||
func (m *SessionDAO) Delete(session Session) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(SESSIONCOLLECTION).Remove(&session)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetSession get a session by given id or create a new one, if nothing was found.
|
||||
func (m *SessionDAO) GetSession(id string) (*Session, error) {
|
||||
m.Db.Connect()
|
||||
session, err := m.FindByID(id)
|
||||
if err != nil {
|
||||
session = Session{ID: bson.ObjectIdHex(id), Username: "", IsAdmin: false}
|
||||
m.Insert(session)
|
||||
}
|
||||
return &session, nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/dao"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var sessionModelTestTable = []struct {
|
||||
name string
|
||||
typeName string
|
||||
}{
|
||||
{"Id", "string"},
|
||||
{"Username", "string"},
|
||||
{"IsAdmin", "bool"},
|
||||
{"Model", "string"},
|
||||
}
|
||||
|
||||
func TestSessionModel(t *testing.T) {
|
||||
m := Session{}
|
||||
if reflect.TypeOf(m).NumField() != len(sessionModelTestTable) {
|
||||
t.Fail()
|
||||
}
|
||||
for index, testData := range sessionModelTestTable {
|
||||
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
|
||||
if givenType != testData.typeName {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSessions(t *testing.T) {
|
||||
var (
|
||||
sessionDao = SessionDAO{Db: dao.TestDb}
|
||||
)
|
||||
sessions, err := sessionDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if sessions != nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertSession(t *testing.T) {
|
||||
var (
|
||||
sessionDao = SessionDAO{Db: dao.TestDb}
|
||||
session = Session{}
|
||||
sessions []Session
|
||||
)
|
||||
session.ID = bson.NewObjectId()
|
||||
session.Username = "test"
|
||||
err := sessionDao.Insert(session)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
sessions, err = sessionDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(sessions) != 1 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpsertSession(t *testing.T) {
|
||||
var (
|
||||
sessionDao = SessionDAO{Db: dao.TestDb}
|
||||
session = Session{}
|
||||
)
|
||||
session.ID = bson.NewObjectId()
|
||||
session.Username = "test2"
|
||||
sessionDao.Upsert(session)
|
||||
sessions, err := sessionDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(sessions) != 2 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSession(t *testing.T) {
|
||||
var (
|
||||
sessionDao = SessionDAO{Db: dao.TestDb}
|
||||
)
|
||||
sessions, err := sessionDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
for _, session := range sessions {
|
||||
sessionDao.Delete(session)
|
||||
}
|
||||
sessions, err = sessionDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(sessions) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user