/JayCutExample/JayCutter.cs

https://github.com/mpj/jaycut-dotnet-example · C# · 123 lines · 60 code · 15 blank · 48 comment · 0 complexity · 7d6434bf552568d93b77beb0089e4e3e MD5 · raw file

  1. using System;
  2. using System.Globalization;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace JayCutExample
  6. {
  7. public enum Method
  8. {
  9. // ReSharper disable InconsistentNaming
  10. GET,
  11. POST,
  12. PUT,
  13. DELETE
  14. // ReSharper restore InconsistentNaming
  15. }
  16. /// <summary>
  17. /// Example wrapper around the JayCut REST Service.
  18. /// </summary>
  19. public class JayCutter
  20. {
  21. public string JayCutApiSecret { get; set; }
  22. public string JayCutUriAuthority { get; set; }
  23. public string JayCutApiKey { get; set; }
  24. /// <summary>
  25. /// Constructs a Uri for a given jaycut API command, with authentication querystring added.
  26. /// </summary>
  27. /// <param name="command">The command path, beginning with a slash. Example: /Users/wayne.johnson</param>
  28. /// <param name="method">The method of the command. Example: PUT</param>
  29. /// <param name="doLogin">If this is set to true, the command uri will cause a session cookie to be set on the client, which means that further
  30. /// commands from that client won't need the authentication string. An url with this set to true is required by the Flash Mixer.</param>
  31. /// <returns></returns>
  32. public string GetUri(string command, Method method, bool doLogin)
  33. {
  34. var expires = ToUnixTimestamp(DateTime.Now.AddDays(1));
  35. var signatureBase = JayCutApiSecret + method + command + expires;
  36. return "http://" + JayCutUriAuthority
  37. + command +
  38. "?login=" + doLogin +
  39. "&api_key=" + JayCutApiKey +
  40. "&signature=" + ToSHA1Hash(signatureBase) +
  41. "&expires=" + expires +
  42. "&_method=" + method;
  43. // Some clients have a problem with PUT and DELETE, _method insures
  44. // that that there are no misunderstandings.
  45. }
  46. /// <summary>
  47. /// The simplest of simplest implementation. It creates a new user
  48. /// and logs him in. This is really only useful when you want to give a non-logged in
  49. /// a demo of the editor.
  50. /// </summary>
  51. /// <returns></returns>
  52. public string JayCutLoginUriSimple()
  53. {
  54. // A POST to /users will create a new user on the JayCut REST API,
  55. // and if doLogin is set to true, sign that user in (which is needed by Flash Mixer)
  56. return GetUri("/users", Method.POST, true);
  57. }
  58. /// <summary>
  59. /// Get a login URI for user with a specific ID. This is a really handy method
  60. /// for integrating the JayCut editor into your own community, using your own user database
  61. /// and login system.
  62. /// </summary>
  63. /// <param name="userId">The id of the user you want to create on the Jaycut system.
  64. /// This can, for instance, be the id of the user in your own user database.
  65. /// <returns>An url that signs in (and creates, if needed) the user with userId.</returns>
  66. public string JayCutLogUriForUser(string userId)
  67. {
  68. var commandPath = "/users/" + userId;
  69. return GetUri(commandPath, Method.PUT, true);
  70. }
  71. #region Internal helper methods
  72. /// <summary>
  73. /// Converts a DateTime to a standard unix timestamp.
  74. /// </summary>
  75. /// <param name="dt"></param>
  76. /// <returns>Unix timestamp as a long.</returns>
  77. private static long ToUnixTimestamp(DateTime dt)
  78. {
  79. var unixRef = new DateTime(1970, 1, 1, 0, 0, 0);
  80. return (dt.Ticks - unixRef.Ticks) / 10000000;
  81. }
  82. /// <summary>
  83. /// Calculates SHA1 sigurature from a string,
  84. /// identical in functionality to the "sha1" function of PHP.
  85. /// </summary>
  86. /// <param name="text">input string</param>
  87. /// <returns>SHA1 hash</returns>
  88. private static string ToSHA1Hash(string text)
  89. {
  90. return ToSHA1Hash(text, Encoding.UTF8);
  91. }
  92. /// <summary>
  93. /// Calculates SHA1 sigurature from a string.
  94. /// </summary>
  95. /// <param name="text">input string</param>
  96. /// <param name="enc">Character encoding</param>
  97. /// <returns>SHA1 hash</returns>
  98. private static string ToSHA1Hash(string text, Encoding enc)
  99. {
  100. var hashBytes = SHA1.Create().ComputeHash(enc.GetBytes(text.Trim()));
  101. var hashBuilder = new StringBuilder();
  102. foreach (var hashByte in hashBytes)
  103. hashBuilder.Append(hashByte.ToString("x2", CultureInfo.InvariantCulture));
  104. return hashBuilder.ToString();
  105. }
  106. #endregion
  107. }
  108. }