AuthClient.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using Nwc.XmlRpc;
  31. using OpenMetaverse;
  32. namespace OpenSim.Framework.Communications.Clients
  33. {
  34. public class AuthClient
  35. {
  36. public static string GetNewKey(string authurl, UUID userID, UUID authToken)
  37. {
  38. //Hashtable keyParams = new Hashtable();
  39. //keyParams["user_id"] = userID;
  40. //keyParams["auth_token"] = authKey;
  41. List<string> SendParams = new List<string>();
  42. SendParams.Add(userID.ToString());
  43. SendParams.Add(authToken.ToString());
  44. XmlRpcRequest request = new XmlRpcRequest("hg_new_auth_key", SendParams);
  45. XmlRpcResponse reply;
  46. try
  47. {
  48. reply = request.Send(authurl, 6000);
  49. }
  50. catch (Exception e)
  51. {
  52. System.Console.WriteLine("[HGrid]: Failed to get new key. Reason: " + e.Message);
  53. return string.Empty;
  54. }
  55. if (!reply.IsFault)
  56. {
  57. string newKey = string.Empty;
  58. if (reply.Value != null)
  59. newKey = (string)reply.Value;
  60. return newKey;
  61. }
  62. else
  63. {
  64. System.Console.WriteLine("[HGrid]: XmlRpc request to get auth key failed with message {0}" + reply.FaultString + ", code " + reply.FaultCode);
  65. return string.Empty;
  66. }
  67. }
  68. public static bool VerifyKey(string authurl, UUID userID, string authKey)
  69. {
  70. List<string> SendParams = new List<string>();
  71. SendParams.Add(userID.ToString());
  72. SendParams.Add(authKey);
  73. System.Console.WriteLine("[HGrid]: Verifying user key with authority " + authurl);
  74. XmlRpcRequest request = new XmlRpcRequest("hg_verify_auth_key", SendParams);
  75. XmlRpcResponse reply;
  76. try
  77. {
  78. reply = request.Send(authurl, 10000);
  79. }
  80. catch (Exception e)
  81. {
  82. System.Console.WriteLine("[HGrid]: Failed to verify key. Reason: " + e.Message);
  83. return false;
  84. }
  85. if (reply != null)
  86. {
  87. if (!reply.IsFault)
  88. {
  89. bool success = false;
  90. if (reply.Value != null)
  91. success = (bool)reply.Value;
  92. return success;
  93. }
  94. else
  95. {
  96. System.Console.WriteLine("[HGrid]: XmlRpc request to verify key failed with message {0}" + reply.FaultString + ", code " + reply.FaultCode);
  97. return false;
  98. }
  99. }
  100. else
  101. {
  102. System.Console.WriteLine("[HGrid]: XmlRpc request to verify key returned null reply");
  103. return false;
  104. }
  105. }
  106. public static bool VerifySession(string authurl, UUID userID, UUID sessionID)
  107. {
  108. Hashtable requestData = new Hashtable();
  109. requestData["avatar_uuid"] = userID.ToString();
  110. requestData["session_id"] = sessionID.ToString();
  111. ArrayList SendParams = new ArrayList();
  112. SendParams.Add(requestData);
  113. XmlRpcRequest UserReq = new XmlRpcRequest("check_auth_session", SendParams);
  114. XmlRpcResponse UserResp = null;
  115. try
  116. {
  117. UserResp = UserReq.Send(authurl, 3000);
  118. }
  119. catch (Exception e)
  120. {
  121. System.Console.WriteLine("[Session Auth]: VerifySession XmlRpc: " + e.Message);
  122. return false;
  123. }
  124. Hashtable responseData = (Hashtable)UserResp.Value;
  125. if (responseData.ContainsKey("auth_session") && responseData["auth_session"].ToString() == "TRUE")
  126. {
  127. //System.Console.WriteLine("[Authorization]: userserver reported authorized session for user " + userID);
  128. return true;
  129. }
  130. else
  131. {
  132. //System.Console.WriteLine("[Authorization]: userserver reported unauthorized session for user " + userID);
  133. return false;
  134. }
  135. }
  136. }
  137. }