✨ User info DirectAccess™
This commit is contained in:
		@@ -6,8 +6,10 @@ import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/internal/cache"
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/nex"
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/proto"
 | 
			
		||||
	"github.com/gofiber/fiber/v2"
 | 
			
		||||
@@ -20,6 +22,19 @@ func userinfoFetch(c *fiber.Ctx) error {
 | 
			
		||||
		return fiber.NewError(fiber.StatusUnauthorized, "user principal data was not found")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if val, err := cachekit.Get[sec.UserInfo](
 | 
			
		||||
		cache.Kcc,
 | 
			
		||||
		cachekit.FKey(cachekit.DAUserInfoPrefix, claims.Session),
 | 
			
		||||
	); err == nil {
 | 
			
		||||
		c.Locals("nex_user", &val)
 | 
			
		||||
		tk, err := IWriter.WriteUserInfoJwt(val)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("unable to sign userinfo: %v", err))
 | 
			
		||||
		}
 | 
			
		||||
		c.Locals("nex_token", tk)
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	service := directory.GetServiceInstanceByType(nex.ServiceTypeAuth)
 | 
			
		||||
	if service != nil {
 | 
			
		||||
		conn, err := service.GetGrpcConn()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										16
									
								
								pkg/internal/cache/redis.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										16
									
								
								pkg/internal/cache/redis.go
									
									
									
									
										vendored
									
									
								
							@@ -1,8 +1,16 @@
 | 
			
		||||
package cache
 | 
			
		||||
 | 
			
		||||
import "github.com/redis/go-redis/v9"
 | 
			
		||||
import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
var Rdb *redis.Client
 | 
			
		||||
	"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
 | 
			
		||||
	"github.com/redis/go-redis/v9"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	Rdb *redis.Client
 | 
			
		||||
	Kcc *cachekit.Conn
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func ConnectRedis(addr, password string, db int) error {
 | 
			
		||||
	Rdb = redis.NewClient(&redis.Options{
 | 
			
		||||
@@ -10,5 +18,9 @@ func ConnectRedis(addr, password string, db int) error {
 | 
			
		||||
		Password: password,
 | 
			
		||||
		DB:       db,
 | 
			
		||||
	})
 | 
			
		||||
	Kcc = &cachekit.Conn{
 | 
			
		||||
		Rd:      Rdb,
 | 
			
		||||
		Timeout: 3 * time.Second,
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								pkg/nex/cachekit/direct_const.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								pkg/nex/cachekit/direct_const.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
package cachekit
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
// Those constants are used to directly get the cached data from redis
 | 
			
		||||
// Formatted like {prefix}#{key}
 | 
			
		||||
const (
 | 
			
		||||
	DAUserInfoPrefix = "userinfo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func FKey(prefix string, key any) string {
 | 
			
		||||
	return fmt.Sprintf("%s#%v", prefix, key)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user