| 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 | 64d0ef0 | 2016-04-13 13:10:04 -0500 | [diff] [blame] | 38 | // AuthOptions are the valid options for Openstack Identity v2 authentication. | 
 | 39 | // For field descriptions, see gophercloud.AuthOptions. | 
| jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 40 | type AuthOptions struct { | 
| jrperritt | 64d0ef0 | 2016-04-13 13:10:04 -0500 | [diff] [blame] | 41 | 	IdentityEndpoint string `json:"-"` | 
 | 42 | 	Username         string `json:"username,omitempty"` | 
 | 43 | 	Password         string `json:"password,omitempty"` | 
 | 44 | 	TenantID         string `json:"tenantId,omitempty"` | 
 | 45 | 	TenantName       string `json:"tenantName,omitempty"` | 
 | 46 | 	AllowReauth      bool   `json:"-"` | 
 | 47 | 	TokenID          string | 
| jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 48 | } | 
 | 49 |  | 
 | 50 | // ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder | 
 | 51 | // interface in the v2 tokens package | 
 | 52 | func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) { | 
 | 53 | 	v2Opts := AuthOptionsV2{ | 
 | 54 | 		TenantID:   opts.TenantID, | 
 | 55 | 		TenantName: opts.TenantName, | 
 | 56 | 	} | 
 | 57 |  | 
 | 58 | 	if opts.Password != "" { | 
 | 59 | 		v2Opts.PasswordCredentials = &PasswordCredentialsV2{ | 
 | 60 | 			Username: opts.Username, | 
 | 61 | 			Password: opts.Password, | 
 | 62 | 		} | 
 | 63 | 	} else { | 
 | 64 | 		v2Opts.TokenCredentials = &TokenCredentialsV2{ | 
 | 65 | 			ID: opts.TokenID, | 
 | 66 | 		} | 
 | 67 | 	} | 
 | 68 |  | 
 | 69 | 	b, err := gophercloud.BuildRequestBody(v2Opts, "auth") | 
 | 70 | 	if err != nil { | 
 | 71 | 		return nil, err | 
 | 72 | 	} | 
 | 73 | 	return b, nil | 
 | 74 | } | 
 | 75 |  | 
| Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 76 | // Create authenticates to the identity service and attempts to acquire a Token. | 
 | 77 | // If successful, the CreateResult | 
 | 78 | // Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(), | 
 | 79 | // 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] | 80 | func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) { | 
| Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 81 | 	b, err := auth.ToTokenV2CreateMap() | 
| Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 82 | 	if err != nil { | 
| Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 83 | 		r.Err = err | 
| Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 84 | 		return | 
| Ash Wilson | 40095f0 | 2014-10-07 15:46:40 -0400 | [diff] [blame] | 85 | 	} | 
| Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 86 | 	_, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{ | 
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 87 | 		OkCodes:     []int{200, 203}, | 
 | 88 | 		MoreHeaders: map[string]string{"X-Auth-Token": ""}, | 
| Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 89 | 	}) | 
| jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 90 | 	return | 
| Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 91 | } | 
| hzlouchao | f6e2926 | 2015-10-27 12:51:08 +0800 | [diff] [blame] | 92 |  | 
| Jon Perritt | a3302e1 | 2016-03-07 03:48:59 -0600 | [diff] [blame] | 93 | // Get validates and retrieves information for user's token. | 
| Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 94 | func Get(client *gophercloud.ServiceClient, token string) (r GetResult) { | 
| Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 95 | 	_, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{ | 
| hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 96 | 		OkCodes: []int{200, 203}, | 
 | 97 | 	}) | 
| jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 98 | 	return | 
| hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 99 | } |