2016-02-02 20:45:05 +01:00
|
|
|
package swift
|
|
|
|
|
2017-12-14 15:23:55 +01:00
|
|
|
import (
|
|
|
|
"net/http"
|
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
|
|
|
|
func (a *auth) StorageUrl(Internal bool) string {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The CDN url if available
|
|
|
|
func (a *auth) CdnUrl() string {
|
|
|
|
if a.parentAuth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.parentAuth.CdnUrl()
|
2016-02-02 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var _ swift.Authenticator = (*auth)(nil)
|