Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 3 | import "github.com/gophercloud/gophercloud" |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 4 | |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 5 | type PasswordCredentialsV2 struct { |
| 6 | Username string `json:"username" required:"true"` |
| 7 | Password string `json:"password" required:"true"` |
| 8 | } |
| 9 | |
| 10 | type TokenCredentialsV2 struct { |
| 11 | ID string `json:"id,omitempty" required:"true"` |
| 12 | } |
| 13 | |
| 14 | // AuthOptionsV2 wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder |
| 15 | // interface. |
| 16 | type AuthOptionsV2 struct { |
| 17 | PasswordCredentials *PasswordCredentialsV2 `json:"passwordCredentials,omitempty" xor:"TokenCredentials"` |
| 18 | |
| 19 | // The TenantID and TenantName fields are optional for the Identity V2 API. |
| 20 | // Some providers allow you to specify a TenantName instead of the TenantId. |
| 21 | // Some require both. Your provider's authentication policies will determine |
| 22 | // how these fields influence authentication. |
| 23 | TenantID string `json:"tenantId,omitempty"` |
| 24 | TenantName string `json:"tenantName,omitempty"` |
| 25 | |
| 26 | // TokenCredentials allows users to authenticate (possibly as another user) with an |
| 27 | // authentication token ID. |
| 28 | TokenCredentials *TokenCredentialsV2 `json:"token,omitempty" xor:"PasswordCredentials"` |
| 29 | } |
| 30 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 31 | // AuthOptionsBuilder describes any argument that may be passed to the Create call. |
| 32 | type AuthOptionsBuilder interface { |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 33 | // ToTokenCreateMap assembles the Create request body, returning an error if parameters are |
| 34 | // missing or inconsistent. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 35 | ToTokenV2CreateMap() (map[string]interface{}, error) |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 36 | } |
| 37 | |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 38 | // AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder |
| 39 | // interface. |
| 40 | type AuthOptions struct { |
| 41 | gophercloud.AuthOptions |
| 42 | } |
| 43 | |
| 44 | // ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder |
| 45 | // interface in the v2 tokens package |
| 46 | func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) { |
| 47 | v2Opts := AuthOptionsV2{ |
| 48 | TenantID: opts.TenantID, |
| 49 | TenantName: opts.TenantName, |
| 50 | } |
| 51 | |
| 52 | if opts.Password != "" { |
| 53 | v2Opts.PasswordCredentials = &PasswordCredentialsV2{ |
| 54 | Username: opts.Username, |
| 55 | Password: opts.Password, |
| 56 | } |
| 57 | } else { |
| 58 | v2Opts.TokenCredentials = &TokenCredentialsV2{ |
| 59 | ID: opts.TokenID, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | b, err := gophercloud.BuildRequestBody(v2Opts, "auth") |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | return b, nil |
| 68 | } |
| 69 | |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 70 | // Create authenticates to the identity service and attempts to acquire a Token. |
| 71 | // If successful, the CreateResult |
| 72 | // Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(), |
| 73 | // which abstracts all of the gory details about navigating service catalogs and such. |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 74 | func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 75 | b, err := auth.ToTokenV2CreateMap() |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 76 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 77 | r.Err = err |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 78 | return |
Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 79 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 80 | _, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 81 | OkCodes: []int{200, 203}, |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 82 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 83 | return |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 84 | } |
hzlouchao | f6e2926 | 2015-10-27 12:51:08 +0800 | [diff] [blame] | 85 | |
Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 86 | // Get validates and retrieves information for user's token. |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 87 | func Get(client *gophercloud.ServiceClient, token string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 88 | _, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{ |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 89 | OkCodes: []int{200, 203}, |
| 90 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame^] | 91 | return |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 92 | } |