2015-11-08 16:29:58 +01:00
|
|
|
package hubic
|
|
|
|
|
|
|
|
import (
|
2019-09-04 21:21:10 +02:00
|
|
|
"context"
|
2015-11-08 16:29:58 +01:00
|
|
|
"net/http"
|
2018-09-04 22:00:36 +02:00
|
|
|
"time"
|
2015-11-08 16:29:58 +01:00
|
|
|
|
2021-01-22 18:23:51 +01:00
|
|
|
"github.com/ncw/swift/v2"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2015-11-08 16:29:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// auth is an authenticator for swift
|
|
|
|
type auth struct {
|
|
|
|
f *Fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAuth creates a swift authenticator
|
|
|
|
func newAuth(f *Fs) *auth {
|
|
|
|
return &auth{
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// Request constructs an http.Request for authentication
|
2015-11-08 16:29:58 +01:00
|
|
|
//
|
|
|
|
// returns nil for not needed
|
2021-01-22 18:23:51 +01:00
|
|
|
func (a *auth) Request(ctx context.Context, c *swift.Connection) (r *http.Request, err error) {
|
2018-09-04 22:00:36 +02:00
|
|
|
const retries = 10
|
|
|
|
for try := 1; try <= retries; try++ {
|
2019-09-04 21:21:10 +02:00
|
|
|
err = a.f.getCredentials(context.TODO())
|
2018-09-04 22:00:36 +02:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
fs.Debugf(a.f, "retrying auth request %d/%d: %v", try, retries, err)
|
2015-11-08 16:29:58 +01:00
|
|
|
}
|
2018-09-04 22:00:36 +02:00
|
|
|
return nil, err
|
2015-11-08 16:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Response parses the result of an http request
|
2021-01-22 18:23:51 +01:00
|
|
|
func (a *auth) Response(ctx context.Context, resp *http.Response) error {
|
2015-11-08 16:29:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The public storage URL - set Internal to true to read
|
|
|
|
// internal/service net URL
|
2018-05-05 12:49:03 +02:00
|
|
|
func (a *auth) StorageUrl(Internal bool) string { // nolint
|
2015-11-08 16:29:58 +01:00
|
|
|
return a.f.credentials.Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// The access token
|
|
|
|
func (a *auth) Token() string {
|
|
|
|
return a.f.credentials.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
// The CDN url if available
|
2018-05-05 12:49:03 +02:00
|
|
|
func (a *auth) CdnUrl() string { // nolint
|
2015-11-08 16:29:58 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var _ swift.Authenticator = (*auth)(nil)
|