2015-10-17 07:41:57 +03:00
|
|
|
package ntlmssp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2016-06-17 22:47:12 +03:00
|
|
|
"io"
|
2015-10-17 07:41:57 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Negotiator is a http.Roundtripper decorator that automatically
|
|
|
|
//converts basic authentication to NTLM/Negotiate authentication when appropriate.
|
|
|
|
type Negotiator struct{ http.RoundTripper }
|
|
|
|
|
|
|
|
//RoundTrip sends the request to the server, handling any authentication
|
|
|
|
//re-sends as needed.
|
|
|
|
func (l Negotiator) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
2016-04-13 01:49:15 +03:00
|
|
|
// Use default round tripper if not provided
|
|
|
|
rt := l.RoundTripper
|
|
|
|
if rt == nil {
|
|
|
|
rt = http.DefaultTransport
|
2015-10-17 07:41:57 +03:00
|
|
|
}
|
2016-04-13 01:49:15 +03:00
|
|
|
// If it is not basic auth, just round trip the request as usual
|
2015-10-17 07:41:57 +03:00
|
|
|
reqauth := authheader(req.Header.Get("Authorization"))
|
2016-04-13 01:49:15 +03:00
|
|
|
if !reqauth.IsBasic() {
|
|
|
|
return rt.RoundTrip(req)
|
|
|
|
}
|
|
|
|
// Save request body
|
|
|
|
body := bytes.Buffer{}
|
|
|
|
if req.Body != nil {
|
|
|
|
_, err = body.ReadFrom(req.Body)
|
2015-10-17 07:41:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-13 01:49:15 +03:00
|
|
|
|
|
|
|
req.Body.Close()
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
|
|
|
|
}
|
|
|
|
// first try anonymous, in case the server still finds us
|
|
|
|
// authenticated from previous traffic
|
|
|
|
req.Header.Del("Authorization")
|
|
|
|
res, err = rt.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusUnauthorized {
|
|
|
|
return res, err
|
2015-10-17 07:41:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resauth := authheader(res.Header.Get("Www-Authenticate"))
|
|
|
|
if !resauth.IsNegotiate() {
|
|
|
|
// Unauthorized, Negotiate not requested, let's try with basic auth
|
|
|
|
req.Header.Set("Authorization", string(reqauth))
|
2016-06-17 22:47:12 +03:00
|
|
|
io.Copy(ioutil.Discard, res.Body)
|
2016-04-13 01:49:15 +03:00
|
|
|
res.Body.Close()
|
2015-10-17 07:41:57 +03:00
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
|
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
res, err = rt.RoundTrip(req)
|
2015-10-17 07:41:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-13 01:49:15 +03:00
|
|
|
if res.StatusCode != http.StatusUnauthorized {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
resauth = authheader(res.Header.Get("Www-Authenticate"))
|
2015-10-17 07:41:57 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
if resauth.IsNegotiate() {
|
|
|
|
// 401 with request:Basic and response:Negotiate
|
2016-06-17 22:47:12 +03:00
|
|
|
io.Copy(ioutil.Discard, res.Body)
|
2016-04-13 01:49:15 +03:00
|
|
|
res.Body.Close()
|
2015-10-17 07:41:57 +03:00
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
// recycle credentials
|
|
|
|
u, p, err := reqauth.GetBasicCreds()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-17 07:41:57 +03:00
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
// send negotiate
|
|
|
|
negotiateMessage := NewNegotiateMessage()
|
|
|
|
req.Header.Set("Authorization", "Negotiate "+base64.StdEncoding.EncodeToString(negotiateMessage))
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
|
2015-10-17 07:41:57 +03:00
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
res, err = rt.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-17 07:41:57 +03:00
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
// receive challenge?
|
|
|
|
resauth = authheader(res.Header.Get("Www-Authenticate"))
|
|
|
|
challengeMessage, err := resauth.GetData()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !resauth.IsNegotiate() || len(challengeMessage) == 0 {
|
|
|
|
// Negotiation failed, let client deal with response
|
|
|
|
return res, nil
|
|
|
|
}
|
2016-06-17 22:47:12 +03:00
|
|
|
io.Copy(ioutil.Discard, res.Body)
|
2016-04-13 01:49:15 +03:00
|
|
|
res.Body.Close()
|
2015-10-17 07:41:57 +03:00
|
|
|
|
2016-04-13 01:49:15 +03:00
|
|
|
// send authenticate
|
|
|
|
authenticateMessage, err := ProcessChallenge(challengeMessage, u, p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-17 07:41:57 +03:00
|
|
|
}
|
2016-04-13 01:49:15 +03:00
|
|
|
req.Header.Set("Authorization", "Negotiate "+base64.StdEncoding.EncodeToString(authenticateMessage))
|
|
|
|
req.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
|
|
|
|
|
|
|
|
res, err = rt.RoundTrip(req)
|
2015-10-17 07:41:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|