Skip to content

Commit 30ebb0f

Browse files
committed
feat: support other region sharepoint with webdav
1 parent 8e059c6 commit 30ebb0f

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

drivers/webdav/odrvcookie/fetch.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package odrvcookie
44
import (
55
"bytes"
66
"encoding/xml"
7+
"fmt"
78
"html/template"
89
"net/http"
910
"net/http/cookiejar"
@@ -52,7 +53,7 @@ xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-util
5253
<a:ReplyTo>
5354
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
5455
</a:ReplyTo>
55-
<a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To>
56+
<a:To s:mustUnderstand="1">{{ .LoginUrl }}</a:To>
5657
<o:Security s:mustUnderstand="1"
5758
xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
5859
<o:UsernameToken>
@@ -137,11 +138,37 @@ func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error)
137138
return cookieResponse, err
138139
}
139140

141+
var loginUrlsMap = map[string]string{
142+
"com": "https://login.microsoftonline.com",
143+
"cn": "https://login.chinacloudapi.cn",
144+
"us": "https://login.microsoftonline.us",
145+
"de": "https://login.microsoftonline.de",
146+
}
147+
148+
func getLoginUrl(endpoint string) (string, error) {
149+
spRoot, err := url.Parse(endpoint)
150+
if err != nil {
151+
return "", err
152+
}
153+
domains := strings.Split(spRoot.Host, ".")
154+
tld := domains[len(domains)-1]
155+
loginUrl, ok := loginUrlsMap[tld]
156+
if !ok {
157+
return "", fmt.Errorf("tld %s is not supported", tld)
158+
}
159+
return loginUrl + "/extSTS.srf", nil
160+
}
161+
140162
func (ca *CookieAuth) getSPToken() (*SuccessResponse, error) {
141-
reqData := map[string]interface{}{
163+
loginUrl, err := getLoginUrl(ca.endpoint)
164+
if err != nil {
165+
return nil, err
166+
}
167+
reqData := map[string]string{
142168
"Username": ca.user,
143169
"Password": ca.pass,
144170
"Address": ca.endpoint,
171+
"LoginUrl": loginUrl,
145172
}
146173

147174
t := template.Must(template.New("authXML").Parse(reqString))
@@ -153,7 +180,7 @@ func (ca *CookieAuth) getSPToken() (*SuccessResponse, error) {
153180

154181
// Execute the first request which gives us an auth token for the sharepoint service
155182
// With this token we can authenticate on the login page and save the returned cookies
156-
req, err := http.NewRequest("POST", "https://login.microsoftonline.com/extSTS.srf", buf)
183+
req, err := http.NewRequest("POST", loginUrl, buf)
157184
if err != nil {
158185
return nil, err
159186
}

0 commit comments

Comments
 (0)