|
Packit |
63bb0d |
package tokens
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
import "github.com/gophercloud/gophercloud"
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// PasswordCredentialsV2 represents the required options to authenticate
|
|
Packit |
63bb0d |
// with a username and password.
|
|
Packit |
63bb0d |
type PasswordCredentialsV2 struct {
|
|
Packit |
63bb0d |
Username string `json:"username" required:"true"`
|
|
Packit |
63bb0d |
Password string `json:"password" required:"true"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// TokenCredentialsV2 represents the required options to authenticate
|
|
Packit |
63bb0d |
// with a token.
|
|
Packit |
63bb0d |
type TokenCredentialsV2 struct {
|
|
Packit |
63bb0d |
ID string `json:"id,omitempty" required:"true"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// AuthOptionsV2 wraps a gophercloud AuthOptions in order to adhere to the
|
|
Packit |
63bb0d |
// AuthOptionsBuilder interface.
|
|
Packit |
63bb0d |
type AuthOptionsV2 struct {
|
|
Packit |
63bb0d |
PasswordCredentials *PasswordCredentialsV2 `json:"passwordCredentials,omitempty" xor:"TokenCredentials"`
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// The TenantID and TenantName fields are optional for the Identity V2 API.
|
|
Packit |
63bb0d |
// Some providers allow you to specify a TenantName instead of the TenantId.
|
|
Packit |
63bb0d |
// Some require both. Your provider's authentication policies will determine
|
|
Packit |
63bb0d |
// how these fields influence authentication.
|
|
Packit |
63bb0d |
TenantID string `json:"tenantId,omitempty"`
|
|
Packit |
63bb0d |
TenantName string `json:"tenantName,omitempty"`
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// TokenCredentials allows users to authenticate (possibly as another user)
|
|
Packit |
63bb0d |
// with an authentication token ID.
|
|
Packit |
63bb0d |
TokenCredentials *TokenCredentialsV2 `json:"token,omitempty" xor:"PasswordCredentials"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// AuthOptionsBuilder allows extensions to add additional parameters to the
|
|
Packit |
63bb0d |
// token create request.
|
|
Packit |
63bb0d |
type AuthOptionsBuilder interface {
|
|
Packit |
63bb0d |
// ToTokenCreateMap assembles the Create request body, returning an error
|
|
Packit |
63bb0d |
// if parameters are missing or inconsistent.
|
|
Packit |
63bb0d |
ToTokenV2CreateMap() (map[string]interface{}, error)
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// AuthOptions are the valid options for Openstack Identity v2 authentication.
|
|
Packit |
63bb0d |
// For field descriptions, see gophercloud.AuthOptions.
|
|
Packit |
63bb0d |
type AuthOptions struct {
|
|
Packit |
63bb0d |
IdentityEndpoint string `json:"-"`
|
|
Packit |
63bb0d |
Username string `json:"username,omitempty"`
|
|
Packit |
63bb0d |
Password string `json:"password,omitempty"`
|
|
Packit |
63bb0d |
TenantID string `json:"tenantId,omitempty"`
|
|
Packit |
63bb0d |
TenantName string `json:"tenantName,omitempty"`
|
|
Packit |
63bb0d |
AllowReauth bool `json:"-"`
|
|
Packit |
63bb0d |
TokenID string
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// ToTokenV2CreateMap builds a token request body from the given AuthOptions.
|
|
Packit |
63bb0d |
func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
|
|
Packit |
63bb0d |
v2Opts := AuthOptionsV2{
|
|
Packit |
63bb0d |
TenantID: opts.TenantID,
|
|
Packit |
63bb0d |
TenantName: opts.TenantName,
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
if opts.Password != "" {
|
|
Packit |
63bb0d |
v2Opts.PasswordCredentials = &PasswordCredentialsV2{
|
|
Packit |
63bb0d |
Username: opts.Username,
|
|
Packit |
63bb0d |
Password: opts.Password,
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
} else {
|
|
Packit |
63bb0d |
v2Opts.TokenCredentials = &TokenCredentialsV2{
|
|
Packit |
63bb0d |
ID: opts.TokenID,
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
b, err := gophercloud.BuildRequestBody(v2Opts, "auth")
|
|
Packit |
63bb0d |
if err != nil {
|
|
Packit |
63bb0d |
return nil, err
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
return b, nil
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Create authenticates to the identity service and attempts to acquire a Token.
|
|
Packit |
63bb0d |
// Generally, rather than interact with this call directly, end users should
|
|
Packit |
63bb0d |
// call openstack.AuthenticatedClient(), which abstracts all of the gory details
|
|
Packit |
63bb0d |
// about navigating service catalogs and such.
|
|
Packit |
63bb0d |
func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) {
|
|
Packit |
63bb0d |
b, err := auth.ToTokenV2CreateMap()
|
|
Packit |
63bb0d |
if err != nil {
|
|
Packit |
63bb0d |
r.Err = err
|
|
Packit |
63bb0d |
return
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
resp, err := client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
|
|
Packit |
63bb0d |
OkCodes: []int{200, 203},
|
|
Packit |
63bb0d |
MoreHeaders: map[string]string{"X-Auth-Token": ""},
|
|
Packit |
63bb0d |
})
|
|
Packit |
63bb0d |
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
|
|
Packit |
63bb0d |
return
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Get validates and retrieves information for user's token.
|
|
Packit |
63bb0d |
func Get(client *gophercloud.ServiceClient, token string) (r GetResult) {
|
|
Packit |
63bb0d |
resp, err := client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
|
|
Packit |
63bb0d |
OkCodes: []int{200, 203},
|
|
Packit |
63bb0d |
})
|
|
Packit |
63bb0d |
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
|
|
Packit |
63bb0d |
return
|
|
Packit |
63bb0d |
}
|