github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v2/tokens/requests.go (about)

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