Parser.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace FastReport.Cloud.OAuth
  6. {
  7. /// <summary>
  8. /// Represents parser for parse OAuth responses.
  9. /// </summary>
  10. public static class Parser
  11. {
  12. #region Private Methods
  13. private static Dictionary<string, string> ParseParameters(Stream stream)
  14. {
  15. Dictionary<string, string> parsedParameters = new Dictionary<string, string>();
  16. StreamReader reader = new StreamReader(stream);
  17. string data = reader.ReadToEnd();
  18. if (!String.IsNullOrEmpty(data))
  19. {
  20. string[] parameters = data.Split('&');
  21. foreach (string param in parameters)
  22. {
  23. string[] pair = param.Split('=');
  24. parsedParameters.Add(pair[0], pair[1]);
  25. }
  26. }
  27. return parsedParameters;
  28. }
  29. private static string ClearJsonString(string str)
  30. {
  31. str = str.Trim();
  32. int firstIndex = str.IndexOf("\"");
  33. int lastIndex = str.LastIndexOf("\"");
  34. if (firstIndex != -1 && lastIndex != -1)
  35. {
  36. str = str.Substring(firstIndex + 1, lastIndex - firstIndex - 1);
  37. }
  38. return str;
  39. }
  40. private static Dictionary<string, string> ParseJsonParameters(Stream stream)
  41. {
  42. Dictionary<string, string> parsedParameters = new Dictionary<string, string>();
  43. StreamReader reader = new StreamReader(stream);
  44. string data = reader.ReadToEnd();
  45. if (!String.IsNullOrEmpty(data))
  46. {
  47. string[] lines = data.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  48. for (int i = 0; i < lines.Length; i++)
  49. {
  50. string line = lines[i];
  51. int index = line.IndexOf('{');
  52. if (index > -1)
  53. {
  54. line = line.Remove(index, 1);
  55. }
  56. index = line.IndexOf('}');
  57. if (index > -1)
  58. {
  59. line = line.Remove(index, 1);
  60. }
  61. string[] pairs = line.Split(',');
  62. for (int j = 0; j < pairs.Length; j++)
  63. {
  64. string[] pair = pairs[j].Split(':');
  65. if (pair.Length == 2)
  66. {
  67. string key = ClearJsonString(pair[0]);
  68. string value = ClearJsonString(pair[1]);
  69. parsedParameters.Add(key, value);
  70. }
  71. }
  72. }
  73. }
  74. return parsedParameters;
  75. }
  76. #endregion // Private Methods
  77. #region Public Methods
  78. /// <summary>
  79. /// Parses token information in stream.
  80. /// </summary>
  81. /// <param name="stream">The stream for parse.</param>
  82. /// <returns>The OAuth token.</returns>
  83. public static Token ParseToken(Stream stream)
  84. {
  85. Dictionary<string, string> parameters = ParseParameters(stream);
  86. return new Token(parameters["oauth_token"], parameters["oauth_token_secret"]);
  87. }
  88. /// <summary>
  89. /// Parses token information in stream for SkyDrive.
  90. /// </summary>
  91. /// <param name="stream">The stream for parse.</param>
  92. /// <returns>The SkyDrive access token.</returns>
  93. public static string ParseSkyDriveToken(Stream stream)
  94. {
  95. Dictionary<string, string> parameters = ParseJsonParameters(stream);
  96. return Uri.UnescapeDataString(parameters["access_token"]);
  97. }
  98. /// <summary>
  99. /// Parses token information in stream for Google Drive.
  100. /// </summary>
  101. /// <param name="stream">The stream for parse.</param>
  102. /// <returns>The Google Drive access token.</returns>
  103. public static string ParseGoogleDriveToken(Stream stream)
  104. {
  105. Dictionary<string, string> parameters = ParseJsonParameters(stream);
  106. return Uri.UnescapeDataString(parameters["access_token"]);
  107. }
  108. /// <summary>
  109. /// Parses token information in stream for FastCloud.
  110. /// </summary>
  111. /// <param name="stream">The stream for parse.</param>
  112. /// <returns>The FastCloud access token.</returns>
  113. public static string ParseFastCloudToken(Stream stream)
  114. {
  115. Dictionary<string, string> parameters = ParseJsonParameters(stream);
  116. return Uri.UnescapeDataString(parameters["token"]);
  117. }
  118. #endregion // Public Methods
  119. }
  120. }