Skip to content
Snippets Groups Projects
Commit 2b203e8b authored by ale's avatar ale
Browse files

support remote authentication credentials in URL

parent 5fb79e7c
Branches
Tags
No related merge requests found
...@@ -8,8 +8,10 @@ import ( ...@@ -8,8 +8,10 @@ import (
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"time" "time"
) )
...@@ -27,11 +29,32 @@ type SyncClient interface { ...@@ -27,11 +29,32 @@ type SyncClient interface {
type remoteServer struct { type remoteServer struct {
client *http.Client client *http.Client
remoteURL string remoteURL string
username string
password string
}
func extractAuth(uri string) (string, string, string) {
parsed, err := url.Parse(uri)
if err != nil {
return uri, "", ""
}
if strings.Contains("@", parsed.Host) {
hostParts := strings.SplitN(parsed.Host, "@", 2)
parsed.Host = hostParts[1]
authParts := strings.SplitN(hostParts[0], ":", 2)
if len(authParts) == 2 {
return parsed.String(), authParts[0], authParts[1]
}
}
return uri, "", ""
} }
func NewRemoteServer(remoteURL string) *remoteServer { func NewRemoteServer(remoteURL string) *remoteServer {
remoteURL, username, password := extractAuth(remoteURL)
return &remoteServer{ return &remoteServer{
remoteURL: remoteURL, remoteURL: remoteURL,
username: username,
password: password,
client: &http.Client{}, client: &http.Client{},
} }
} }
...@@ -48,6 +71,9 @@ func (r *remoteServer) DiffRequest(diffreq *diffRequest) (*diffResponse, error) ...@@ -48,6 +71,9 @@ func (r *remoteServer) DiffRequest(diffreq *diffRequest) (*diffResponse, error)
return nil, err return nil, err
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
if r.username != "" {
req.SetBasicAuth(r.username, r.password)
}
resp, err := r.client.Do(req) resp, err := r.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -120,7 +146,10 @@ func (r *remoteServer) SendBook(book *Book, files []*File) error { ...@@ -120,7 +146,10 @@ func (r *remoteServer) SendBook(book *Book, files []*File) error {
if err != nil { if err != nil {
return err return err
} }
req.Header.Add("Content-Type", w.FormDataContentType()) req.Header.Set("Content-Type", w.FormDataContentType())
if r.username != "" {
req.SetBasicAuth(r.username, r.password)
}
resp, err := r.client.Do(req) resp, err := r.client.Do(req)
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment