✨ Bots aka. automated accounts
This commit is contained in:
@ -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 {
|
||||
|
24
pkg/internal/services/bots.go
Normal file
24
pkg/internal/services/bots.go
Normal 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
|
||||
}
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user