Fix Nextcloud user creation password encoding

- Replace manual form encoding with url.Values.Encode() for proper URL encoding
- Fixes issue where passwords with special characters were malformed
- Ensures password sent to Nextcloud matches password stored in database
- This fixes WebDAV authentication failures for auto-provisioned users
This commit is contained in:
Leon Bösche
2026-01-11 01:30:40 +01:00
parent ed22c5eda4
commit fd4224d1da

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
@@ -14,14 +15,15 @@ import (
func CreateNextcloudUser(nextcloudBaseURL, adminUser, adminPass, username, password string) error {
// Remove any path from base URL, we need just the scheme://host:port
baseURL := strings.Split(nextcloudBaseURL, "/remote.php")[0]
url := fmt.Sprintf("%s/ocs/v1.php/cloud/users", baseURL)
urlStr := fmt.Sprintf("%s/ocs/v1.php/cloud/users", baseURL)
// OCS API expects form-encoded data, not JSON
formData := fmt.Sprintf("userid=%s&password=%s",
strings.ReplaceAll(username, " ", "+"),
strings.ReplaceAll(password, " ", "+"))
// OCS API expects form-encoded data with proper URL encoding
formData := url.Values{
"userid": {username},
"password": {password},
}.Encode()
req, err := http.NewRequest("POST", url, bytes.NewBufferString(formData))
req, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(formData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}