mirror of
https://github.com/h44z/wg-portal.git
synced 2026-11-20 20:53:40 +01:00
Feat/client side key generation (#764)
* feat: expose editable key capability to frontend Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * feat: expose editable keys setting Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * feat: add browser WireGuard key and config helpers Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * feat: support browser-only key generation for self provisioning Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * fix: hide server config actions without private key Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * refactor: share browser WireGuard key helpers Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * refactor: reuse WireGuard browser key helpers Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * fix: address review feedback for client-side key generation - Make generated key fields read-only so the downloaded config always matches the registered public key - Drop unreachable error handling and defensive branches; mirror wg_peer.tpl in the config builder - Keep only the keypair helper shared with KeyGeneraterView - Restore trailing newlines and remove the duplicated help text - Document browser-side key generation and add EditableKeys to the v0 swagger spec Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * test: cover EditableKeys setting and browser-generated peer keys Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * improve ui, fix potential race condition --------- Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> Co-authored-by: Christoph <christoph.h@sprinternet.at>
This commit is contained in:
@@ -2633,6 +2633,9 @@
|
||||
"$ref": "#/definitions/model.SettingsBackendNames"
|
||||
}
|
||||
},
|
||||
"EditableKeys": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"LoginFormVisible": {
|
||||
"type": "boolean"
|
||||
},
|
||||
|
||||
@@ -403,6 +403,8 @@ definitions:
|
||||
items:
|
||||
$ref: '#/definitions/model.SettingsBackendNames'
|
||||
type: array
|
||||
EditableKeys:
|
||||
type: boolean
|
||||
LoginFormVisible:
|
||||
type: boolean
|
||||
MailLinkOnly:
|
||||
|
||||
@@ -141,6 +141,7 @@ func (e ConfigEndpoint) handleSettingsGet() http.HandlerFunc {
|
||||
MailLinkOnly: e.cfg.Mail.LinkOnly,
|
||||
PersistentConfigSupported: e.cfg.Advanced.ConfigStoragePath != "",
|
||||
SelfProvisioning: e.cfg.Core.SelfProvisioningAllowed,
|
||||
EditableKeys: e.cfg.Core.EditableKeys,
|
||||
ApiAdminOnly: e.cfg.Advanced.ApiAdminOnly,
|
||||
WebAuthnEnabled: e.cfg.Auth.WebAuthn.Enabled,
|
||||
MinPasswordLength: e.cfg.Auth.MinPasswordLength,
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/app/api/v0/model"
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
type testControllerManager struct{}
|
||||
|
||||
func (testControllerManager) GetControllerNames() []config.BackendBase {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getSettings(t *testing.T, cfg *config.Config, userId domain.UserIdentifier) model.Settings {
|
||||
t.Helper()
|
||||
|
||||
ep := ConfigEndpoint{cfg: cfg, controllerMgr: testControllerManager{}}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v0/config/settings", nil)
|
||||
req = req.WithContext(domain.SetUserInfo(context.Background(), &domain.ContextUserInfo{Id: userId}))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
ep.handleSettingsGet().ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected status %d, got %d", http.StatusOK, rec.Code)
|
||||
}
|
||||
|
||||
var settings model.Settings
|
||||
if err := json.NewDecoder(rec.Body).Decode(&settings); err != nil {
|
||||
t.Fatalf("failed to decode settings: %v", err)
|
||||
}
|
||||
return settings
|
||||
}
|
||||
|
||||
// TestConfigEndpointSettingsExposesEditableKeys pins the EditableKeys flag that the frontend uses to offer
|
||||
// browser-side key generation in frontend/src/components/UserPeerEditModal.vue.
|
||||
func TestConfigEndpointSettingsExposesEditableKeys(t *testing.T) {
|
||||
for _, editable := range []bool{true, false} {
|
||||
cfg := &config.Config{}
|
||||
cfg.Core.EditableKeys = editable
|
||||
|
||||
if got := getSettings(t, cfg, "user@example.com").EditableKeys; got != editable {
|
||||
t.Fatalf("expected EditableKeys=%v for logged-in users, got %v", editable, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigEndpointSettingsHidesEditableKeysFromAnonymousUsers(t *testing.T) {
|
||||
cfg := &config.Config{}
|
||||
cfg.Core.EditableKeys = true
|
||||
|
||||
if getSettings(t, cfg, domain.CtxUnknownUserId).EditableKeys {
|
||||
t.Fatalf("expected EditableKeys to be hidden from anonymous users")
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ type Settings struct {
|
||||
MailLinkOnly bool `json:"MailLinkOnly"`
|
||||
PersistentConfigSupported bool `json:"PersistentConfigSupported"`
|
||||
SelfProvisioning bool `json:"SelfProvisioning"`
|
||||
EditableKeys bool `json:"EditableKeys"`
|
||||
ApiAdminOnly bool `json:"ApiAdminOnly"`
|
||||
WebAuthnEnabled bool `json:"WebAuthnEnabled"`
|
||||
MinPasswordLength int `json:"MinPasswordLength"`
|
||||
|
||||
@@ -212,6 +212,61 @@ func TestCreatePeer_SetsIdentifier_FromPublicKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreatePeer_KeepsBrowserGeneratedKeyWithoutPrivateKey covers the browser-side key generation flow of
|
||||
// frontend/src/components/UserPeerEditModal.vue: the client only sends its public key, so the server must
|
||||
// neither store a private key nor replace the key pair with a server-generated one.
|
||||
func TestCreatePeer_KeepsBrowserGeneratedKeyWithoutPrivateKey(t *testing.T) {
|
||||
cfg := &config.Config{}
|
||||
cfg.Core.SelfProvisioningAllowed = true
|
||||
cfg.Core.EditableKeys = true
|
||||
|
||||
ctrlMgr := &ControllerManager{
|
||||
controllers: map[domain.InterfaceBackend]backendInstance{
|
||||
config.LocalBackendName: {Implementation: &mockController{}},
|
||||
},
|
||||
}
|
||||
|
||||
db := &mockDB{iface: &domain.Interface{Identifier: "wg0", Type: domain.InterfaceTypeServer}}
|
||||
|
||||
m := Manager{
|
||||
cfg: cfg,
|
||||
bus: &mockBus{},
|
||||
db: db,
|
||||
wg: ctrlMgr,
|
||||
}
|
||||
|
||||
userId := domain.UserIdentifier("user@example.com")
|
||||
ctx := domain.SetUserInfo(context.Background(), &domain.ContextUserInfo{Id: userId, IsAdmin: false})
|
||||
|
||||
pubKey := "BROWSER_GENERATED_PUBLIC_KEY"
|
||||
|
||||
input := &domain.Peer{
|
||||
UserIdentifier: userId,
|
||||
InterfaceIdentifier: domain.InterfaceIdentifier("wg0"),
|
||||
Interface: domain.PeerInterfaceConfig{
|
||||
KeyPair: domain.KeyPair{PublicKey: pubKey},
|
||||
},
|
||||
}
|
||||
|
||||
out, err := m.CreatePeer(ctx, input)
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePeer returned error: %v", err)
|
||||
}
|
||||
|
||||
saved := db.savedPeers[domain.PeerIdentifier(pubKey)]
|
||||
if saved == nil {
|
||||
t.Fatalf("expected peer with identifier %q to be saved in DB", pubKey)
|
||||
}
|
||||
for name, peer := range map[string]*domain.Peer{"returned": out, "saved": saved} {
|
||||
if peer.Interface.PublicKey != pubKey {
|
||||
t.Fatalf("expected %s peer to keep public key %q, got %q", name, pubKey, peer.Interface.PublicKey)
|
||||
}
|
||||
if peer.Interface.PrivateKey != "" {
|
||||
t.Fatalf("expected %s peer to have no private key, got %q", name, peer.Interface.PrivateKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateDefaultPeer_RespectsInterfaceFlag(t *testing.T) {
|
||||
// Arrange
|
||||
cfg := &config.Config{}
|
||||
|
||||
Reference in New Issue
Block a user