blob: 73e0b911bfa7f251455c8ada339c14b1c3425ff0 [file] [log] [blame]
Ash Wilson1f110512014-10-02 15:43:47 -04001package tokens
2
Jon Perritta3302e12016-03-07 03:48:59 -06003import "github.com/gophercloud/gophercloud"
Ash Wilson1f110512014-10-02 15:43:47 -04004
jrperritt29ae6b32016-04-13 12:59:37 -05005type PasswordCredentialsV2 struct {
6 Username string `json:"username" required:"true"`
7 Password string `json:"password" required:"true"`
8}
9
10type 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.
16type 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 Wilson40095f02014-10-07 15:46:40 -040031// AuthOptionsBuilder describes any argument that may be passed to the Create call.
32type AuthOptionsBuilder interface {
Ash Wilson40095f02014-10-07 15:46:40 -040033 // ToTokenCreateMap assembles the Create request body, returning an error if parameters are
34 // missing or inconsistent.
Jon Perrittdb0ae142016-03-13 00:33:41 -060035 ToTokenV2CreateMap() (map[string]interface{}, error)
Ash Wilson40095f02014-10-07 15:46:40 -040036}
37
jrperritt29ae6b32016-04-13 12:59:37 -050038// AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder
39// interface.
40type AuthOptions struct {
41 gophercloud.AuthOptions
42}
43
44// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
45// interface in the v2 tokens package
46func (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 Wilson40095f02014-10-07 15:46:40 -040070// 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 Perritt2be387a2016-03-31 09:31:58 -050074func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060075 b, err := auth.ToTokenV2CreateMap()
Ash Wilson40095f02014-10-07 15:46:40 -040076 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060077 r.Err = err
Jon Perritt2be387a2016-03-31 09:31:58 -050078 return
Ash Wilson40095f02014-10-07 15:46:40 -040079 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060080 _, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010081 OkCodes: []int{200, 203},
Ash Wilson1f110512014-10-02 15:43:47 -040082 })
jrperritt29ae6b32016-04-13 12:59:37 -050083 return
Ash Wilson1f110512014-10-02 15:43:47 -040084}
hzlouchaof6e29262015-10-27 12:51:08 +080085
Jon Perritta3302e12016-03-07 03:48:59 -060086// Get validates and retrieves information for user's token.
Jon Perritt2be387a2016-03-31 09:31:58 -050087func Get(client *gophercloud.ServiceClient, token string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060088 _, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
hzlouchaob7640892015-11-04 21:37:20 +080089 OkCodes: []int{200, 203},
90 })
jrperritt29ae6b32016-04-13 12:59:37 -050091 return
hzlouchaob7640892015-11-04 21:37:20 +080092}