github.com/vmware/govmomi@v0.51.0/vapi/authentication/authentication.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package authentication 6 7 import ( 8 "context" 9 "net/http" 10 11 "github.com/vmware/govmomi/vapi/rest" 12 ) 13 14 // Manager extends rest.Client, adding authentication related methods. 15 type Manager struct { 16 *rest.Client 17 } 18 19 // NewManager creates a new Manager instance with the given client. 20 func NewManager(client *rest.Client) *Manager { 21 return &Manager{ 22 Client: client, 23 } 24 } 25 26 type TokenIssueSpec struct { 27 SubjectToken string `json:"subject_token"` 28 SubjectTokenType string `json:"subject_token_type"` 29 GrantType string `json:"grant_type"` 30 ActorToken string `json:"actor_token,omitempty"` 31 ActorTokenType string `json:"actor_token_type,omitempty"` 32 RequestedTokenType string `json:"requested_token_type,omitempty"` 33 Resource string `json:"resource,omitempty"` 34 Scope string `json:"scope,omitempty"` 35 Audience string `json:"audience,omitempty"` 36 } 37 38 type TokenInfo struct { 39 AccessToken string `json:"access_token"` 40 TokenType string `json:"token_type"` 41 ExpiresIn int `json:"expires_in,omitempty"` 42 IssuedTokenType string `json:"issued_token_type,omitempty"` 43 RefreshToken string `json:"refresh_token,omitempty"` 44 Scope string `json:"scope,omitempty"` 45 } 46 47 func (c *Manager) Issue(ctx context.Context, token TokenIssueSpec) (*TokenInfo, error) { 48 url := c.Resource("/vcenter/tokenservice/token-exchange") 49 50 var res TokenInfo 51 52 spec := struct { 53 Spec TokenIssueSpec `json:"spec"` 54 }{Spec: token} 55 56 err := c.Do(ctx, url.Request(http.MethodPost, spec), &res) 57 if err != nil { 58 return nil, err 59 } 60 61 return &res, nil 62 }