2016-02-02 19:45:05 +00:00
|
|
|
package swift
|
|
|
|
|
2017-12-14 14:23:55 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
2019-03-17 17:37:54 +00:00
|
|
|
"time"
|
2016-02-02 19:45:05 +00:00
|
|
|
|
2017-12-14 14:23:55 +00:00
|
|
|
"github.com/ncw/swift"
|
|
|
|
)
|
|
|
|
|
|
|
|
// auth is an authenticator for swift. It overrides the StorageUrl
|
|
|
|
// and AuthToken with fixed values.
|
2016-02-02 19:45:05 +00:00
|
|
|
type auth struct {
|
2017-12-14 14:23:55 +00:00
|
|
|
parentAuth swift.Authenticator
|
2016-02-02 19:45:05 +00:00
|
|
|
storageURL string
|
2017-12-14 14:23:55 +00:00
|
|
|
authToken string
|
2016-02-02 19:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newAuth creates a swift authenticator wrapper to override the
|
2017-12-14 14:23:55 +00:00
|
|
|
// StorageUrl and AuthToken values.
|
|
|
|
//
|
|
|
|
// Note that parentAuth can be nil
|
|
|
|
func newAuth(parentAuth swift.Authenticator, storageURL string, authToken string) *auth {
|
2016-02-02 19:45:05 +00:00
|
|
|
return &auth{
|
2017-12-14 14:23:55 +00: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 19:45:05 +00:00
|
|
|
}
|
2017-12-14 14:23:55 +00: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 19:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The public storage URL - set Internal to true to read
|
|
|
|
// internal/service net URL
|
2018-05-05 10:49:03 +00:00
|
|
|
func (a *auth) StorageUrl(Internal bool) string { // nolint
|
2016-02-02 19:45:05 +00:00
|
|
|
if a.storageURL != "" {
|
|
|
|
return a.storageURL
|
|
|
|
}
|
2017-12-14 14:23:55 +00: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 17:37:54 +00: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 14:23:55 +00:00
|
|
|
// The CDN url if available
|
2018-05-05 10:49:03 +00:00
|
|
|
func (a *auth) CdnUrl() string { // nolint
|
2017-12-14 14:23:55 +00:00
|
|
|
if a.parentAuth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.parentAuth.CdnUrl()
|
2016-02-02 19:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2019-03-17 17:37:54 +00:00
|
|
|
var (
|
|
|
|
_ swift.Authenticator = (*auth)(nil)
|
|
|
|
_ swift.Expireser = (*auth)(nil)
|
|
|
|
)
|