From 6d0c48c45dfbcf2546385fb801e82d79ffb9a949 Mon Sep 17 00:00:00 2001 From: langhuihui <178529795@qq.com> Date: Thu, 26 Dec 2024 20:22:31 +0800 Subject: [PATCH] feat: add get secret api --- plugin.go | 36 ++++++++++++++++++++++++++++++------ server.go | 3 --- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/plugin.go b/plugin.go index 1d04d64..fbd9525 100644 --- a/plugin.go +++ b/plugin.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto/md5" + "encoding/hex" "encoding/json" "fmt" "net" @@ -506,13 +507,10 @@ func (p *Plugin) auth(streamPath string, key string, secret string, expire strin return fmt.Errorf("auth failed secret length must be 32") } trueSecret := md5.Sum([]byte(key + streamPath + expire)) - for i := 0; i < 16; i++ { - hex, err := strconv.ParseInt(secret[i<<1:(i<<1)+2], 16, 16) - if trueSecret[i] != byte(hex) || err != nil { - return fmt.Errorf("auth failed invalid secret") - } + if secret == hex.EncodeToString(trueSecret[:]) { + return nil } - return nil + return fmt.Errorf("auth failed invalid secret") } func (p *Plugin) OnSubscribe(streamPath string, args url.Values) { @@ -660,6 +658,32 @@ func (p *Plugin) registerHandler(handlers map[string]http.HandlerFunc) { for patten, handler := range handlers { p.handle(patten, handler) } + if p.config.EnableAuth && p.Server.ServerConfig.EnableLogin { + p.handle("/api/secret/{type}/{streamPath...}", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + authHeader := r.Header.Get("Authorization") + if authHeader == "" { + http.Error(rw, "missing authorization header", http.StatusUnauthorized) + return + } + + tokenString := strings.TrimPrefix(authHeader, "Bearer ") + _, err := p.Server.ValidateToken(tokenString) + if err != nil { + http.Error(rw, "invalid token", http.StatusUnauthorized) + return + } + streamPath := r.PathValue("streamPath") + t := r.PathValue("type") + expire := r.URL.Query().Get("expire") + if t == "publish" { + secret := md5.Sum([]byte(p.config.Publish.Key + streamPath + expire)) + rw.Write([]byte(hex.EncodeToString(secret[:]))) + } else if t == "subscribe" { + secret := md5.Sum([]byte(p.config.Subscribe.Key + streamPath + expire)) + rw.Write([]byte(hex.EncodeToString(secret[:]))) + } + })) + } if rootHandler, ok := p.handler.(http.Handler); ok { p.handle("/", rootHandler) } diff --git a/server.go b/server.go index 9a9ac6e..8560856 100644 --- a/server.go +++ b/server.go @@ -202,9 +202,6 @@ func (s *Server) Start() (err error) { s.Waiting.Logger = s.Logger var httpMux http.Handler = httpConf.CreateHttpMux() - if s.ServerConfig.EnableLogin { - httpMux = auth.Middleware(s)(httpMux) - } mux := runtime.NewServeMux( runtime.WithMarshalerOption("text/plain", &pb.TextPlain{}), runtime.WithForwardResponseOption(func(ctx context.Context, w http.ResponseWriter, m proto.Message) error {