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