b2: Fix link sharing #3314

This commit is contained in:
yparitcher 2019-07-03 20:19:06 -04:00 committed by Nick Craig-Wood
parent b4266da4eb
commit d72e4105fb

View file

@ -132,6 +132,7 @@ minimum size.`,
This is usually set to a Cloudflare CDN URL as Backblaze offers This is usually set to a Cloudflare CDN URL as Backblaze offers
free egress for data downloaded through the Cloudflare network. free egress for data downloaded through the Cloudflare network.
This is probably only useful for a public bucket.
Leave blank if you want to use the endpoint provided by Backblaze.`, Leave blank if you want to use the endpoint provided by Backblaze.`,
Advanced: true, Advanced: true,
}, { }, {
@ -450,6 +451,16 @@ func (f *Fs) authorizeAccount() error {
return nil return nil
} }
// hasPermission returns if the current AuthorizationToken has the selected permission
func (f *Fs) hasPermission(permission string) bool {
for _, capability := range f.info.Allowed.Capabilities {
if capability == permission {
return true
}
}
return false
}
// getUploadURL returns the upload info with the UploadURL and the AuthorizationToken // getUploadURL returns the upload info with the UploadURL and the AuthorizationToken
// //
// This should be returned with returnUploadURL when finished // This should be returned with returnUploadURL when finished
@ -1181,6 +1192,9 @@ func (f *Fs) getDownloadAuthorization(remote string) (authorization string, err
if validDurationInSeconds <= 0 || validDurationInSeconds > 604800 { if validDurationInSeconds <= 0 || validDurationInSeconds > 604800 {
return "", errors.New("--b2-download-auth-duration must be between 1 sec and 1 week") return "", errors.New("--b2-download-auth-duration must be between 1 sec and 1 week")
} }
if !f.hasPermission("shareFiles") {
return "", errors.New("sharing a file link requires the shareFiles permission")
}
bucketID, err := f.getBucketID() bucketID, err := f.getBucketID()
if err != nil { if err != nil {
return "", err return "", err
@ -1191,7 +1205,7 @@ func (f *Fs) getDownloadAuthorization(remote string) (authorization string, err
} }
var request = api.GetDownloadAuthorizationRequest{ var request = api.GetDownloadAuthorizationRequest{
BucketID: bucketID, BucketID: bucketID,
FileNamePrefix: remote, FileNamePrefix: path.Join(f.root, remote),
ValidDurationInSeconds: validDurationInSeconds, ValidDurationInSeconds: validDurationInSeconds,
} }
var response api.GetDownloadAuthorizationResponse var response api.GetDownloadAuthorizationResponse