From ef089dd867e15e257efa7d635421c615a4dd15ea Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 9 Jun 2022 00:24:35 +0800 Subject: [PATCH] webdav: add SharePoint in other specific regions support --- backend/webdav/odrvcookie/fetch.go | 36 +++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/backend/webdav/odrvcookie/fetch.go b/backend/webdav/odrvcookie/fetch.go index f6b25d29e..c3ade04e5 100644 --- a/backend/webdav/odrvcookie/fetch.go +++ b/backend/webdav/odrvcookie/fetch.go @@ -74,7 +74,7 @@ xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-util http://www.w3.org/2005/08/addressing/anonymous -https://login.microsoftonline.com/extSTS.srf +{{ .SPTokenURL }} @@ -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 }