2016-02-02 20:45:05 +01:00
|
|
|
package swift
|
|
|
|
|
2017-12-14 15:23:55 +01:00
|
|
|
import (
|
|
|
|
"net/http"
|
2019-03-17 18:37:54 +01:00
|
|
|
"time"
|
2016-02-02 20:45:05 +01:00
|
|
|
|
2017-12-14 15:23:55 +01:00
|
|
|
"github.com/ncw/swift"
|
|
|
|
)
|
|
|
|
|
|
|
|
// auth is an authenticator for swift. It overrides the StorageUrl
|
|
|
|
// and AuthToken with fixed values.
|
2016-02-02 20:45:05 +01:00
|
|
|
type auth struct {
|
2017-12-14 15:23:55 +01:00
|
|
|
parentAuth swift.Authenticator
|
2016-02-02 20:45:05 +01:00
|
|
|
storageURL string
|
2017-12-14 15:23:55 +01:00
|
|
|
authToken string
|
2016-02-02 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newAuth creates a swift authenticator wrapper to override the
|
2017-12-14 15:23:55 +01:00
|
|
|
// StorageUrl and AuthToken values.
|
|
|
|
//
|
|
|
|
// Note that parentAuth can be nil
|
|
|
|
func newAuth(parentAuth swift.Authenticator, storageURL string, authToken string) *auth {
|
2016-02-02 20:45:05 +01:00
|
|
|
return &auth{
|
2017-12-14 15:23:55 +01:00
|
|
|
parentAuth: parentAuth,
|
|
|
|
storageURL: storageURL,
|
|
|
|
authToken: authToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request creates an http.Request for the auth - return nil if not needed
|
|
|
|
func (a *auth) Request(c *swift.Connection) (*http.Request, error) {
|
|
|
|
if a.parentAuth == nil {
|
|
|
|
return nil, nil
|
2016-02-02 20:45:05 +01:00
|
|
|
}
|
2017-12-14 15:23:55 +01:00
|
|
|
return a.parentAuth.Request(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response parses the http.Response
|
|
|
|
func (a *auth) Response(resp *http.Response) error {
|
|
|
|
if a.parentAuth == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a.parentAuth.Response(resp)
|
2016-02-02 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-02-02 20:45:05 +01:00
|
|
|
if a.storageURL != "" {
|
|
|
|
return a.storageURL
|
|
|
|
}
|
2017-12-14 15:23:55 +01: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-17 18:37:54 +01: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 15:23:55 +01:00
|
|
|
// The CDN url if available
|
2018-05-05 12:49:03 +02:00
|
|
|
func (a *auth) CdnUrl() string { // nolint
|
2017-12-14 15:23:55 +01:00
|
|
|
if a.parentAuth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.parentAuth.CdnUrl()
|
2016-02-02 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2019-03-17 18:37:54 +01:00
|
|
|
var (
|
|
|
|
_ swift.Authenticator = (*auth)(nil)
|
|
|
|
_ swift.Expireser = (*auth)(nil)
|
|
|
|
)
|