mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
|
package bolt
|
||
|
|
||
|
import (
|
||
|
"github.com/ansible-semaphore/semaphore/db"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
|
||
|
var globalTokenObject = db.ObjectProperties{
|
||
|
TableName: "token",
|
||
|
}
|
||
|
|
||
|
type globalToken struct {
|
||
|
ID string `db:"id" json:"id"`
|
||
|
UserID int `db:"user_id" json:"user_id"`
|
||
|
}
|
||
|
|
||
|
|
||
|
func (d *BoltDb) CreateSession(session db.Session) (db.Session, error) {
|
||
|
newSession, err := d.createObject(session.UserID, db.SessionObject, 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.TokenObject, 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) {
|
||
|
err = d.getObject(0, globalTokenObject, strObjectID(tokenID), &token)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (d *BoltDb) ExpireAPIToken(userID int, tokenID string) (err error) {
|
||
|
var token db.APIToken
|
||
|
err = d.getObject(userID, db.TokenObject, strObjectID(tokenID), &token)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
token.Expired = true
|
||
|
err = d.updateObject(userID, db.TokenObject, token)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (d *BoltDb) GetSession(userID int, sessionID int) (session db.Session, err error) {
|
||
|
err = d.getObject(userID, db.SessionObject, intObjectID(sessionID), &session)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (d *BoltDb) ExpireSession(userID int, sessionID int) (err error) {
|
||
|
var session db.Session
|
||
|
err = d.getObject(userID, db.SessionObject, intObjectID(sessionID), &session)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
session.Expired = true
|
||
|
err = d.updateObject(userID, db.SessionObject, session)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (d *BoltDb) TouchSession(userID int, sessionID int) (err error) {
|
||
|
var session db.Session
|
||
|
err = d.getObject(userID, db.SessionObject, intObjectID(sessionID), &session)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
session.LastActive = time.Now()
|
||
|
err = d.updateObject(userID, db.SessionObject, session)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (d *BoltDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) {
|
||
|
err = d.getObjects(userID, db.SessionObject, db.RetrieveQueryParams{}, &tokens)
|
||
|
return
|
||
|
}
|
||
|
|