github.com/gophercloud/gophercloud@v1.11.0/openstack/objectstorage/v1/swauth/requests.go (about) 1 package swauth 2 3 import "github.com/gophercloud/gophercloud" 4 5 // AuthOptsBuilder describes struct types that can be accepted by the Auth call. 6 type AuthOptsBuilder interface { 7 ToAuthOptsMap() (map[string]string, error) 8 } 9 10 // AuthOpts specifies an authentication request. 11 type AuthOpts struct { 12 // User is an Swauth-based username in username:tenant format. 13 User string `h:"X-Auth-User" required:"true"` 14 15 // Key is a secret/password to authenticate the User with. 16 Key string `h:"X-Auth-Key" required:"true"` 17 } 18 19 // ToAuthOptsMap formats an AuthOpts structure into a request body. 20 func (opts AuthOpts) ToAuthOptsMap() (map[string]string, error) { 21 return gophercloud.BuildHeaders(opts) 22 } 23 24 // Auth performs an authentication request for a Swauth-based user. 25 func Auth(c *gophercloud.ProviderClient, opts AuthOptsBuilder) (r GetAuthResult) { 26 h := make(map[string]string) 27 28 if opts != nil { 29 headers, err := opts.ToAuthOptsMap() 30 if err != nil { 31 r.Err = err 32 return 33 } 34 35 for k, v := range headers { 36 h[k] = v 37 } 38 } 39 40 resp, err := c.Request("GET", getURL(c), &gophercloud.RequestOpts{ 41 MoreHeaders: h, 42 OkCodes: []int{200}, 43 }) 44 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 45 return r 46 } 47 48 // NewObjectStorageV1 creates a Swauth-authenticated *gophercloud.ServiceClient 49 // client that can issue ObjectStorage-based API calls. 50 func NewObjectStorageV1(pc *gophercloud.ProviderClient, authOpts AuthOpts) (*gophercloud.ServiceClient, error) { 51 auth, err := Auth(pc, authOpts).Extract() 52 if err != nil { 53 return nil, err 54 } 55 56 swiftClient := &gophercloud.ServiceClient{ 57 ProviderClient: pc, 58 Endpoint: gophercloud.NormalizeURL(auth.StorageURL), 59 } 60 61 swiftClient.TokenID = auth.Token 62 63 return swiftClient, nil 64 }