Skip to content

Commit

Permalink
Add an auth token method to objects.
Browse files Browse the repository at this point in the history
Also, plumb this down the stack.
  • Loading branch information
kurin committed Jul 6, 2018
1 parent 97eb90f commit aef613c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
21 changes: 20 additions & 1 deletion b2/b2.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"sync"
Expand Down Expand Up @@ -763,5 +764,23 @@ func (b *Bucket) getObject(ctx context.Context, name string) (*Object, error) {
// in a private bucket. Only objects that begin with prefix can be accessed.
// The token expires after the given duration.
func (b *Bucket) AuthToken(ctx context.Context, prefix string, valid time.Duration) (string, error) {
return b.b.getDownloadAuthorization(ctx, prefix, valid)
return b.b.getDownloadAuthorization(ctx, prefix, valid, "")
}

// AuthURL returns a URL for the given object with embedded token and,
// possibly, b2ContentDisposition arguments.
func (o *Object) AuthURL(ctx context.Context, valid time.Duration, b2cd string) (*url.URL, error) {
token, err := o.b.b.getDownloadAuthorization(ctx, o.name, valid, b2cd)
if err != nil {
return nil, err
}
urlString := fmt.Sprintf("%s/file/%s/%s?Authorization=%s", o.b.BaseURL(), o.b.Name(), o.name, token)
if b2cd != "" {
urlString = fmt.Sprintf("%s&b2ContentDisposition=%s", urlString, b2cd)
}
u, err := url.Parse(urlString)
if err != nil {
return nil, err
}
return u, nil
}
6 changes: 3 additions & 3 deletions b2/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type beBucketInterface interface {
listUnfinishedLargeFiles(context.Context, int, string) ([]beFileInterface, string, error)
downloadFileByName(context.Context, string, int64, int64) (beFileReaderInterface, error)
hideFile(context.Context, string) (beFileInterface, error)
getDownloadAuthorization(context.Context, string, time.Duration) (string, error)
getDownloadAuthorization(context.Context, string, time.Duration, string) (string, error)
baseURL() string
file(string, string) beFileInterface
}
Expand Down Expand Up @@ -412,11 +412,11 @@ func (b *beBucket) hideFile(ctx context.Context, name string) (beFileInterface,
return file, nil
}

func (b *beBucket) getDownloadAuthorization(ctx context.Context, p string, v time.Duration) (string, error) {
func (b *beBucket) getDownloadAuthorization(ctx context.Context, p string, v time.Duration, s string) (string, error) {
var tok string
f := func() error {
g := func() error {
t, err := b.b2bucket.getDownloadAuthorization(ctx, p, v)
t, err := b.b2bucket.getDownloadAuthorization(ctx, p, v, s)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions b2/baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type b2BucketInterface interface {
listUnfinishedLargeFiles(context.Context, int, string) ([]b2FileInterface, string, error)
downloadFileByName(context.Context, string, int64, int64) (b2FileReaderInterface, error)
hideFile(context.Context, string) (b2FileInterface, error)
getDownloadAuthorization(context.Context, string, time.Duration) (string, error)
getDownloadAuthorization(context.Context, string, time.Duration, string) (string, error)
baseURL() string
file(string, string) b2FileInterface
}
Expand Down Expand Up @@ -351,8 +351,8 @@ func (b *b2Bucket) hideFile(ctx context.Context, name string) (b2FileInterface,
return &b2File{f}, nil
}

func (b *b2Bucket) getDownloadAuthorization(ctx context.Context, p string, v time.Duration) (string, error) {
return b.b.GetDownloadAuthorization(ctx, p, v)
func (b *b2Bucket) getDownloadAuthorization(ctx context.Context, p string, v time.Duration, s string) (string, error) {
return b.b.GetDownloadAuthorization(ctx, p, v, s)
}

func (b *b2Bucket) baseURL() string {
Expand Down
9 changes: 5 additions & 4 deletions base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,12 @@ func (b *Bucket) ListFileVersions(ctx context.Context, count int, startName, sta
}

// GetDownloadAuthorization wraps b2_get_download_authorization.
func (b *Bucket) GetDownloadAuthorization(ctx context.Context, prefix string, valid time.Duration) (string, error) {
func (b *Bucket) GetDownloadAuthorization(ctx context.Context, prefix string, valid time.Duration, contentDisposition string) (string, error) {
b2req := &b2types.GetDownloadAuthorizationRequest{
BucketID: b.id,
Prefix: prefix,
Valid: int(valid.Seconds()),
BucketID: b.id,
Prefix: prefix,
Valid: int(valid.Seconds()),
ContentDisposition: contentDisposition,
}
b2resp := &b2types.GetDownloadAuthorizationResponse{}
headers := map[string]string{
Expand Down
7 changes: 4 additions & 3 deletions internal/b2types/b2types.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ type GetFileInfoResponse struct {
}

type GetDownloadAuthorizationRequest struct {
BucketID string `json:"bucketId"`
Prefix string `json:"fileNamePrefix"`
Valid int `json:"validDurationInSeconds"`
BucketID string `json:"bucketId"`
Prefix string `json:"fileNamePrefix"`
Valid int `json:"validDurationInSeconds"`
ContentDisposition string `json:"b2ContentDisposition,omitempty"`
}

type GetDownloadAuthorizationResponse struct {
Expand Down

0 comments on commit aef613c

Please sign in to comment.