2016-12-19 21:38:06 +00:00
|
|
|
package lfsapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-01 19:41:10 +00:00
|
|
|
"github.com/git-lfs/git-lfs/v3/creds"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/errors"
|
|
|
|
"github.com/git-lfs/git-lfs/v3/lfshttp"
|
2021-12-14 16:05:42 +00:00
|
|
|
"github.com/git-lfs/git-lfs/v3/tr"
|
2016-12-19 21:38:06 +00:00
|
|
|
"github.com/rubyist/tracerx"
|
|
|
|
)
|
|
|
|
|
2016-12-19 22:05:35 +00:00
|
|
|
var (
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
defaultEndpointFinder = NewEndpointFinder(nil)
|
|
|
|
defaultMaxAuthAttempts = 3
|
2016-12-19 22:05:35 +00:00
|
|
|
)
|
|
|
|
|
2017-10-27 20:10:46 +00:00
|
|
|
// DoWithAuth sends an HTTP request to get an HTTP response. It attempts to add
|
|
|
|
// authentication from netrc or git's credential helpers if necessary,
|
2021-01-12 22:44:28 +00:00
|
|
|
// supporting basic authentication.
|
2019-11-28 19:03:52 +00:00
|
|
|
func (c *Client) DoWithAuth(remote string, access creds.Access, req *http.Request) (*http.Response, error) {
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
count := 0
|
|
|
|
res, err := c.doWithAuth(remote, &count, access, req, nil)
|
2018-09-27 23:46:56 +00:00
|
|
|
|
2018-10-02 23:47:10 +00:00
|
|
|
if errors.IsAuthError(err) {
|
2018-09-27 23:46:56 +00:00
|
|
|
if len(req.Header.Get("Authorization")) == 0 {
|
|
|
|
// This case represents a rejected request that
|
|
|
|
// should have been authenticated but wasn't. Do
|
|
|
|
// not count this against our redirection
|
|
|
|
// maximum.
|
2019-11-28 19:03:52 +00:00
|
|
|
newAccess := c.Endpoints.AccessFor(access.URL())
|
2018-10-02 23:35:54 +00:00
|
|
|
tracerx.Printf("api: http response indicates %q authentication. Resubmitting...", newAccess.Mode())
|
2018-10-02 23:47:10 +00:00
|
|
|
return c.DoWithAuth(remote, newAccess, req)
|
2018-09-27 23:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
2018-05-25 16:24:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 23:47:10 +00:00
|
|
|
// DoWithAuthNoRetry sends an HTTP request to get an HTTP response. It works in
|
|
|
|
// the same way as DoWithAuth, but will not retry the request if it fails with
|
|
|
|
// an authorization error.
|
2019-11-28 19:03:52 +00:00
|
|
|
func (c *Client) DoWithAuthNoRetry(remote string, access creds.Access, req *http.Request) (*http.Response, error) {
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
count := 0
|
|
|
|
return c.doWithAuth(remote, &count, access, req, nil)
|
2018-10-02 23:47:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:32 +00:00
|
|
|
// DoAPIRequestWithAuth sends an HTTP request to get an HTTP response similarly
|
|
|
|
// to DoWithAuth, but using the LFS API endpoint for the provided remote and
|
|
|
|
// operation to determine the access mode.
|
|
|
|
func (c *Client) DoAPIRequestWithAuth(remote string, req *http.Request) (*http.Response, error) {
|
|
|
|
operation := getReqOperation(req)
|
|
|
|
apiEndpoint := c.Endpoints.Endpoint(operation, remote)
|
|
|
|
access := c.Endpoints.AccessFor(apiEndpoint.Url)
|
2018-10-02 23:47:10 +00:00
|
|
|
return c.DoWithAuth(remote, access, req)
|
2018-09-24 23:45:32 +00:00
|
|
|
}
|
|
|
|
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
func (c *Client) doWithAuth(remote string, count *int, access creds.Access, req *http.Request, via []*http.Request) (*http.Response, error) {
|
|
|
|
if *count == defaultMaxAuthAttempts {
|
|
|
|
return nil, fmt.Errorf("too many authentication attempts")
|
|
|
|
}
|
|
|
|
|
2018-09-06 21:42:41 +00:00
|
|
|
req.Header = c.client.ExtraHeadersFor(req)
|
2017-11-16 20:04:42 +00:00
|
|
|
|
2019-04-18 14:41:55 +00:00
|
|
|
credWrapper, err := c.getCreds(remote, access, req)
|
2016-12-20 00:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
c.credContext.SetStateFields(credWrapper.Creds["state[]"])
|
2016-12-20 00:14:03 +00:00
|
|
|
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
res, err := c.doWithCreds(req, count, credWrapper, access, via)
|
2016-12-20 00:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.IsAuthError(err) {
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
multistage := credWrapper.Creds.IsMultistage()
|
|
|
|
newMode, newModes, headers := getAuthAccess(res, access.Mode(), c.access, multistage)
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
newAccess := access.Upgrade(newMode)
|
2018-10-02 23:35:54 +00:00
|
|
|
if newAccess.Mode() != access.Mode() {
|
2018-09-24 23:45:32 +00:00
|
|
|
c.Endpoints.SetAccess(newAccess)
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
c.access = newModes
|
2016-12-20 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:41:55 +00:00
|
|
|
if credWrapper.Creds != nil {
|
2018-09-27 23:46:56 +00:00
|
|
|
req.Header.Del("Authorization")
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
if multistage && *count < defaultMaxAuthAttempts && res != nil && res.StatusCode == 401 {
|
|
|
|
*count++
|
|
|
|
} else {
|
|
|
|
credWrapper.CredentialHelper.Reject(credWrapper.Creds)
|
|
|
|
}
|
2016-12-20 00:14:03 +00:00
|
|
|
}
|
Pass authentication types to Git credential helpers
Git recently added a new field to the credential helper, `wwwauth[]`,
which may be repeated and includes all of the `WWW-Authenticate` headers
so that the credential helper may choose an appropriate set of
credentials and extract any sort of necessary data from the field (such
as challenge).
In Git LFS, we also want to do this with the `LFS-Authenticate` headers
as well, since those are often used for the same purpose, so include
both these headers in that field when passing them to `git credential
fill`.
Note that `git credential fill` only honours this value and passes it to
the credential helper in Git 2.41 and newer (including the latest
`HEAD`). However, just to be safe, let's add an undocumented and
experimental option (`credential.*.skipwwwauth`) that users can use to
control this, which we can remove in a few releases if it turns out it's
not needed. Similarly, skip our new tests if we have an older version
of Git where this doesn't work, since they'll otherwise fail.
2023-05-31 13:42:17 +00:00
|
|
|
c.credContext.SetWWWAuthHeaders(headers)
|
2016-12-20 00:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:28:12 +00:00
|
|
|
if res != nil && res.StatusCode < 300 && res.StatusCode > 199 {
|
2019-04-18 14:41:55 +00:00
|
|
|
credWrapper.CredentialHelper.Approve(credWrapper.Creds)
|
2016-12-20 00:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
2016-12-19 22:05:35 +00:00
|
|
|
}
|
|
|
|
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
func (c *Client) doWithCreds(req *http.Request, count *int, credWrapper creds.CredentialHelperWrapper, access creds.Access, via []*http.Request) (*http.Response, error) {
|
2021-01-12 22:44:28 +00:00
|
|
|
if access.Mode() == creds.NegotiateAccess {
|
2019-11-28 19:57:16 +00:00
|
|
|
return c.doWithNegotiate(req, credWrapper)
|
2016-12-20 19:07:16 +00:00
|
|
|
}
|
2018-09-11 21:14:37 +00:00
|
|
|
|
|
|
|
req.Header.Set("User-Agent", lfshttp.UserAgent)
|
|
|
|
|
2019-11-28 19:19:26 +00:00
|
|
|
client, err := c.client.HttpClient(req.URL, access.Mode())
|
2019-10-28 16:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectedReq, res, err := c.client.DoWithRedirect(client, req, "", via)
|
2018-09-11 21:14:37 +00:00
|
|
|
if err != nil || res != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if redirectedReq == nil {
|
2021-12-14 16:05:42 +00:00
|
|
|
return res, errors.New(tr.Tr.Get("failed to redirect request"))
|
2018-09-11 21:14:37 +00:00
|
|
|
}
|
|
|
|
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
return c.doWithAuth("", count, access, redirectedReq, via)
|
2016-12-20 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 18:13:42 +00:00
|
|
|
// getCreds fills the authorization header for the given request if possible,
|
|
|
|
// from the following sources:
|
|
|
|
//
|
|
|
|
// 1. NTLM access is handled elsewhere.
|
|
|
|
// 2. Existing Authorization or ?token query tells LFS that the request is ready.
|
|
|
|
// 3. Netrc based on the hostname.
|
|
|
|
// 4. URL authentication on the Endpoint URL or the Git Remote URL.
|
|
|
|
// 5. Git Credential Helper, potentially prompting the user.
|
|
|
|
//
|
|
|
|
// There are three URLs in play, that make this a little confusing.
|
|
|
|
//
|
2022-09-26 01:15:31 +00:00
|
|
|
// 1. The request URL, which should be something like "https://git.com/repo.git/info/lfs/objects/batch"
|
|
|
|
// 2. The LFS API URL, which should be something like "https://git.com/repo.git/info/lfs"
|
|
|
|
// This URL used for the "lfs.URL.access" git config key, which determines
|
|
|
|
// what kind of auth the LFS server expects. Could be BasicAccess,
|
|
|
|
// NTLMAccess, NegotiateAccess, or NoneAccess, in which the Git Credential
|
|
|
|
// Helper step is skipped. We do not want to prompt the user for a password
|
|
|
|
// to fetch public repository data.
|
|
|
|
// 3. The Git Remote URL, which should be something like "https://git.com/repo.git"
|
|
|
|
// This URL is used for the Git Credential Helper. This way existing https
|
|
|
|
// Git remote credentials can be re-used for LFS.
|
2019-11-28 19:03:52 +00:00
|
|
|
func (c *Client) getCreds(remote string, access creds.Access, req *http.Request) (creds.CredentialHelperWrapper, error) {
|
2017-10-26 22:09:37 +00:00
|
|
|
ef := c.Endpoints
|
|
|
|
if ef == nil {
|
|
|
|
ef = defaultEndpointFinder
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:38:06 +00:00
|
|
|
operation := getReqOperation(req)
|
|
|
|
apiEndpoint := ef.Endpoint(operation, remote)
|
2017-01-05 21:13:27 +00:00
|
|
|
|
2021-01-12 22:44:28 +00:00
|
|
|
if access.Mode() != creds.NegotiateAccess {
|
2019-11-28 19:03:52 +00:00
|
|
|
if requestHasAuth(req) || access.Mode() == creds.NoneAccess {
|
2019-05-01 16:45:21 +00:00
|
|
|
return creds.CredentialHelperWrapper{CredentialHelper: creds.NullCreds, Input: nil, Url: nil, Creds: nil}, nil
|
2017-01-05 21:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
credsURL, err := getCredURLForAPI(ef, operation, remote, apiEndpoint, req)
|
|
|
|
if err != nil {
|
2022-01-28 05:49:32 +00:00
|
|
|
return creds.CredentialHelperWrapper{CredentialHelper: creds.NullCreds, Input: nil, Url: nil, Creds: nil}, errors.Wrap(err, tr.Tr.Get("credentials"))
|
2017-01-05 21:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if credsURL == nil {
|
2019-05-01 16:45:21 +00:00
|
|
|
return creds.CredentialHelperWrapper{CredentialHelper: creds.NullCreds, Input: nil, Url: nil, Creds: nil}, nil
|
2017-01-05 21:13:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:41:55 +00:00
|
|
|
credWrapper := c.getGitCredsWrapper(ef, req, credsURL)
|
|
|
|
err = credWrapper.FillCreds()
|
2017-10-26 22:09:37 +00:00
|
|
|
if err == nil {
|
|
|
|
tracerx.Printf("Filled credentials for %s", credsURL)
|
2024-05-10 15:46:23 +00:00
|
|
|
setRequestAuthWithCreds(req, credWrapper.Creds)
|
2017-10-26 22:09:37 +00:00
|
|
|
}
|
2019-04-18 14:41:55 +00:00
|
|
|
return credWrapper, err
|
2016-12-19 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 22:44:28 +00:00
|
|
|
// Negotiate only
|
2017-10-26 22:09:37 +00:00
|
|
|
|
2017-01-05 21:13:27 +00:00
|
|
|
credsURL, err := url.Parse(apiEndpoint.Url)
|
2016-12-23 03:06:51 +00:00
|
|
|
if err != nil {
|
2022-01-28 05:49:32 +00:00
|
|
|
return creds.CredentialHelperWrapper{CredentialHelper: creds.NullCreds, Input: nil, Url: nil, Creds: nil}, errors.Wrap(err, tr.Tr.Get("credentials"))
|
2016-12-19 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 22:09:37 +00:00
|
|
|
// NTLM uses creds to create the session
|
2019-04-18 14:41:55 +00:00
|
|
|
credWrapper := c.getGitCredsWrapper(ef, req, credsURL)
|
|
|
|
return credWrapper, err
|
2016-12-19 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:41:55 +00:00
|
|
|
func (c *Client) getGitCredsWrapper(ef EndpointFinder, req *http.Request, u *url.URL) creds.CredentialHelperWrapper {
|
2019-05-01 16:45:21 +00:00
|
|
|
return c.credContext.GetCredentialHelper(c.Credentials, u)
|
2016-12-19 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 21:42:41 +00:00
|
|
|
func getCredURLForAPI(ef EndpointFinder, operation, remote string, apiEndpoint lfshttp.Endpoint, req *http.Request) (*url.URL, error) {
|
2016-12-23 03:06:51 +00:00
|
|
|
apiURL, err := url.Parse(apiEndpoint.Url)
|
2016-12-19 21:38:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the LFS request doesn't match the current LFS url, don't bother
|
|
|
|
// attempting to set the Authorization header from the LFS or Git remote URLs.
|
2016-12-23 03:06:51 +00:00
|
|
|
if req.URL.Scheme != apiURL.Scheme ||
|
|
|
|
req.URL.Host != apiURL.Host {
|
2016-12-19 21:38:06 +00:00
|
|
|
return req.URL, nil
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
if setRequestAuthFromURL(req, apiURL) {
|
2016-12-19 21:38:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(remote) > 0 {
|
|
|
|
if u := ef.GitRemoteURL(remote, operation == "upload"); u != "" {
|
2019-08-14 14:49:48 +00:00
|
|
|
schemedUrl, _ := fixSchemelessURL(u)
|
2017-03-27 22:38:04 +00:00
|
|
|
|
|
|
|
gitRemoteURL, err := url.Parse(schemedUrl)
|
2016-12-19 21:38:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
if gitRemoteURL.Scheme == apiURL.Scheme &&
|
|
|
|
gitRemoteURL.Host == apiURL.Host {
|
2016-12-19 21:38:06 +00:00
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
if setRequestAuthFromURL(req, gitRemoteURL) {
|
2016-12-19 21:38:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
return gitRemoteURL, nil
|
2016-12-19 21:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
return apiURL, nil
|
2016-12-19 21:38:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 14:49:48 +00:00
|
|
|
// fixSchemelessURL prepends an empty scheme "//" if none was found in
|
|
|
|
// the URL and replaces the first colon with a slash in order to satisfy RFC
|
|
|
|
// 3986 §3.3, and `net/url.Parse()`.
|
2017-03-27 22:38:04 +00:00
|
|
|
//
|
|
|
|
// It returns a string parse-able with `net/url.Parse()` and a boolean whether
|
|
|
|
// or not an empty scheme was added.
|
2019-08-14 14:49:48 +00:00
|
|
|
func fixSchemelessURL(u string) (string, bool) {
|
2017-03-27 22:38:04 +00:00
|
|
|
if hasScheme(u) {
|
|
|
|
return u, false
|
|
|
|
}
|
|
|
|
|
|
|
|
colon := strings.Index(u, ":")
|
|
|
|
slash := strings.Index(u, "/")
|
|
|
|
|
|
|
|
if colon >= 0 && (slash < 0 || colon < slash) {
|
|
|
|
// First path segment has a colon, assumed that it's a
|
|
|
|
// scheme-less URL. Append an empty scheme on top to
|
|
|
|
// satisfy RFC 3986 §3.3, and `net/url.Parse()`.
|
2019-08-14 14:49:48 +00:00
|
|
|
//
|
|
|
|
// In addition, replace the first colon with a slash since
|
|
|
|
// otherwise the colon looks like it's introducing a port
|
|
|
|
// number.
|
|
|
|
return fmt.Sprintf("//%s", strings.Replace(u, ":", "/", 1)), true
|
2017-03-27 22:38:04 +00:00
|
|
|
}
|
|
|
|
return u, true
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// supportedSchemes is the list of URL schemes the `lfsapi` package
|
|
|
|
// supports.
|
|
|
|
supportedSchemes = []string{"ssh", "http", "https"}
|
|
|
|
)
|
|
|
|
|
|
|
|
// hasScheme returns whether or not a given string (taken to represent a RFC
|
|
|
|
// 3986 URL) has a scheme that is supported by the `lfsapi` package.
|
|
|
|
func hasScheme(what string) bool {
|
|
|
|
for _, scheme := range supportedSchemes {
|
|
|
|
if strings.HasPrefix(what, fmt.Sprintf("%s://", scheme)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
func requestHasAuth(req *http.Request) bool {
|
2018-05-17 03:48:46 +00:00
|
|
|
// The "Authorization" string constant is safe, since we assume that all
|
|
|
|
// request headers have been canonicalized.
|
2016-12-19 21:38:06 +00:00
|
|
|
if len(req.Header.Get("Authorization")) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(req.URL.Query().Get("token")) > 0
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:06:51 +00:00
|
|
|
func setRequestAuthFromURL(req *http.Request, u *url.URL) bool {
|
|
|
|
if u.User == nil {
|
2016-12-19 21:38:06 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pass, ok := u.User.Password(); ok {
|
2021-12-14 16:05:42 +00:00
|
|
|
fmt.Fprintln(os.Stderr, tr.Tr.Get("warning: current Git remote contains credentials"))
|
2016-12-19 21:38:06 +00:00
|
|
|
setRequestAuth(req, u.User.Username(), pass)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func setRequestAuth(req *http.Request, user, pass string) {
|
|
|
|
if len(user) == 0 && len(pass) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token := fmt.Sprintf("%s:%s", user, pass)
|
|
|
|
auth := "Basic " + strings.TrimSpace(base64.StdEncoding.EncodeToString([]byte(token)))
|
|
|
|
req.Header.Set("Authorization", auth)
|
|
|
|
}
|
|
|
|
|
2024-05-10 15:46:23 +00:00
|
|
|
func setRequestAuthWithCreds(req *http.Request, c creds.Creds) {
|
|
|
|
authtype := creds.FirstEntryForKey(c, "authtype")
|
|
|
|
credential := creds.FirstEntryForKey(c, "credential")
|
|
|
|
if len(authtype) == 0 && len(credential) == 0 {
|
|
|
|
user := creds.FirstEntryForKey(c, "username")
|
|
|
|
pass := creds.FirstEntryForKey(c, "password")
|
|
|
|
setRequestAuth(req, user, pass)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := fmt.Sprintf("%s %s", authtype, credential)
|
|
|
|
req.Header.Set("Authorization", auth)
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:38:06 +00:00
|
|
|
func getReqOperation(req *http.Request) string {
|
|
|
|
operation := "download"
|
|
|
|
if req.Method == "POST" || req.Method == "PUT" {
|
|
|
|
operation = "upload"
|
|
|
|
}
|
|
|
|
return operation
|
|
|
|
}
|
2016-12-20 00:14:03 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
authenticateHeaders = []string{"Lfs-Authenticate", "Www-Authenticate"}
|
|
|
|
)
|
|
|
|
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
func getAuthAccess(res *http.Response, access creds.AccessMode, modes []creds.AccessMode, multistage bool) (creds.AccessMode, []creds.AccessMode, []string) {
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
newModes := make([]creds.AccessMode, 0, len(modes))
|
|
|
|
for _, mode := range modes {
|
Implement multistage authentication
In 2.46, Git will support multistage authentication from credential
helpers. This will allow Git and Git LFS to implement functionality
like NTLM and Kerberos in the credential helper, which lets this
functionality to work even if Git LFS doesn't support it natively.
This requires two separate pieces of data. First, it involves a
`state[]` field, which each credential helper can add to keep track of
state. Second, it allows the usage of a boolean `continue` field, which
indicates that the response is multistage and this is not the final
stage.
In order to make this work, we adjust a few things. First, we advertise
the `state` capability. Additionally, we save and pass back the
`state[]` fields that the credential helper may send to us. We also
don't change the authentication scheme if the helper told us that this
was a multistage response. Finally, we add a check to avoid a
credential helper getting stuck in an infinite loop if it keeps handing
back the same credentials.
2024-06-17 15:33:27 +00:00
|
|
|
if multistage || access != mode {
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
newModes = append(newModes, mode)
|
|
|
|
}
|
|
|
|
}
|
Pass authentication types to Git credential helpers
Git recently added a new field to the credential helper, `wwwauth[]`,
which may be repeated and includes all of the `WWW-Authenticate` headers
so that the credential helper may choose an appropriate set of
credentials and extract any sort of necessary data from the field (such
as challenge).
In Git LFS, we also want to do this with the `LFS-Authenticate` headers
as well, since those are often used for the same purpose, so include
both these headers in that field when passing them to `git credential
fill`.
Note that `git credential fill` only honours this value and passes it to
the credential helper in Git 2.41 and newer (including the latest
`HEAD`). However, just to be safe, let's add an undocumented and
experimental option (`credential.*.skipwwwauth`) that users can use to
control this, which we can remove in a few releases if it turns out it's
not needed. Similarly, skip our new tests if we have an older version
of Git where this doesn't work, since they'll otherwise fail.
2023-05-31 13:42:17 +00:00
|
|
|
headers := make([]string, 0)
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
if res != nil {
|
Pass authentication types to Git credential helpers
Git recently added a new field to the credential helper, `wwwauth[]`,
which may be repeated and includes all of the `WWW-Authenticate` headers
so that the credential helper may choose an appropriate set of
credentials and extract any sort of necessary data from the field (such
as challenge).
In Git LFS, we also want to do this with the `LFS-Authenticate` headers
as well, since those are often used for the same purpose, so include
both these headers in that field when passing them to `git credential
fill`.
Note that `git credential fill` only honours this value and passes it to
the credential helper in Git 2.41 and newer (including the latest
`HEAD`). However, just to be safe, let's add an undocumented and
experimental option (`credential.*.skipwwwauth`) that users can use to
control this, which we can remove in a few releases if it turns out it's
not needed. Similarly, skip our new tests if we have an older version
of Git where this doesn't work, since they'll otherwise fail.
2023-05-31 13:42:17 +00:00
|
|
|
for _, headerName := range authenticateHeaders {
|
|
|
|
headers = append(headers, res.Header[headerName]...)
|
|
|
|
}
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
supportedModes := make(map[creds.AccessMode]struct{})
|
|
|
|
for _, headerName := range authenticateHeaders {
|
|
|
|
for _, auth := range res.Header[headerName] {
|
|
|
|
pieces := strings.SplitN(strings.ToLower(auth), " ", 2)
|
|
|
|
if len(pieces) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-12-20 16:40:58 +00:00
|
|
|
|
2024-05-10 15:46:23 +00:00
|
|
|
supportedModes[creds.AccessMode(pieces[0])] = struct{}{}
|
Fall back from Negotiate to Basic
The Negotiate authentication scheme can support multiple different types
of authentication. The two most popular are NTLM and Kerberos. When we
supported NTLM, we'd first try Kerberos, and if it failed, fall back to
NTLM.
However, we no longer support NTLM, but some people still have server
configuration that uses NTLM via Negotiate. For these people,
authentication may be broken. Let's fall back to Basic in such a case
by keeping track of which authentication mechanisms we've tried,
keeping only the supported mechanisms if we got a response, and
stripping out failing mechanisms, falling back to Basic.
To help with servers that support both Negotiate and Basic, we
specifically consider SPNEGO (Negotiate) errors as authentication
errors. This is because if the server supports Kerberos but the client
does not have a ticket, then we'll get an error trying to read the
client's tickets, which will manifest in this way.
2022-01-13 20:10:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, mode := range newModes {
|
|
|
|
if _, ok := supportedModes[mode]; ok {
|
Pass authentication types to Git credential helpers
Git recently added a new field to the credential helper, `wwwauth[]`,
which may be repeated and includes all of the `WWW-Authenticate` headers
so that the credential helper may choose an appropriate set of
credentials and extract any sort of necessary data from the field (such
as challenge).
In Git LFS, we also want to do this with the `LFS-Authenticate` headers
as well, since those are often used for the same purpose, so include
both these headers in that field when passing them to `git credential
fill`.
Note that `git credential fill` only honours this value and passes it to
the credential helper in Git 2.41 and newer (including the latest
`HEAD`). However, just to be safe, let's add an undocumented and
experimental option (`credential.*.skipwwwauth`) that users can use to
control this, which we can remove in a few releases if it turns out it's
not needed. Similarly, skip our new tests if we have an older version
of Git where this doesn't work, since they'll otherwise fail.
2023-05-31 13:42:17 +00:00
|
|
|
return mode, newModes, headers
|
2016-12-20 00:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Pass authentication types to Git credential helpers
Git recently added a new field to the credential helper, `wwwauth[]`,
which may be repeated and includes all of the `WWW-Authenticate` headers
so that the credential helper may choose an appropriate set of
credentials and extract any sort of necessary data from the field (such
as challenge).
In Git LFS, we also want to do this with the `LFS-Authenticate` headers
as well, since those are often used for the same purpose, so include
both these headers in that field when passing them to `git credential
fill`.
Note that `git credential fill` only honours this value and passes it to
the credential helper in Git 2.41 and newer (including the latest
`HEAD`). However, just to be safe, let's add an undocumented and
experimental option (`credential.*.skipwwwauth`) that users can use to
control this, which we can remove in a few releases if it turns out it's
not needed. Similarly, skip our new tests if we have an older version
of Git where this doesn't work, since they'll otherwise fail.
2023-05-31 13:42:17 +00:00
|
|
|
return creds.BasicAccess, newModes, headers
|
2016-12-20 00:14:03 +00:00
|
|
|
}
|