Bots aka. automated accounts

This commit is contained in:
2024-08-24 23:49:19 +08:00
parent 8f61253bd3
commit b22657d09f
8 changed files with 168 additions and 31 deletions

View File

@ -42,7 +42,7 @@ func RollApiKey(key models.ApiKey) (models.ApiKey, error) {
return key, err
}
ticket, err := RotateTicket(ticket)
ticket, err := RotateTicket(ticket, true)
if err != nil {
return key, err
} else {

View File

@ -0,0 +1,24 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func GetBotCount(user models.Account) (int64, error) {
var count int64
if err := database.C.Where("automated_id = ?", user.ID).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func NewBot(user models.Account, bot models.Account) (models.Account, error) {
bot.AutomatedBy = &user
bot.AutomatedID = &user.ID
if err := database.C.Save(&bot).Error; err != nil {
return bot, err
}
return bot, nil
}

View File

@ -18,6 +18,18 @@ func GetThirdClient(id string) (models.ThirdClient, error) {
return client, nil
}
func GetThirdClientWithUser(id string, userId uint) (models.ThirdClient, error) {
var client models.ThirdClient
if err := database.C.Where(&models.ThirdClient{
Alias: id,
AccountID: &userId,
}).First(&client).Error; err != nil {
return client, err
}
return client, nil
}
func GetThirdClientWithSecret(id, secret string) (models.ThirdClient, error) {
client, err := GetThirdClient(id)
if err != nil {

View File

@ -147,10 +147,13 @@ func ActiveTicketWithMFA(ticket models.AuthTicket, factor models.AuthFactor, cod
return ticket, nil
}
func RotateTicket(ticket models.AuthTicket) (models.AuthTicket, error) {
func RotateTicket(ticket models.AuthTicket, fullyRestart ...bool) (models.AuthTicket, error) {
ticket.GrantToken = lo.ToPtr(uuid.NewString())
ticket.AccessToken = lo.ToPtr(uuid.NewString())
ticket.RefreshToken = lo.ToPtr(uuid.NewString())
if len(fullyRestart) > 0 && fullyRestart[0] {
ticket.LastGrantAt = nil
}
err := database.C.Save(&ticket).Error
return ticket, err
}