2016-02-03 03:45:05 +08:00
|
|
|
package swift
|
|
|
|
|
2017-12-14 22:23:55 +08:00
|
|
|
import (
|
2021-01-23 01:23:51 +08:00
|
|
|
"context"
|
2017-12-14 22:23:55 +08:00
|
|
|
"net/http"
|
2019-03-18 01:37:54 +08:00
|
|
|
"time"
|
2016-02-03 03:45:05 +08:00
|
|
|
|
2021-01-23 01:23:51 +08:00
|
|
|
"github.com/ncw/swift/v2"
|
2017-12-14 22:23:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// auth is an authenticator for swift. It overrides the StorageUrl
|
|
|
|
// and AuthToken with fixed values.
|
2016-02-03 03:45:05 +08:00
|
|
|
type auth struct {
|
2017-12-14 22:23:55 +08:00
|
|
|
parentAuth swift.Authenticator
|
2016-02-03 03:45:05 +08:00
|
|
|
storageURL string
|
2017-12-14 22:23:55 +08:00
|
|
|
authToken string
|
2016-02-03 03:45:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newAuth creates a swift authenticator wrapper to override the
|
2017-12-14 22:23:55 +08:00
|
|
|
// StorageUrl and AuthToken values.
|
|
|
|
//
|
|
|
|
// Note that parentAuth can be nil
|
|
|
|
func newAuth(parentAuth swift.Authenticator, storageURL string, authToken string) *auth {
|
2016-02-03 03:45:05 +08:00
|
|
|
return &auth{
|
2017-12-14 22:23:55 +08:00
|
|
|
parentAuth: parentAuth,
|
|
|
|
storageURL: storageURL,
|
|
|
|
authToken: authToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request creates an http.Request for the auth - return nil if not needed
|
2021-01-23 01:23:51 +08:00
|
|
|
func (a *auth) Request(ctx context.Context, c *swift.Connection) (*http.Request, error) {
|
2017-12-14 22:23:55 +08:00
|
|
|
if a.parentAuth == nil {
|
|
|
|
return nil, nil
|
2016-02-03 03:45:05 +08:00
|
|
|
}
|
2021-01-23 01:23:51 +08:00
|
|
|
return a.parentAuth.Request(ctx, c)
|
2017-12-14 22:23:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Response parses the http.Response
|
2021-01-23 01:23:51 +08:00
|
|
|
func (a *auth) Response(ctx context.Context, resp *http.Response) error {
|
2017-12-14 22:23:55 +08:00
|
|
|
if a.parentAuth == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-23 01:23:51 +08:00
|
|
|
return a.parentAuth.Response(ctx, resp)
|
2016-02-03 03:45:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The public storage URL - set Internal to true to read
|
|
|
|
// internal/service net URL
|
2018-05-05 18:49:03 +08:00
|
|
|
func (a *auth) StorageUrl(Internal bool) string { // nolint
|
2016-02-03 03:45:05 +08:00
|
|
|
if a.storageURL != "" {
|
|
|
|
return a.storageURL
|
|
|
|
}
|
2017-12-14 22:23:55 +08:00
|
|
|
if a.parentAuth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.parentAuth.StorageUrl(Internal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The access token
|
|
|
|
func (a *auth) Token() string {
|
|
|
|
if a.authToken != "" {
|
|
|
|
return a.authToken
|
|
|
|
}
|
|
|
|
if a.parentAuth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.parentAuth.Token()
|
|
|
|
}
|
|
|
|
|
2019-03-18 01:37:54 +08:00
|
|
|
// Expires returns the time the token expires if known or Zero if not.
|
|
|
|
func (a *auth) Expires() (t time.Time) {
|
|
|
|
if do, ok := a.parentAuth.(swift.Expireser); ok {
|
|
|
|
t = do.Expires()
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:23:55 +08:00
|
|
|
// The CDN url if available
|
2018-05-05 18:49:03 +08:00
|
|
|
func (a *auth) CdnUrl() string { // nolint
|
2017-12-14 22:23:55 +08:00
|
|
|
if a.parentAuth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.parentAuth.CdnUrl()
|
2016-02-03 03:45:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2019-03-18 01:37:54 +08:00
|
|
|
var (
|
|
|
|
_ swift.Authenticator = (*auth)(nil)
|
|
|
|
_ swift.Expireser = (*auth)(nil)
|
|
|
|
)
|