github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/objectstorage/v1/swauth/requests.go (about) 1 package swauth 2 3 import "github.com/huaweicloud/golangsdk" 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 golangsdk.BuildHeaders(opts) 22 } 23 24 // Auth performs an authentication request for a Swauth-based user. 25 func Auth(c *golangsdk.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), &golangsdk.RequestOpts{ 41 MoreHeaders: h, 42 OkCodes: []int{200}, 43 }) 44 45 if resp != nil { 46 r.Header = resp.Header 47 } 48 49 r.Err = err 50 51 return r 52 } 53 54 // NewObjectStorageV1 creates a Swauth-authenticated *golangsdk.ServiceClient 55 // client that can issue ObjectStorage-based API calls. 56 func NewObjectStorageV1(pc *golangsdk.ProviderClient, authOpts AuthOpts) (*golangsdk.ServiceClient, error) { 57 auth, err := Auth(pc, authOpts).Extract() 58 if err != nil { 59 return nil, err 60 } 61 62 swiftClient := &golangsdk.ServiceClient{ 63 ProviderClient: pc, 64 Endpoint: golangsdk.NormalizeURL(auth.StorageURL), 65 } 66 67 swiftClient.TokenID = auth.Token 68 69 return swiftClient, nil 70 }