Skip to content

Commit

Permalink
Add Authorization header for V1 Services as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Roberts committed Nov 26, 2019
1 parent 19f1fd5 commit e4e179b
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions AtlassianCloudBackupsLibrary/BackupAtlassianService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,32 +159,14 @@ public async Task<IBackupJob> Execute(bool runCleanUpOnly = false)
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

Logger.Current.Log(_logLabel, "Attempting to log in to Atlassian...");
// Get auth token from the cloud instance
var content =
new StringContent(
"{" + string.Format(" \"username\" : \"{0}\", \"password\" : \"{1}\" ", UserName,
Password) + "}", Encoding.UTF8, "application/json");

var response = await Client.PostAsync(_service.AuthUrl, content);

if (response.IsSuccessStatusCode)
{
Logger.Current.Log(_logLabel,
string.Format("Successfully logged in to the {0} account as {1}.", Account, UserName));
}
else
{
Logger.Current.Log(_logLabel, "Failed to authenticate to Atlassian cloud servers!");
Logger.Current.Log(_logLabel,
string.Format("Aborting backup job for {0}!", GetServiceToBeBackedUpLabel()));
return this;
}
var basicHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{UserName}:{Password}"));
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicHeaderValue);

Logger.Current.Log(_logLabel, "Triggering backup...");
// Trigger backup
var triggerContent =
new StringContent("{ \"cbAttachments\" : true }", Encoding.UTF8, "application/json");
new StringContent("{ \"cbAttachments\" : \"true\", \"exportToCloud\" : \"true\" }",
Encoding.UTF8, "application/json");
var triggerResponse = await Client.PostAsync(_service.BackupTriggerUrl, triggerContent);

if (!triggerResponse.IsSuccessStatusCode)
Expand Down

0 comments on commit e4e179b

Please sign in to comment.