aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/client/client.go
diff options
context:
space:
mode:
authorElad <theman@elad.im>2018-08-15 23:41:52 +0800
committerBalint Gabor <balint.g@gmail.com>2018-08-15 23:41:52 +0800
commite8752f4e9f9be3d2932cd4835a5d72d17ac2338b (patch)
tree73f1514fc0134f2f5ef4b467f1076548b8a18bc3 /swarm/api/client/client.go
parent040aa2bb101e5e602308b24812bfbf2451b21174 (diff)
downloadgo-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.tar
go-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.tar.gz
go-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.tar.bz2
go-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.tar.lz
go-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.tar.xz
go-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.tar.zst
go-tangerine-e8752f4e9f9be3d2932cd4835a5d72d17ac2338b.zip
cmd/swarm, swarm: added access control functionality (#17404)
Co-authored-by: Janos Guljas <janos@resenje.org> Co-authored-by: Anton Evangelatov <anton.evangelatov@gmail.com> Co-authored-by: Balint Gabor <balint.g@gmail.com>
Diffstat (limited to 'swarm/api/client/client.go')
-rw-r--r--swarm/api/client/client.go48
1 files changed, 38 insertions, 10 deletions
diff --git a/swarm/api/client/client.go b/swarm/api/client/client.go
index 8a9efe360..3d06e9e1c 100644
--- a/swarm/api/client/client.go
+++ b/swarm/api/client/client.go
@@ -43,6 +43,10 @@ var (
DefaultClient = NewClient(DefaultGateway)
)
+var (
+ ErrUnauthorized = errors.New("unauthorized")
+)
+
func NewClient(gateway string) *Client {
return &Client{
Gateway: gateway,
@@ -188,7 +192,7 @@ func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bo
// DownloadDirectory downloads the files contained in a swarm manifest under
// the given path into a local directory (existing files will be overwritten)
-func (c *Client) DownloadDirectory(hash, path, destDir string) error {
+func (c *Client) DownloadDirectory(hash, path, destDir, credentials string) error {
stat, err := os.Stat(destDir)
if err != nil {
return err
@@ -201,13 +205,20 @@ func (c *Client) DownloadDirectory(hash, path, destDir string) error {
if err != nil {
return err
}
+ if credentials != "" {
+ req.SetBasicAuth("", credentials)
+ }
req.Header.Set("Accept", "application/x-tar")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
- if res.StatusCode != http.StatusOK {
+ switch res.StatusCode {
+ case http.StatusOK:
+ case http.StatusUnauthorized:
+ return ErrUnauthorized
+ default:
return fmt.Errorf("unexpected HTTP status: %s", res.Status)
}
tr := tar.NewReader(res.Body)
@@ -248,7 +259,7 @@ func (c *Client) DownloadDirectory(hash, path, destDir string) error {
// DownloadFile downloads a single file into the destination directory
// if the manifest entry does not specify a file name - it will fallback
// to the hash of the file as a filename
-func (c *Client) DownloadFile(hash, path, dest string) error {
+func (c *Client) DownloadFile(hash, path, dest, credentials string) error {
hasDestinationFilename := false
if stat, err := os.Stat(dest); err == nil {
hasDestinationFilename = !stat.IsDir()
@@ -261,9 +272,9 @@ func (c *Client) DownloadFile(hash, path, dest string) error {
}
}
- manifestList, err := c.List(hash, path)
+ manifestList, err := c.List(hash, path, credentials)
if err != nil {
- return fmt.Errorf("could not list manifest: %v", err)
+ return err
}
switch len(manifestList.Entries) {
@@ -280,13 +291,19 @@ func (c *Client) DownloadFile(hash, path, dest string) error {
if err != nil {
return err
}
+ if credentials != "" {
+ req.SetBasicAuth("", credentials)
+ }
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
-
- if res.StatusCode != http.StatusOK {
+ switch res.StatusCode {
+ case http.StatusOK:
+ case http.StatusUnauthorized:
+ return ErrUnauthorized
+ default:
return fmt.Errorf("unexpected HTTP status: expected 200 OK, got %d", res.StatusCode)
}
filename := ""
@@ -367,13 +384,24 @@ func (c *Client) DownloadManifest(hash string) (*api.Manifest, bool, error) {
// - a prefix of "dir1/" would return [dir1/dir2/, dir1/file3.txt]
//
// where entries ending with "/" are common prefixes.
-func (c *Client) List(hash, prefix string) (*api.ManifestList, error) {
- res, err := http.DefaultClient.Get(c.Gateway + "/bzz-list:/" + hash + "/" + prefix)
+func (c *Client) List(hash, prefix, credentials string) (*api.ManifestList, error) {
+ req, err := http.NewRequest(http.MethodGet, c.Gateway+"/bzz-list:/"+hash+"/"+prefix, nil)
+ if err != nil {
+ return nil, err
+ }
+ if credentials != "" {
+ req.SetBasicAuth("", credentials)
+ }
+ res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
- if res.StatusCode != http.StatusOK {
+ switch res.StatusCode {
+ case http.StatusOK:
+ case http.StatusUnauthorized:
+ return nil, ErrUnauthorized
+ default:
return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
}
var list api.ManifestList