Skip to content

Commit 36b651a

Browse files
authored
Merge pull request #17 from GRAPHISOFT/gs-lfs-password-auth
Fix password based authentication for LFS
2 parents 7a43762 + 4dec98c commit 36b651a

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

p4-fusion/lfs/communication/lfscomm.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ BatchResponse PerformBatchUploadRequest(const std::string& serverUrl, const Cred
369369
return result;
370370
}
371371

372-
Communicator::UploadResult PerformUpload(const std::string& uploadUrl, const std::vector<char>& fileContents, const std::map<std::string, std::string>& actionHeaders)
372+
Communicator::UploadResult PerformUpload(const std::string& uploadUrl, const std::vector<char>& fileContents, const std::map<std::string, std::string>& actionHeaders, const Credentials& auth = Credentials())
373373
{
374374
// Initialize curl
375375
CURLHandle curl;
@@ -390,11 +390,13 @@ Communicator::UploadResult PerformUpload(const std::string& uploadUrl, const std
390390
uploadHeaders = curl_slist_append(uploadHeaders, "Accept: application/vnd.git-lfs");
391391
}
392392

393+
const Credentials& authToUse = (actionHeaders.find("Authorization") == actionHeaders.end()) ? auth : Credentials();
394+
393395
// Add action-specific headers from the batch response
394396
uploadHeaders = CreateHeadersFromMap(actionHeaders, uploadHeaders);
395397

396398
RequestResult uploadResult = {};
397-
SetupRequest(curl.get(), uploadUrl, fileContents.data(), fileContents.size(), uploadHeaders, &uploadResult);
399+
SetupRequest(curl.get(), uploadUrl, fileContents.data(), fileContents.size(), uploadHeaders, &uploadResult, authToUse);
398400
curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, "PUT");
399401

400402
uploadResult = PerformRequestWithRetry([&]() -> RequestResult
@@ -436,7 +438,7 @@ std::string CreateVerifyPayload(const std::string& oid, size_t fileSize)
436438
return verifyBuffer.GetString();
437439
}
438440

439-
bool PerformVerify(const std::string& verifyUrl, const std::string& oid, size_t fileSize, const std::map<std::string, std::string>& actionHeaders)
441+
bool PerformVerify(const std::string& verifyUrl, const std::string& oid, size_t fileSize, const std::map<std::string, std::string>& actionHeaders, const Credentials& auth = Credentials())
440442
{
441443
CURLHandle curl;
442444
if (!curl)
@@ -451,10 +453,10 @@ bool PerformVerify(const std::string& verifyUrl, const std::string& oid, size_t
451453
// Add action-specific headers from the batch response
452454
verifyHeaders = CreateHeadersFromMap(actionHeaders, verifyHeaders);
453455

454-
// Perform request without authentication (headers from action should contain auth)
455-
RequestResult verifyResult = {};
456-
SetupRequest(curl.get(), verifyUrl, verifyPayload.data(), verifyPayload.size(), verifyHeaders, &verifyResult);
456+
const Credentials& authToUse = (actionHeaders.find("Authorization") == actionHeaders.end()) ? auth : Credentials();
457457

458+
RequestResult verifyResult = {};
459+
SetupRequest(curl.get(), verifyUrl, verifyPayload.data(), verifyPayload.size(), verifyHeaders, &verifyResult, authToUse);
458460
verifyResult = PerformRequestWithRetry([&]() -> RequestResult
459461
{
460462
verifyResult.curl_result = curl_easy_perform(curl.get());
@@ -492,15 +494,15 @@ Communicator::UploadResult LFSComm::UploadFile(const std::vector<char>& fileCont
492494
return UploadResult::AlreadyExists;
493495
}
494496

495-
auto uploadResult = PerformUpload(batchResponse.uploadUrl, fileContents, batchResponse.uploadHeaders);
497+
auto uploadResult = PerformUpload(batchResponse.uploadUrl, fileContents, batchResponse.uploadHeaders, m_Creds);
496498
if (uploadResult != UploadResult::Uploaded)
497499
{
498500
return uploadResult;
499501
}
500502

501503
if (!batchResponse.verifyUrl.empty())
502504
{
503-
if (!PerformVerify(batchResponse.verifyUrl, oid, fileContents.size(), batchResponse.verifyHeaders))
505+
if (!PerformVerify(batchResponse.verifyUrl, oid, fileContents.size(), batchResponse.verifyHeaders, m_Creds))
504506
{
505507
return UploadResult::Error;
506508
}

0 commit comments

Comments
 (0)