Skip to content

Commit 64dd41d

Browse files
committedJan 7, 2025·
fix: Add session lookup fallback for soap header auth
In 1918984 / PR #3636 we deferred the http.CookieJar lookup until request time. Fallback to using Path="/" if initial lookup fails. Signed-off-by: Doug MacEachern <dougm@broadcom.com>
1 parent 8f7d233 commit 64dd41d

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed
 

‎vim25/soap/client.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,31 @@ func (c *Client) NewServiceClient(path string, namespace string) *Client {
214214
return c.newServiceClientWithTransport(path, namespace, c.t)
215215
}
216216

217-
// SessionCookie returns a SessionCookie with value of the vmware_soap_session http.Cookie.
218-
func (c *Client) SessionCookie() *HeaderElement {
219-
for _, cookie := range c.Jar.Cookies(c.URL()) {
217+
func sessionCookie(jar http.CookieJar, u *url.URL) *HeaderElement {
218+
for _, cookie := range jar.Cookies(u) {
220219
if cookie.Name == SessionCookieName {
221220
return &HeaderElement{Value: cookie.Value}
222221
}
223222
}
224223
return nil
225224
}
226225

226+
// SessionCookie returns a SessionCookie with value of the vmware_soap_session http.Cookie.
227+
func (c *Client) SessionCookie() *HeaderElement {
228+
u := c.URL()
229+
230+
if cookie := sessionCookie(c.Jar, u); cookie != nil {
231+
return cookie
232+
}
233+
234+
// Default "/sdk" Path would match above,
235+
// but saw a case of Path == "sdk", where above returns nil.
236+
// The jar entry Path is normally "/", so fallback to that.
237+
u.Path = "/"
238+
239+
return sessionCookie(c.Jar, u)
240+
}
241+
227242
func (c *Client) newServiceClientWithTransport(path string, namespace string, t *http.Transport) *Client {
228243
vc := c.URL()
229244
u, err := url.Parse(path)

‎vim25/soap/client_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,29 @@ func TestParseURL(t *testing.T) {
229229
})
230230
}
231231
}
232+
233+
func TestSessionCookie(t *testing.T) {
234+
u := &url.URL{
235+
Scheme: "http",
236+
Host: "localhost:1080",
237+
Path: "sdk", // see comment in Client.SessionCookie
238+
}
239+
240+
c := NewClient(u, true)
241+
242+
cookie := &http.Cookie{
243+
Name: SessionCookieName,
244+
Value: "ANY",
245+
Path: "/",
246+
Domain: "localhost",
247+
HttpOnly: true,
248+
Secure: false,
249+
}
250+
251+
c.Jar.SetCookies(u, []*http.Cookie{cookie})
252+
253+
val := c.SessionCookie()
254+
if val == nil {
255+
t.Fatal("no session cookie")
256+
}
257+
}

0 commit comments

Comments
 (0)
Please sign in to comment.