From 3297937e53b632500a04d1d9a9e249f0fc5a93e9 Mon Sep 17 00:00:00 2001 From: ila <2+ila@noreply.git.ilapage.cn> Date: Thu, 13 Aug 2026 12:23:51 +0800 Subject: [PATCH] feat: complete split camera credentials (#50) --- Sense/server/app/sense/device/service.go | 33 +++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/Sense/server/app/sense/device/service.go b/Sense/server/app/sense/device/service.go index 0f7da54..0ea037c 100644 --- a/Sense/server/app/sense/device/service.go +++ b/Sense/server/app/sense/device/service.go @@ -34,7 +34,7 @@ func (s *Service) Create(ctx context.Context, actor identity.Principal, name, lo adapter = AdapterReady } now := s.now().UTC() - item := Device{ID: newID(), Name: name, Location: strings.TrimSpace(location), Modality: modality, Capabilities: capabilities, Status: StatusPending, AdapterStatus: adapter, Version: 1, CreatedAt: now, UpdatedAt: now} + item := Device{ID: newID(), Name: name, Location: strings.TrimSpace(location), Modality: modality, Capabilities: capabilities, Status: StatusPending, AdapterStatus: adapter, RTSPCredentialSameAsONVIF: true, Version: 1, CreatedAt: now, UpdatedAt: now} if err := s.store.Create(ctx, item); err != nil { return Device{}, err } @@ -45,7 +45,9 @@ func (s *Service) Get(ctx context.Context, id string) (Device, error) { item, err := s.store.Get(ctx, id) if err == nil { item.CredentialConfigured = len(item.CredentialCiphertext) > 0 + item.RTSPCredentialConfigured = len(item.RTSPCredentialCiphertext) > 0 item.CredentialCiphertext = nil + item.RTSPCredentialCiphertext = nil } return item, err } @@ -53,6 +55,7 @@ func (s *Service) List(ctx context.Context, filter ListFilter) (Page, error) { page, err := s.store.List(ctx, filter) for i := range page.Items { page.Items[i].CredentialCiphertext = nil + page.Items[i].RTSPCredentialCiphertext = nil } return page, err } @@ -74,31 +77,50 @@ func (s *Service) Update(ctx context.Context, actor identity.Principal, id, name } identity.RecordAudit(ctx, actor.UserID, "device.update", id, "success", map[string]any{"version": item.Version}) item.CredentialConfigured = len(item.CredentialCiphertext) > 0 + item.RTSPCredentialConfigured = len(item.RTSPCredentialCiphertext) > 0 item.CredentialCiphertext = nil + item.RTSPCredentialCiphertext = nil return item, nil } func (s *Service) SetCredential(ctx context.Context, actor identity.Principal, id, username, password string) (Device, error) { - if strings.TrimSpace(username) == "" || password == "" { + return s.SetCredentials(ctx, actor, id, username, password, true, "", "") +} +func (s *Service) SetCredentials(ctx context.Context, actor identity.Principal, id, onvifUsername, onvifPassword string, rtspSame bool, rtspUsername, rtspPassword string) (Device, error) { + if strings.TrimSpace(onvifUsername) == "" || onvifPassword == "" { return Device{}, fmt.Errorf("用户名和密码不能为空") } + if !rtspSame && (strings.TrimSpace(rtspUsername) == "" || rtspPassword == "") { + return Device{}, fmt.Errorf("RTSP 用户名和密码不能为空") + } item, err := s.store.Get(ctx, id) if err != nil { return Device{}, err } - ciphertext, err := s.vault.Encrypt(username, password) + ciphertext, err := s.vault.Encrypt(onvifUsername, onvifPassword) if err != nil { return Device{}, err } expected := item.Version item.CredentialCiphertext = ciphertext + item.RTSPCredentialSameAsONVIF = rtspSame + if rtspSame { + item.RTSPCredentialCiphertext = append([]byte(nil), ciphertext...) + } else { + item.RTSPCredentialCiphertext, err = s.vault.Encrypt(rtspUsername, rtspPassword) + if err != nil { + return Device{}, err + } + } item.CredentialConfigured = true + item.RTSPCredentialConfigured = true item.Version++ item.UpdatedAt = s.now().UTC() if err := s.store.Update(ctx, item, expected); err != nil { return Device{}, err } - identity.RecordAudit(ctx, actor.UserID, "device.credential.update", id, "success", map[string]any{"configured": true}) + identity.RecordAudit(ctx, actor.UserID, "device.credential.update", id, "success", map[string]any{"configured": true, "rtsp_same_as_onvif": rtspSame}) item.CredentialCiphertext = nil + item.RTSPCredentialCiphertext = nil return item, nil } func (s *Service) Disable(ctx context.Context, actor identity.Principal, id string, expected int64) (Device, error) { @@ -114,7 +136,9 @@ func (s *Service) Disable(ctx context.Context, actor identity.Principal, id stri } identity.RecordAudit(ctx, actor.UserID, "device.disable", id, "success", nil) item.CredentialConfigured = len(item.CredentialCiphertext) > 0 + item.RTSPCredentialConfigured = len(item.RTSPCredentialCiphertext) > 0 item.CredentialCiphertext = nil + item.RTSPCredentialCiphertext = nil return item, nil } func newID() string { @@ -124,3 +148,4 @@ func newID() string { } return "dev_" + hex.EncodeToString(b) } +