webdav: add SharePoint in other specific regions support

This commit is contained in:
Noah Hsu 2022-06-09 00:24:35 +08:00 committed by GitHub
parent e3d44612c1
commit ef089dd867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,7 +74,7 @@ xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-util
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To>
<a:To s:mustUnderstand="1">{{ .SPTokenURL }}</a:To>
<o:Security s:mustUnderstand="1"
xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<o:UsernameToken>
@ -159,11 +159,37 @@ func (ca *CookieAuth) getSPCookie(conf *SharepointSuccessResponse) (*CookieRespo
return &cookieResponse, nil
}
var spTokenURLMap = map[string]string{
"com": "https://login.microsoftonline.com",
"cn": "https://login.chinacloudapi.cn",
"us": "https://login.microsoftonline.us",
"de": "https://login.microsoftonline.de",
}
func getSPTokenURL(endpoint string) (string, error) {
spRoot, err := url.Parse(endpoint)
if err != nil {
return "", fmt.Errorf("error while parse endpoint: %w", err)
}
domains := strings.Split(spRoot.Host, ".")
tld := domains[len(domains)-1]
spTokenURL, ok := spTokenURLMap[tld]
if !ok {
return "", fmt.Errorf("error while get SPToken url, unsupported tld: %s", tld)
}
return spTokenURL + "/extSTS.srf", nil
}
func (ca *CookieAuth) getSPToken(ctx context.Context) (conf *SharepointSuccessResponse, err error) {
spTokenURL, err := getSPTokenURL(ca.endpoint)
if err != nil {
return nil, err
}
reqData := map[string]interface{}{
"Username": ca.user,
"Password": ca.pass,
"Address": ca.endpoint,
"Username": ca.user,
"Password": ca.pass,
"Address": ca.endpoint,
"SPTokenURL": spTokenURL,
}
t := template.Must(template.New("authXML").Parse(reqString))
@ -175,7 +201,7 @@ func (ca *CookieAuth) getSPToken(ctx context.Context) (conf *SharepointSuccessRe
// Create and execute the first request which returns an auth token for the sharepoint service
// With this token we can authenticate on the login page and save the returned cookies
req, err := http.NewRequestWithContext(ctx, "POST", "https://login.microsoftonline.com/extSTS.srf", buf)
req, err := http.NewRequestWithContext(ctx, "POST", spTokenURL, buf)
if err != nil {
return nil, err
}