2024-02-20 13:46:15 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-09-22 05:13:05 +00:00
|
|
|
"context"
|
2024-02-20 13:46:15 +00:00
|
|
|
"fmt"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2024-03-22 16:28:27 +00:00
|
|
|
"time"
|
2024-03-20 12:56:43 +00:00
|
|
|
|
2024-09-22 05:13:05 +00:00
|
|
|
"github.com/eko/gocache/lib/v4/cache"
|
|
|
|
"github.com/eko/gocache/lib/v4/marshaler"
|
|
|
|
"github.com/eko/gocache/lib/v4/store"
|
2024-05-27 15:00:49 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
|
2024-02-20 13:46:15 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-03-22 16:28:27 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-02-20 13:46:15 +00:00
|
|
|
)
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
func Authenticate(sessionId uint) (ctx models.AuthTicket, perms map[string]any, err error) {
|
|
|
|
if ctx, err = GetAuthContext(sessionId); err == nil {
|
2024-05-17 12:34:34 +00:00
|
|
|
var heldPerms map[string]any
|
|
|
|
rawHeldPerms, _ := jsoniter.Marshal(ctx.Account.PermNodes)
|
|
|
|
_ = jsoniter.Unmarshal(rawHeldPerms, &heldPerms)
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
perms = FilterPermNodes(heldPerms, ctx.Claims)
|
2024-11-23 13:35:57 +00:00
|
|
|
ctx.Account.PermNodes = perms
|
2024-03-23 02:15:25 +00:00
|
|
|
return
|
2024-03-22 16:28:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 02:15:25 +00:00
|
|
|
err = fiber.NewError(fiber.StatusUnauthorized, err.Error())
|
|
|
|
return
|
2024-03-22 16:28:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
func GetAuthContextCacheKey(sessionId uint) string {
|
|
|
|
return fmt.Sprintf("auth-context#%d", sessionId)
|
2024-09-22 05:13:05 +00:00
|
|
|
}
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
func GetAuthContext(sessionId uint) (models.AuthTicket, error) {
|
2024-03-22 16:28:27 +00:00
|
|
|
var err error
|
2024-10-23 16:46:59 +00:00
|
|
|
var ctx models.AuthTicket
|
2024-03-22 16:28:27 +00:00
|
|
|
|
2024-09-22 05:13:05 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
|
2024-11-03 13:31:32 +00:00
|
|
|
key := GetAuthContextCacheKey(sessionId)
|
|
|
|
if val, err := marshal.Get(context.Background(), key, new(models.AuthTicket)); err == nil {
|
2024-10-23 16:46:59 +00:00
|
|
|
ctx = *val.(*models.AuthTicket)
|
2024-05-17 11:37:58 +00:00
|
|
|
} else {
|
2024-10-23 16:46:59 +00:00
|
|
|
ctx, err = CacheAuthContext(sessionId)
|
|
|
|
log.Debug().Uint("session", sessionId).Msg("Created a new auth context cache")
|
2024-03-22 16:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
func CacheAuthContext(sessionId uint) (models.AuthTicket, error) {
|
2024-03-22 16:28:27 +00:00
|
|
|
// Query data from primary database
|
2024-10-23 16:46:59 +00:00
|
|
|
var ticket models.AuthTicket
|
|
|
|
if err := database.C.
|
|
|
|
Where("id = ?", sessionId).
|
|
|
|
Preload("Account").
|
|
|
|
First(&ticket).Error; err != nil {
|
|
|
|
return ticket, fmt.Errorf("invalid auth ticket: %v", err)
|
2024-04-21 04:20:06 +00:00
|
|
|
} else if err := ticket.IsAvailable(); err != nil {
|
2024-10-23 16:46:59 +00:00
|
|
|
return ticket, fmt.Errorf("unavailable auth ticket: %v", err)
|
2024-02-20 13:46:15 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 04:20:06 +00:00
|
|
|
user, err := GetAccount(ticket.AccountID)
|
2024-02-20 13:46:15 +00:00
|
|
|
if err != nil {
|
2024-10-23 16:46:59 +00:00
|
|
|
return ticket, fmt.Errorf("invalid account: %v", err)
|
2024-03-22 16:28:27 +00:00
|
|
|
}
|
2024-07-24 09:23:44 +00:00
|
|
|
groups, err := GetUserAccountGroup(user)
|
|
|
|
if err != nil {
|
2024-10-23 16:46:59 +00:00
|
|
|
return ticket, fmt.Errorf("unable to get account groups: %v", err)
|
2024-07-24 09:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range groups {
|
|
|
|
for k, v := range group.PermNodes {
|
|
|
|
if _, ok := user.PermNodes[k]; !ok {
|
|
|
|
user.PermNodes[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-23 13:44:14 +00:00
|
|
|
ticket.Account = user
|
2024-03-22 16:28:27 +00:00
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
// Put the data into the cache
|
2024-09-22 05:13:05 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
2024-07-17 05:27:16 +00:00
|
|
|
|
2024-11-03 13:31:32 +00:00
|
|
|
key := GetAuthContextCacheKey(sessionId)
|
|
|
|
err = marshal.Set(
|
|
|
|
context.Background(),
|
|
|
|
key,
|
2024-10-23 16:46:59 +00:00
|
|
|
ticket,
|
2024-11-03 13:31:32 +00:00
|
|
|
store.WithExpiration(10*time.Minute),
|
2024-09-22 05:13:05 +00:00
|
|
|
store.WithTags([]string{"auth-context", fmt.Sprintf("user#%d", user.ID)}),
|
|
|
|
)
|
2024-07-17 05:27:16 +00:00
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
return ticket, nil
|
2024-05-17 11:37:58 +00:00
|
|
|
}
|
2024-03-22 16:28:27 +00:00
|
|
|
|
2024-05-17 11:37:58 +00:00
|
|
|
func InvalidAuthCacheWithUser(userId uint) {
|
2024-09-22 05:13:05 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
2024-10-23 16:46:59 +00:00
|
|
|
ctx := context.Background()
|
2024-09-22 05:13:05 +00:00
|
|
|
|
|
|
|
cacheManager.Invalidate(
|
2024-10-23 16:46:59 +00:00
|
|
|
ctx,
|
2024-09-22 05:13:05 +00:00
|
|
|
store.WithInvalidateTags([]string{"auth-context", fmt.Sprintf("user#%d", userId)}),
|
|
|
|
)
|
2024-02-20 13:46:15 +00:00
|
|
|
}
|