2018-04-09 10:05:43 +02:00
|
|
|
// Package odrvcookie can fetch authentication cookies for a sharepoint webdav endpoint
|
|
|
|
package odrvcookie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-09-04 21:21:10 +02:00
|
|
|
"context"
|
2018-04-09 10:05:43 +02:00
|
|
|
"encoding/xml"
|
2019-10-02 15:05:40 +02:00
|
|
|
"fmt"
|
2018-04-09 10:05:43 +02:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/fshttp"
|
2018-04-09 10:05:43 +02:00
|
|
|
"golang.org/x/net/publicsuffix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CookieAuth hold the authentication information
|
|
|
|
// These are username and password as well as the authentication endpoint
|
|
|
|
type CookieAuth struct {
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// CookieResponse contains the requested cookies
|
|
|
|
type CookieResponse struct {
|
|
|
|
RtFa http.Cookie
|
|
|
|
FedAuth http.Cookie
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:05:40 +02:00
|
|
|
// SharepointSuccessResponse holds a response from a successful microsoft login
|
|
|
|
type SharepointSuccessResponse struct {
|
2018-04-09 10:05:43 +02:00
|
|
|
XMLName xml.Name `xml:"Envelope"`
|
2019-10-02 15:05:40 +02:00
|
|
|
Body SuccessResponseBody `xml:"Body"`
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 15:05:40 +02:00
|
|
|
// SuccessResponseBody is the body of a successful response, it holds the token
|
2018-04-09 10:05:43 +02:00
|
|
|
type SuccessResponseBody struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Type string `xml:"RequestSecurityTokenResponse>TokenType"`
|
|
|
|
Created time.Time `xml:"RequestSecurityTokenResponse>Lifetime>Created"`
|
|
|
|
Expires time.Time `xml:"RequestSecurityTokenResponse>Lifetime>Expires"`
|
|
|
|
Token string `xml:"RequestSecurityTokenResponse>RequestedSecurityToken>BinarySecurityToken"`
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// SharepointError holds an error response microsoft login
|
2019-10-02 15:05:40 +02:00
|
|
|
type SharepointError struct {
|
|
|
|
XMLName xml.Name `xml:"Envelope"`
|
|
|
|
Body ErrorResponseBody `xml:"Body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SharepointError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s (%s)", e.Body.FaultCode, e.Body.Reason, e.Body.Detail)
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// ErrorResponseBody contains the body of an erroneous response
|
2019-10-02 15:05:40 +02:00
|
|
|
type ErrorResponseBody struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
FaultCode string `xml:"Fault>Code>Subcode>Value"`
|
|
|
|
Reason string `xml:"Fault>Reason>Text"`
|
|
|
|
Detail string `xml:"Fault>Detail>error>internalerror>text"`
|
|
|
|
}
|
|
|
|
|
2018-04-09 10:05:43 +02:00
|
|
|
// reqString is a template that gets populated with the user data in order to retrieve a "BinarySecurityToken"
|
|
|
|
const reqString = `<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
|
|
|
|
xmlns:a="http://www.w3.org/2005/08/addressing"
|
|
|
|
xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
|
|
|
|
<s:Header>
|
|
|
|
<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>
|
|
|
|
<a:ReplyTo>
|
|
|
|
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
|
|
|
|
</a:ReplyTo>
|
2022-06-08 18:24:35 +02:00
|
|
|
<a:To s:mustUnderstand="1">{{ .SPTokenURL }}</a:To>
|
2018-04-09 10:05:43 +02:00
|
|
|
<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>
|
|
|
|
<o:Username>{{ .Username }}</o:Username>
|
|
|
|
<o:Password>{{ .Password }}</o:Password>
|
|
|
|
</o:UsernameToken>
|
|
|
|
</o:Security>
|
|
|
|
</s:Header>
|
|
|
|
<s:Body>
|
|
|
|
<t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
|
|
|
|
<wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
|
|
|
|
<a:EndpointReference>
|
|
|
|
<a:Address>{{ .Address }}</a:Address>
|
|
|
|
</a:EndpointReference>
|
|
|
|
</wsp:AppliesTo>
|
|
|
|
<t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>
|
|
|
|
<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
|
|
|
|
<t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>
|
|
|
|
</t:RequestSecurityToken>
|
|
|
|
</s:Body>
|
|
|
|
</s:Envelope>`
|
|
|
|
|
|
|
|
// New creates a new CookieAuth struct
|
|
|
|
func New(pUser, pPass, pEndpoint string) CookieAuth {
|
|
|
|
retStruct := CookieAuth{
|
|
|
|
user: pUser,
|
|
|
|
pass: pPass,
|
|
|
|
endpoint: pEndpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
return retStruct
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cookies creates a CookieResponse. It fetches the auth token and then
|
|
|
|
// retrieves the Cookies
|
2019-09-04 21:21:10 +02:00
|
|
|
func (ca *CookieAuth) Cookies(ctx context.Context) (*CookieResponse, error) {
|
|
|
|
tokenResp, err := ca.getSPToken(ctx)
|
2018-04-09 10:05:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ca.getSPCookie(tokenResp)
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:05:40 +02:00
|
|
|
func (ca *CookieAuth) getSPCookie(conf *SharepointSuccessResponse) (*CookieResponse, error) {
|
2018-04-09 10:05:43 +02:00
|
|
|
spRoot, err := url.Parse(ca.endpoint)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("error while constructing endpoint URL: %w", err)
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:57:34 +01:00
|
|
|
u, err := url.Parse(spRoot.Scheme + "://" + spRoot.Host + "/_forms/default.aspx?wa=wsignin1.0")
|
2018-04-09 10:05:43 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("error while constructing login URL: %w", err)
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Jar: jar,
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:41:17 +01:00
|
|
|
// Send the previously acquired Token as a Post parameter
|
2019-10-02 15:05:40 +02:00
|
|
|
if _, err = client.Post(u.String(), "text/xml", strings.NewReader(conf.Body.Token)); err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("error while grabbing cookies from endpoint: %w", err)
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cookieResponse := CookieResponse{}
|
|
|
|
for _, cookie := range jar.Cookies(u) {
|
|
|
|
if (cookie.Name == "rtFa") || (cookie.Name == "FedAuth") {
|
|
|
|
switch cookie.Name {
|
|
|
|
case "rtFa":
|
|
|
|
cookieResponse.RtFa = *cookie
|
|
|
|
case "FedAuth":
|
|
|
|
cookieResponse.FedAuth = *cookie
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &cookieResponse, nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 18:24:35 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:05:40 +02:00
|
|
|
func (ca *CookieAuth) getSPToken(ctx context.Context) (conf *SharepointSuccessResponse, err error) {
|
2022-06-08 18:24:35 +02:00
|
|
|
spTokenURL, err := getSPTokenURL(ca.endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-09 10:05:43 +02:00
|
|
|
reqData := map[string]interface{}{
|
2022-06-08 18:24:35 +02:00
|
|
|
"Username": ca.user,
|
|
|
|
"Password": ca.pass,
|
|
|
|
"Address": ca.endpoint,
|
|
|
|
"SPTokenURL": spTokenURL,
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t := template.Must(template.New("authXML").Parse(reqString))
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := t.Execute(buf, reqData); err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("error while filling auth token template: %w", err)
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-06-08 18:24:35 +02:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", spTokenURL, buf)
|
2018-04-09 10:05:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:24:43 +01:00
|
|
|
client := fshttp.NewClient(ctx)
|
2018-04-09 10:05:43 +02:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("error while logging in to endpoint: %w", err)
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
|
|
|
|
|
|
|
respBuf := bytes.Buffer{}
|
|
|
|
_, err = respBuf.ReadFrom(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := respBuf.Bytes()
|
|
|
|
|
2019-10-02 15:05:40 +02:00
|
|
|
conf = &SharepointSuccessResponse{}
|
2018-04-09 10:05:43 +02:00
|
|
|
err = xml.Unmarshal(s, conf)
|
2019-10-02 15:05:40 +02:00
|
|
|
if conf.Body.Token == "" {
|
|
|
|
// xml Unmarshal won't fail if the response doesn't contain a token
|
|
|
|
// However, the token will be empty
|
|
|
|
sErr := &SharepointError{}
|
|
|
|
|
|
|
|
errSErr := xml.Unmarshal(s, sErr)
|
|
|
|
if errSErr == nil {
|
|
|
|
return nil, sErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 10:05:43 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("error while reading endpoint response: %w", err)
|
2018-04-09 10:05:43 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|