Wednesday, 7 July 2010

BASIC AUTHENTICATION (FOR GOOGLE CHECKOUT)

Using GCheckout.dll

Join("") is an IEnumerable extension I wrote (to concatenate strings with a separator) Base64Decode() is a string extension I wrote (decode base64)

Configuration.Google[Test]Merchant{Id|Key} are just some static variables holding my info.

  1. private GCheckout.EnvironmentType CheckAuthorization(string authHeader)
  2. {
  3. if (string.IsNullOrEmpty(authHeader))
  4. {
  5. Logger.Debug("No Authorization Header");
  6. return GCheckout.EnvironmentType.Unknown;
  7. }
  8. if (authHeader.Contains("Basic"))
  9. authHeader = authHeader.Substring(authHeader.IndexOf("Basic") + 5);
  10. var base64Auth = authHeader.Trim().Base64Decode();
  11. var userPass = base64Auth.Split(':');
  12. if (userPass.Length < 2)
  13. {
  14. Logger.Debug("Authorization Header was malformed. Raw: " + authHeader + ", Base64Decoded: " + base64Auth);
  15. return GCheckout.EnvironmentType.Unknown;
  16. }
  17. if (userPass.Length > 2)
  18. userPass[1] = userPass.Skip(1).Join("");
  19. if (userPass[0] == Configuration.GoogleMerchantId &&
  20. userPass[1] == Configuration.GoogleMerchantKey)
  21. {
  22. return GCheckout.EnvironmentType.Production;
  23. }
  24. else if (userPass[0] == Configuration.GoogleTestMerchantId &&
  25. userPass[1] == Configuration.GoogleTestMerchantKey)
  26. {
  27. return GCheckout.EnvironmentType.Sandbox;
  28. }
  29. else
  30. {
  31. return GCheckout.EnvironmentType.Unknown;
  32. }
  33. }

No comments:

Post a Comment