feat: webdav for sharepoint online (#460)
This commit is contained in:
parent
395de069c2
commit
8e059c64b5
@ -3,6 +3,7 @@ package webdav
|
|||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/drivers/base"
|
"github.com/Xhofe/alist/drivers/base"
|
||||||
|
"github.com/Xhofe/alist/drivers/webdav/odrvcookie"
|
||||||
"github.com/Xhofe/alist/model"
|
"github.com/Xhofe/alist/model"
|
||||||
"github.com/Xhofe/alist/utils"
|
"github.com/Xhofe/alist/utils"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -47,7 +48,7 @@ func (driver WebDav) Items() []base.Item {
|
|||||||
Required: true,
|
Required: true,
|
||||||
Default: "other",
|
Default: "other",
|
||||||
Values: "sharepoint,other",
|
Values: "sharepoint,other",
|
||||||
Description: "sharepoint temporarily unavailable",
|
Description: "webdav vendor",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,9 +57,17 @@ func (driver WebDav) Save(account *model.Account, old *model.Account) error {
|
|||||||
if account == nil {
|
if account == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
var err error
|
||||||
|
if isSharePoint(account) {
|
||||||
|
_, err = odrvcookie.GetCookie(account.Username, account.Password, account.SiteUrl)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
account.Status = err.Error()
|
||||||
|
} else {
|
||||||
account.Status = "work"
|
account.Status = "work"
|
||||||
|
}
|
||||||
_ = model.SaveAccount(account)
|
_ = model.SaveAccount(account)
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (driver WebDav) File(path string, account *model.Account) (*model.File, error) {
|
func (driver WebDav) File(path string, account *model.Account) (*model.File, error) {
|
||||||
|
44
drivers/webdav/odrvcookie/cookie.go
Normal file
44
drivers/webdav/odrvcookie/cookie.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package odrvcookie
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/utils/cookie"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SpCookie struct {
|
||||||
|
Cookie string
|
||||||
|
expire time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sp SpCookie) IsExpire() bool {
|
||||||
|
return time.Now().Before(sp.expire)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cookiesMap = struct {
|
||||||
|
sync.Mutex
|
||||||
|
m map[string]*SpCookie
|
||||||
|
}{m: make(map[string]*SpCookie)}
|
||||||
|
|
||||||
|
func GetCookie(username, password, siteUrl string) (string, error) {
|
||||||
|
cookiesMap.Lock()
|
||||||
|
defer cookiesMap.Unlock()
|
||||||
|
spCookie, ok := cookiesMap.m[username]
|
||||||
|
if ok {
|
||||||
|
if !spCookie.IsExpire() {
|
||||||
|
return spCookie.Cookie, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ca := New(username, password, siteUrl)
|
||||||
|
tokenConf, err := ca.Cookies()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
spCookie = &SpCookie{
|
||||||
|
Cookie: cookie.ToString([]*http.Cookie{&tokenConf.RtFa, &tokenConf.FedAuth}),
|
||||||
|
expire: time.Now().Add(time.Hour * 12),
|
||||||
|
}
|
||||||
|
cookiesMap.m[username] = spCookie
|
||||||
|
return spCookie.Cookie, nil
|
||||||
|
}
|
@ -5,7 +5,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -90,25 +89,29 @@ func New(pUser, pPass, pEndpoint string) CookieAuth {
|
|||||||
// Cookies creates a CookieResponse. It fetches the auth token and then
|
// Cookies creates a CookieResponse. It fetches the auth token and then
|
||||||
// retrieves the Cookies
|
// retrieves the Cookies
|
||||||
func (ca *CookieAuth) Cookies() (CookieResponse, error) {
|
func (ca *CookieAuth) Cookies() (CookieResponse, error) {
|
||||||
return ca.getSPCookie(ca.getSPToken())
|
spToken, err := ca.getSPToken()
|
||||||
|
if err != nil {
|
||||||
|
return CookieResponse{}, err
|
||||||
|
}
|
||||||
|
return ca.getSPCookie(spToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error) {
|
func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error) {
|
||||||
spRoot, err := url.Parse(ca.endpoint)
|
spRoot, err := url.Parse(ca.endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return CookieResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := url.Parse("https://" + spRoot.Host + "/_forms/default.aspx?wa=wsignin1.0")
|
u, err := url.Parse("https://" + spRoot.Host + "/_forms/default.aspx?wa=wsignin1.0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return CookieResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// To authenticate with davfs or anything else we need two cookies (rtFa and FedAuth)
|
// To authenticate with davfs or anything else we need two cookies (rtFa and FedAuth)
|
||||||
// In order to get them we use the token we got earlier and a cookieJar
|
// In order to get them we use the token we got earlier and a cookieJar
|
||||||
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return CookieResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
@ -117,7 +120,7 @@ func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error)
|
|||||||
|
|
||||||
// Send the previously aquired Token as a Post parameter
|
// Send the previously aquired Token as a Post parameter
|
||||||
if _, err = client.Post(u.String(), "text/xml", strings.NewReader(conf.Succ.Token)); err != nil {
|
if _, err = client.Post(u.String(), "text/xml", strings.NewReader(conf.Succ.Token)); err != nil {
|
||||||
log.Fatal(err)
|
return CookieResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cookieResponse := CookieResponse{}
|
cookieResponse := CookieResponse{}
|
||||||
@ -134,7 +137,7 @@ func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error)
|
|||||||
return cookieResponse, err
|
return cookieResponse, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ca *CookieAuth) getSPToken() *SuccessResponse {
|
func (ca *CookieAuth) getSPToken() (*SuccessResponse, error) {
|
||||||
reqData := map[string]interface{}{
|
reqData := map[string]interface{}{
|
||||||
"Username": ca.user,
|
"Username": ca.user,
|
||||||
"Password": ca.pass,
|
"Password": ca.pass,
|
||||||
@ -145,20 +148,20 @@ func (ca *CookieAuth) getSPToken() *SuccessResponse {
|
|||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
if err := t.Execute(buf, reqData); err != nil {
|
if err := t.Execute(buf, reqData); err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the first request which gives us an auth token for the sharepoint service
|
// Execute the first request which gives us an auth token for the sharepoint service
|
||||||
// With this token we can authenticate on the login page and save the returned cookies
|
// With this token we can authenticate on the login page and save the returned cookies
|
||||||
req, err := http.NewRequest("POST", "https://login.microsoftonline.com/extSTS.srf", buf)
|
req, err := http.NewRequest("POST", "https://login.microsoftonline.com/extSTS.srf", buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
@ -169,8 +172,8 @@ func (ca *CookieAuth) getSPToken() *SuccessResponse {
|
|||||||
var conf SuccessResponse
|
var conf SuccessResponse
|
||||||
err = xml.Unmarshal(s, &conf)
|
err = xml.Unmarshal(s, &conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &conf
|
return &conf, err
|
||||||
}
|
}
|
||||||
|
7
drivers/webdav/util.go
Normal file
7
drivers/webdav/util.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package webdav
|
||||||
|
|
||||||
|
import "github.com/Xhofe/alist/model"
|
||||||
|
|
||||||
|
func isSharePoint(account *model.Account) bool {
|
||||||
|
return account.InternalType == "sharepoint"
|
||||||
|
}
|
@ -5,7 +5,6 @@ import (
|
|||||||
"github.com/Xhofe/alist/drivers/webdav/odrvcookie"
|
"github.com/Xhofe/alist/drivers/webdav/odrvcookie"
|
||||||
"github.com/Xhofe/alist/model"
|
"github.com/Xhofe/alist/model"
|
||||||
"github.com/Xhofe/alist/utils"
|
"github.com/Xhofe/alist/utils"
|
||||||
"github.com/Xhofe/alist/utils/cookie"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/studio-b12/gowebdav"
|
"github.com/studio-b12/gowebdav"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -14,20 +13,15 @@ import (
|
|||||||
|
|
||||||
func (driver WebDav) NewClient(account *model.Account) *gowebdav.Client {
|
func (driver WebDav) NewClient(account *model.Account) *gowebdav.Client {
|
||||||
c := gowebdav.NewClient(account.SiteUrl, account.Username, account.Password)
|
c := gowebdav.NewClient(account.SiteUrl, account.Username, account.Password)
|
||||||
if account.InternalType == "sharepoint" {
|
if isSharePoint(account) {
|
||||||
|
cookie, err := odrvcookie.GetCookie(account.Username, account.Password, account.SiteUrl)
|
||||||
ca := odrvcookie.New(account.Username, account.Password, account.SiteUrl)
|
log.Debugln(cookie, err)
|
||||||
tokenConf, err := ca.Cookies()
|
if err == nil {
|
||||||
log.Debugln(err, tokenConf)
|
log.Debugln("set interceptor")
|
||||||
if err != nil {
|
|
||||||
c.SetInterceptor(func(method string, rq *http.Request) {
|
c.SetInterceptor(func(method string, rq *http.Request) {
|
||||||
rq.Header.Del("Authorization")
|
rq.Header.Del("Authorization")
|
||||||
//rq.AddCookie(&tokenConf.FedAuth)
|
rq.Header.Set("Cookie", cookie)
|
||||||
//rq.AddCookie(&tokenConf.RtFa)
|
log.Debugf("sp webdav req: %+v", rq)
|
||||||
rq.Header.Set("Cookie", cookie.ToString([]*http.Cookie{&tokenConf.RtFa, &tokenConf.FedAuth}))
|
|
||||||
if method == "PROPFIND" {
|
|
||||||
rq.Header.Set("Depth", "0")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user