blob: 1c4ba7cb96201bcf60887962cf5cc0fdc7292d37 [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
jrperritt64d0ef02016-04-13 13:10:04 -050038// AuthOptions are the valid options for Openstack Identity v2 authentication.
39// For field descriptions, see gophercloud.AuthOptions.
jrperritt29ae6b32016-04-13 12:59:37 -050040type AuthOptions struct {
jrperritt64d0ef02016-04-13 13:10:04 -050041 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
jrperritt29ae6b32016-04-13 12:59:37 -050048}
49
50// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
51// interface in the v2 tokens package
52func (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 Wilson40095f02014-10-07 15:46:40 -040076// 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 Perritt2be387a2016-03-31 09:31:58 -050080func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060081 b, err := auth.ToTokenV2CreateMap()
Ash Wilson40095f02014-10-07 15:46:40 -040082 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060083 r.Err = err
Jon Perritt2be387a2016-03-31 09:31:58 -050084 return
Ash Wilson40095f02014-10-07 15:46:40 -040085 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060086 _, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010087 OkCodes: []int{200, 203},
Ash Wilson1f110512014-10-02 15:43:47 -040088 })
jrperritt29ae6b32016-04-13 12:59:37 -050089 return
Ash Wilson1f110512014-10-02 15:43:47 -040090}
hzlouchaof6e29262015-10-27 12:51:08 +080091
Jon Perritta3302e12016-03-07 03:48:59 -060092// Get validates and retrieves information for user's token.
Jon Perritt2be387a2016-03-31 09:31:58 -050093func Get(client *gophercloud.ServiceClient, token string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060094 _, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
hzlouchaob7640892015-11-04 21:37:20 +080095 OkCodes: []int{200, 203},
96 })
jrperritt29ae6b32016-04-13 12:59:37 -050097 return
hzlouchaob7640892015-11-04 21:37:20 +080098}