github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v2/tokens/requests.go (about) 1 package tokens 2 3 import "github.com/huaweicloud/golangsdk" 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 golangsdk 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 golangsdk.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 := golangsdk.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 *golangsdk.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) { 85 b, err := auth.ToTokenV2CreateMap() 86 if err != nil { 87 r.Err = err 88 return 89 } 90 _, r.Err = client.Post(CreateURL(client), b, &r.Body, &golangsdk.RequestOpts{ 91 OkCodes: []int{200, 203}, 92 MoreHeaders: map[string]string{"X-Auth-Token": ""}, 93 }) 94 return 95 } 96 97 // Get validates and retrieves information for user's token. 98 func Get(client *golangsdk.ServiceClient, token string) (r GetResult) { 99 _, r.Err = client.Get(GetURL(client, token), &r.Body, &golangsdk.RequestOpts{ 100 OkCodes: []int{200, 203}, 101 }) 102 return 103 }