forked from aksdb/CalAnonSync
Simplified authentication negotiation
This commit is contained in:
parent
a739a9b4bb
commit
7ca7675a85
|
@ -66,28 +66,24 @@ type EWSRoundTripper struct {
|
|||
}
|
||||
|
||||
func (er EWSRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
if er.authType == authTypeBasic {
|
||||
r.SetBasicAuth(er.username, er.password)
|
||||
}
|
||||
|
||||
er.initMutex.Lock()
|
||||
resp, err := er.delegate.RoundTrip(r)
|
||||
if err == nil && resp.StatusCode == http.StatusUnauthorized && er.authType == authTypeUnknown {
|
||||
// This is a good time to find out what the server prefers.
|
||||
authHeaders := resp.Header["Www-Authenticate"]
|
||||
if authHeaders != nil {
|
||||
for _, h := range authHeaders {
|
||||
if strings.HasPrefix(h, "BASIC") {
|
||||
er.authType = authTypeBasic
|
||||
} else if strings.HasPrefix(h, "NTLM") {
|
||||
er.authType = authTypeNTLM
|
||||
break // NTLM is the best we could do
|
||||
if er.authType == authTypeUnknown {
|
||||
// Find authentication scheme.
|
||||
resp, err := http.DefaultClient.Get(r.URL.String())
|
||||
if err == nil && resp.StatusCode == http.StatusUnauthorized {
|
||||
// This is a good time to find out what the server prefers.
|
||||
authHeaders := resp.Header["Www-Authenticate"]
|
||||
if authHeaders != nil {
|
||||
for _, h := range authHeaders {
|
||||
if strings.HasPrefix(h, "BASIC") {
|
||||
er.authType = authTypeBasic
|
||||
} else if strings.HasPrefix(h, "NTLM") {
|
||||
er.authType = authTypeNTLM
|
||||
break // NTLM is the best we could do
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// So, do we know more than before? If so, try again.
|
||||
if er.authType > authTypeUnknown {
|
||||
if er.authType == authTypeNTLM {
|
||||
// We need to replace the delegator.
|
||||
er.delegate = &httpntlm.NtlmTransport{
|
||||
|
@ -96,13 +92,15 @@ func (er EWSRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
|||
Password: er.password,
|
||||
}
|
||||
}
|
||||
er.initMutex.Unlock()
|
||||
return er.RoundTrip(r)
|
||||
}
|
||||
}
|
||||
|
||||
er.initMutex.Unlock()
|
||||
return resp, err
|
||||
|
||||
if er.authType == authTypeBasic {
|
||||
r.SetBasicAuth(er.username, er.password)
|
||||
}
|
||||
|
||||
return er.delegate.RoundTrip(r)
|
||||
}
|
||||
|
||||
func NewEWSCalendar(url, username, password string) *EWSCalendar {
|
||||
|
|
Loading…
Reference in New Issue