Semaphore/db/bolt/session.go

107 lines
2.7 KiB
Go
Raw Permalink Normal View History

package bolt
import (
"github.com/semaphoreui/semaphore/db"
"reflect"
"time"
)
type globalToken struct {
2022-01-23 15:01:11 +01:00
ID string `db:"id" json:"id"`
UserID int `db:"user_id" json:"user_id"`
}
2022-02-03 08:05:13 +01:00
var globalTokenObject = db.ObjectProps{
TableName: "token",
PrimaryColumnName: "id",
Type: reflect.TypeOf(globalToken{}),
IsGlobal: true,
}
func (d *BoltDb) CreateSession(session db.Session) (db.Session, error) {
newSession, err := d.createObject(session.UserID, db.SessionProps, session)
if err != nil {
return db.Session{}, err
}
return newSession.(db.Session), nil
}
func (d *BoltDb) CreateAPIToken(token db.APIToken) (db.APIToken, error) {
// create token in bucket "token_<user id>"
newToken, err := d.createObject(token.UserID, db.TokenProps, token)
if err != nil {
return db.APIToken{}, err
}
// create token in bucket "token"
_, err = d.createObject(0, globalTokenObject, globalToken{ID: token.ID, UserID: token.UserID})
if err != nil {
return db.APIToken{}, err
}
return newToken.(db.APIToken), nil
}
func (d *BoltDb) GetAPIToken(tokenID string) (token db.APIToken, err error) {
var t globalToken
err = d.getObject(0, globalTokenObject, strObjectID(tokenID), &t)
if err != nil {
return
}
err = d.getObject(t.UserID, db.TokenProps, strObjectID(tokenID), &token)
return
}
func (d *BoltDb) ExpireAPIToken(userID int, tokenID string) (err error) {
var token db.APIToken
err = d.getObject(userID, db.TokenProps, strObjectID(tokenID), &token)
if err != nil {
return
}
token.Expired = true
err = d.updateObject(userID, db.TokenProps, token)
return
}
2022-11-09 09:10:42 +01:00
func (d *BoltDb) DeleteAPIToken(userID int, tokenID string) (err error) {
err = d.ExpireAPIToken(userID, tokenID)
if err != nil {
return
}
2022-11-09 10:02:48 +01:00
err = d.deleteObject(userID, db.TokenProps, strObjectID(tokenID), nil)
2022-11-09 09:10:42 +01:00
return
}
func (d *BoltDb) GetSession(userID int, sessionID int) (session db.Session, err error) {
err = d.getObject(userID, db.SessionProps, intObjectID(sessionID), &session)
return
}
func (d *BoltDb) ExpireSession(userID int, sessionID int) (err error) {
var session db.Session
err = d.getObject(userID, db.SessionProps, intObjectID(sessionID), &session)
if err != nil {
return
}
session.Expired = true
err = d.updateObject(userID, db.SessionProps, session)
return
}
func (d *BoltDb) TouchSession(userID int, sessionID int) (err error) {
var session db.Session
err = d.getObject(userID, db.SessionProps, intObjectID(sessionID), &session)
if err != nil {
return
}
session.LastActive = time.Now()
err = d.updateObject(userID, db.SessionProps, session)
return
}
func (d *BoltDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) {
err = d.getObjects(userID, db.TokenProps, db.RetrieveQueryParams{}, nil, &tokens)
return
}