Jump to content


Swarm API: C# API access


  • Please log in to reply
2 replies to this topic

#1 Clutchplate

Clutchplate

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 10 January 2019 - 06:45 PM

I'm trying to read the Swarm API from a C# client app, but am having issues with authentication. I think we use level 3 security (we use p4 login -ap to login to Swarm).

I have it working for a while by additionally adding the web browser cookie to the request. However this is bad, because extracting the web browser cookie is a pain in Windows 10 or non-IE browsers (and is probably a security issue...).

Here's my initial code to check whether the current user can access Swarm:

public async Task<bool> CheckAuthorization()
{
  bool result = false;
  var request = (HttpWebRequest)WebRequest.Create(@"http://p4swarm/files");
  request.UseDefaultCredentials = false;
  request.Credentials = new NetworkCredential() { UserName = "<my ticket number>", Domain = "MYCOMPANY", Password = string.Empty};
  request.MaximumAutomaticRedirections = 4;
  request.MaximumResponseHeadersLength = 4;
  request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
  // request.CookieContainer = new CookieContainer();
  // request.CookieContainer.Add(new Cookie("SWARM", "<web browser cookie>", "/", "p4swarm"));
  // request.CookieContainer.Add(new Cookie("remember", Environment.UserName, "/", "p4swarm"));
  request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
  request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
  try
  {
	Log.WriteLine($"AUTH: Sending WebRequest with {SwarmrSettings.Instance.SwarmCookie}, awaiting response");
	var p4Response = (HttpWebResponse)await request.GetResponseAsync();
	Log.WriteLine("AUTH: Response obtained. Status:" + p4Response.StatusCode.ToString());
	result = p4Response.StatusCode == HttpStatusCode.OK;
  }
  catch (Exception ex)
  {
	Log.WriteLine("AUTH: Exception caught in WebRequest. " + ex.Message);
	result = false;
  }
  return result;
}

If I run it as above I get a 401 Unauthorized error.

If I leave the commented out lines in the code, it works fine (provided I can find the browser cookie), but eventually the cookie expires, and the user can no longer use the app.

What is the correct way to authenticate from within a client app like this?

- Lutz

#2 Clutchplate

Clutchplate

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 10 January 2019 - 09:43 PM

Using Fiddler to look at the network traffic, Swarm does not even do an Authentication challenge. And sending the Authorization header has no effect either.

#3 Clutchplate

Clutchplate

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 11 January 2019 - 07:26 PM

Ok, I got some help from colleagues and WebRequest is not really the right fit for this. Here's what does work:

_httpClient = new HttpClient { BaseAddress = new Uri(@"http://p4swarm") };
var authString = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Environment.UserName}:{P4SwarmToken}"));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authString);
...
var p4Response = await _httpClient.GetAsync(@"api/v9/reviews");
 

- Lutz


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users