@@ -2,10 +2,13 @@ package middleware
22
33import (
44 "context"
5+ "encoding/json"
6+ "log/slog"
57 "net/http"
68 "strings"
79
810 "github.com/labstack/echo/v4"
11+ "github.com/redis/go-redis/v9"
912
1013 "github.com/chaitin/MonkeyCode/backend/domain"
1114 "github.com/chaitin/MonkeyCode/backend/ent/rule"
@@ -16,15 +19,23 @@ const (
1619 ApiContextKey = "session:apikey"
1720)
1821
22+ type proxyModelKey struct {}
23+
1924type ProxyMiddleware struct {
2025 usecase domain.ProxyUsecase
26+ redis * redis.Client
27+ logger * slog.Logger
2128}
2229
2330func NewProxyMiddleware (
2431 usecase domain.ProxyUsecase ,
32+ redis * redis.Client ,
33+ logger * slog.Logger ,
2534) * ProxyMiddleware {
2635 return & ProxyMiddleware {
2736 usecase : usecase ,
37+ redis : redis ,
38+ logger : logger .With ("module" , "ProxyMiddleware" ),
2839 }
2940}
3041
@@ -39,21 +50,54 @@ func (p *ProxyMiddleware) Auth() echo.MiddlewareFunc {
3950 return c .JSON (http .StatusUnauthorized , echo.Map {"error" : "Unauthorized" })
4051 }
4152
42- key , err := p .usecase .ValidateApiKey (c .Request ().Context (), apiKey )
43- if err != nil {
44- return c .JSON (http .StatusUnauthorized , echo.Map {"error" : "Unauthorized" })
53+ ctx := c .Request ().Context ()
54+ p .logger .With ("apiKey" , apiKey ).DebugContext (ctx , "v1 auth" )
55+ if strings .Contains (apiKey , "." ) {
56+ s , err := p .redis .Get (ctx , apiKey ).Result ()
57+ if err != nil {
58+ p .logger .With ("fn" , "Auth" ).With ("error" , err ).ErrorContext (ctx , "failed to get api key from redis" )
59+ return c .JSON (http .StatusUnauthorized , echo.Map {"error" : "Unauthorized" })
60+ }
61+ var model * domain.Model
62+ if err := json .Unmarshal ([]byte (s ), & model ); err != nil {
63+ p .logger .With ("fn" , "Auth" ).With ("error" , err ).ErrorContext (ctx , "failed to unmarshal model from redis" )
64+ return c .JSON (http .StatusUnauthorized , echo.Map {"error" : "Unauthorized" })
65+ }
66+ parts := strings .Split (apiKey , "." )
67+ if len (parts ) != 2 {
68+ p .logger .With ("fn" , "Auth" ).With ("apiKey" , apiKey ).ErrorContext (ctx , "invalid api key" )
69+ return c .JSON (http .StatusUnauthorized , echo.Map {"error" : "Unauthorized" })
70+ }
71+ ctx = context .WithValue (ctx , proxyModelKey {}, model )
72+ ctx = context .WithValue (ctx , logger.UserIDKey {}, parts [0 ])
73+ c .Set (ApiContextKey , & domain.ApiKey {
74+ UserID : parts [0 ],
75+ Key : apiKey ,
76+ })
77+ } else {
78+ key , err := p .usecase .ValidateApiKey (ctx , apiKey )
79+ if err != nil {
80+ return c .JSON (http .StatusUnauthorized , echo.Map {"error" : "Unauthorized" })
81+ }
82+ ctx = context .WithValue (ctx , logger.UserIDKey {}, key .UserID )
83+ c .Set (ApiContextKey , key )
4584 }
4685
47- ctx := c .Request ().Context ()
48- ctx = context .WithValue (ctx , logger.UserIDKey {}, key .UserID )
4986 ctx = rule .SkipPermission (ctx )
5087 c .SetRequest (c .Request ().WithContext (ctx ))
51- c .Set (ApiContextKey , key )
5288 return next (c )
5389 }
5490 }
5591}
5692
93+ func GetProxyModel (ctx context.Context ) * domain.Model {
94+ m := ctx .Value (proxyModelKey {})
95+ if m == nil {
96+ return nil
97+ }
98+ return m .(* domain.Model )
99+ }
100+
57101func GetApiKey (c echo.Context ) * domain.ApiKey {
58102 i := c .Get (ApiContextKey )
59103 if i == nil {
0 commit comments