blob: e8589ae0cb6572303ae7844f012bd126cacfcc77 [file] [log] [blame]
Joe Topjian7a0f3e52016-08-22 20:26:34 -06001package swauth
2
3import "github.com/gophercloud/gophercloud"
4
5// AuthOptsBuilder describes struct types that can be accepted by the Auth call.
6// The AuthOpts struct in this package does.
7type AuthOptsBuilder interface {
8 ToAuthOptsMap() (map[string]string, error)
9}
10
11// AuthOpts specifies an authentication request.
12type AuthOpts struct {
13 // User is an Swauth-based username in username:tenant format.
14 User string `h:"X-Auth-User" required:"true"`
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.
20func (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.
25func 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
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 *gophercloud.ServiceClient
55// client that can issue ObjectStorage-based API calls.
56func NewObjectStorageV1(pc *gophercloud.ProviderClient, authOpts AuthOpts) (*gophercloud.ServiceClient, error) {
57 auth, err := Auth(pc, authOpts).Extract()
58 if err != nil {
59 return nil, err
60 }
61
62 swiftClient := &gophercloud.ServiceClient{
63 ProviderClient: pc,
Joe Topjian176266d2016-11-07 14:45:46 -070064 Endpoint: gophercloud.NormalizeURL(auth.StorageURL),
Joe Topjian7a0f3e52016-08-22 20:26:34 -060065 }
66
67 swiftClient.TokenID = auth.Token
68
69 return swiftClient, nil
70}