github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v2/tokens/requests.go (about)

     1  package tokens
     2  
     3  import "github.com/gophercloud/gophercloud"
     4  
     5  // PasswordCredentialsV2 represents the required options to authenticate
     6  // with a username and password.
     7  type PasswordCredentialsV2 struct {
     8  	Username string `json:"username" required:"true"`
     9  	Password string `json:"password" required:"true"`
    10  }
    11  
    12  // TokenCredentialsV2 represents the required options to authenticate
    13  // with a token.
    14  type TokenCredentialsV2 struct {
    15  	ID string `json:"id,omitempty" required:"true"`
    16  }
    17  
    18  // AuthOptionsV2 wraps a gophercloud AuthOptions in order to adhere to the
    19  // AuthOptionsBuilder interface.
    20  type AuthOptionsV2 struct {
    21  	PasswordCredentials *PasswordCredentialsV2 `json:"passwordCredentials,omitempty" xor:"TokenCredentials"`
    22  
    23  	// The TenantID and TenantName fields are optional for the Identity V2 API.
    24  	// Some providers allow you to specify a TenantName instead of the TenantId.
    25  	// Some require both. Your provider's authentication policies will determine
    26  	// how these fields influence authentication.
    27  	TenantID   string `json:"tenantId,omitempty"`
    28  	TenantName string `json:"tenantName,omitempty"`
    29  
    30  	// TokenCredentials allows users to authenticate (possibly as another user)
    31  	// with an authentication token ID.
    32  	TokenCredentials *TokenCredentialsV2 `json:"token,omitempty" xor:"PasswordCredentials"`
    33  }
    34  
    35  // AuthOptionsBuilder allows extensions to add additional parameters to the
    36  // token create request.
    37  type AuthOptionsBuilder interface {
    38  	// ToTokenCreateMap assembles the Create request body, returning an error
    39  	// if parameters are missing or inconsistent.
    40  	ToTokenV2CreateMap() (map[string]interface{}, error)
    41  }
    42  
    43  // AuthOptions are the valid options for Openstack Identity v2 authentication.
    44  // For field descriptions, see gophercloud.AuthOptions.
    45  type AuthOptions struct {
    46  	IdentityEndpoint string `json:"-"`
    47  	Username         string `json:"username,omitempty"`
    48  	Password         string `json:"password,omitempty"`
    49  	TenantID         string `json:"tenantId,omitempty"`
    50  	TenantName       string `json:"tenantName,omitempty"`
    51  	AllowReauth      bool   `json:"-"`
    52  	TokenID          string
    53  }
    54  
    55  // ToTokenV2CreateMap builds a token request body from the given AuthOptions.
    56  func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
    57  	v2Opts := AuthOptionsV2{
    58  		TenantID:   opts.TenantID,
    59  		TenantName: opts.TenantName,
    60  	}
    61  
    62  	if opts.Password != "" {
    63  		v2Opts.PasswordCredentials = &PasswordCredentialsV2{
    64  			Username: opts.Username,
    65  			Password: opts.Password,
    66  		}
    67  	} else {
    68  		v2Opts.TokenCredentials = &TokenCredentialsV2{
    69  			ID: opts.TokenID,
    70  		}
    71  	}
    72  
    73  	b, err := gophercloud.BuildRequestBody(v2Opts, "auth")
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	return b, nil
    78  }
    79  
    80  // Create authenticates to the identity service and attempts to acquire a Token.
    81  // Generally, rather than interact with this call directly, end users should
    82  // call openstack.AuthenticatedClient(), which abstracts all of the gory details
    83  // about navigating service catalogs and such.
    84  func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) {
    85  	b, err := auth.ToTokenV2CreateMap()
    86  	if err != nil {
    87  		r.Err = err
    88  		return
    89  	}
    90  	resp, err := client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
    91  		OkCodes:     []int{200, 203},
    92  		OmitHeaders: []string{"X-Auth-Token"},
    93  	})
    94  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    95  	return
    96  }
    97  
    98  // Get validates and retrieves information for user's token.
    99  func Get(client *gophercloud.ServiceClient, token string) (r GetResult) {
   100  	resp, err := client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
   101  		OkCodes: []int{200, 203},
   102  	})
   103  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   104  	return
   105  }