github.com/vmware/govmomi@v0.43.0/session/example_test.go (about) 1 /* 2 Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package session_test 18 19 import ( 20 "context" 21 "fmt" 22 "net/url" 23 24 _ "github.com/vmware/govmomi/lookup/simulator" 25 "github.com/vmware/govmomi/session" 26 "github.com/vmware/govmomi/simulator" 27 "github.com/vmware/govmomi/sts" 28 _ "github.com/vmware/govmomi/sts/simulator" 29 "github.com/vmware/govmomi/vim25" 30 "github.com/vmware/govmomi/vim25/soap" 31 ) 32 33 func ExampleManager_LoginByToken() { 34 simulator.Run(func(ctx context.Context, vc *vim25.Client) error { 35 c, err := sts.NewClient(ctx, vc) 36 if err != nil { 37 return err 38 } 39 40 // Issue a bearer token 41 req := sts.TokenRequest{ 42 Userinfo: url.UserPassword("Administrator@VSPHERE.LOCAL", "password"), 43 } 44 45 signer, err := c.Issue(ctx, req) 46 if err != nil { 47 return err 48 } 49 50 // Create a new un-authenticated client and LoginByToken 51 vc2, err := vim25.NewClient(ctx, soap.NewClient(vc.URL(), true)) 52 if err != nil { 53 return err 54 } 55 56 m := session.NewManager(vc2) 57 header := soap.Header{Security: signer} 58 59 err = m.LoginByToken(vc2.WithHeader(ctx, header)) 60 if err != nil { 61 return err 62 } 63 64 session, err := m.UserSession(ctx) 65 if err != nil { 66 return err 67 } 68 69 fmt.Println(session.UserName) 70 71 return nil 72 }) 73 // Output: Administrator@VSPHERE.LOCAL 74 }