31 using System.Reflection;
35 using System.Text.RegularExpressions;
37 using System.Xml.Serialization;
38 using System.Collections.Generic;
39 using OpenSim.Server.Base;
40 using OpenSim.Services.Interfaces;
41 using OpenSim.Framework;
42 using OpenSim.Framework.ServiceAuth;
43 using OpenSim.Framework.Servers.HttpServer;
46 namespace OpenSim.Server.Handlers.Authentication
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 private bool m_AllowGetAuthInfo =
false;
55 private bool m_AllowSetAuthInfo =
false;
56 private bool m_AllowSetPassword =
false;
59 this(service, null, null) {}
62 base(
"POST",
"/auth", auth)
64 m_AuthenticationService = service;
68 m_AllowGetAuthInfo = config.GetBoolean(
"AllowGetAuthInfo", m_AllowGetAuthInfo);
69 m_AllowSetAuthInfo = config.GetBoolean(
"AllowSetAuthInfo", m_AllowSetAuthInfo);
70 m_AllowSetPassword = config.GetBoolean(
"AllowSetPassword", m_AllowSetPassword);
78 string[] p = SplitParams(path);
85 StreamReader sr =
new StreamReader(request);
86 string body = sr.ReadToEnd();
89 return DoPlainMethods(body);
91 byte[] buffer =
new byte[request.Length];
92 long length = request.Length;
95 request.Read(buffer, 0, (int)length);
97 return DoEncryptedMethods(buffer);
103 private byte[] DoPlainMethods(
string body)
105 Dictionary<string, object> request =
106 ServerUtils.ParseQueryString(body);
110 if (request.ContainsKey(
"LIFETIME"))
112 lifetime = Convert.ToInt32(request[
"LIFETIME"].ToString());
117 if (!request.ContainsKey(
"METHOD"))
118 return FailureResult();
119 if (!request.ContainsKey(
"PRINCIPAL"))
120 return FailureResult();
122 string method = request[
"METHOD"].ToString();
127 if (!UUID.TryParse(request[
"PRINCIPAL"].ToString(), out principalID))
128 return FailureResult();
133 if (!request.ContainsKey(
"PASSWORD"))
134 return FailureResult();
136 token = m_AuthenticationService.Authenticate(principalID, request[
"PASSWORD"].ToString(), lifetime);
138 if (token != String.Empty)
139 return SuccessResult(token);
140 return FailureResult();
143 if (!m_AllowSetPassword)
144 return FailureResult();
146 if (!request.ContainsKey(
"PASSWORD"))
147 return FailureResult();
149 if (m_AuthenticationService.SetPassword(principalID, request[
"PASSWORD"].ToString()))
150 return SuccessResult();
152 return FailureResult();
155 if (!request.ContainsKey(
"TOKEN"))
156 return FailureResult();
158 if (m_AuthenticationService.Verify(principalID, request[
"TOKEN"].ToString(), lifetime))
159 return SuccessResult();
161 return FailureResult();
164 if (!request.ContainsKey(
"TOKEN"))
165 return FailureResult();
167 if (m_AuthenticationService.Release(principalID, request[
"TOKEN"].ToString()))
168 return SuccessResult();
170 return FailureResult();
173 if (m_AllowGetAuthInfo)
174 return GetAuthInfo(principalID);
179 if (m_AllowSetAuthInfo)
180 return SetAuthInfo(principalID, request);
185 return FailureResult();
188 private byte[] DoEncryptedMethods(byte[] ciphertext)
193 private byte[] SuccessResult()
195 XmlDocument doc =
new XmlDocument();
197 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
200 doc.AppendChild(xmlnode);
202 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
205 doc.AppendChild(rootElement);
207 XmlElement result = doc.CreateElement(
"",
"Result",
"");
208 result.AppendChild(doc.CreateTextNode(
"Success"));
210 rootElement.AppendChild(result);
212 return Util.DocToBytes(doc);
215 byte[] GetAuthInfo(UUID principalID)
217 AuthInfo info = m_AuthenticationService.GetAuthInfo(principalID);
221 Dictionary<string, object> result =
new Dictionary<string, object>();
222 result[
"result"] = info.ToKeyValuePairs();
224 return ResultToBytes(result);
228 return FailureResult();
232 byte[] SetAuthInfo(UUID principalID, Dictionary<string, object> request)
234 AuthInfo existingInfo = m_AuthenticationService.GetAuthInfo(principalID);
236 if (existingInfo == null)
237 return FailureResult();
239 if (request.ContainsKey(
"AccountType"))
240 existingInfo.
AccountType = request[
"AccountType"].ToString();
242 if (request.ContainsKey(
"PasswordHash"))
243 existingInfo.
PasswordHash = request[
"PasswordHash"].ToString();
245 if (request.ContainsKey(
"PasswordSalt"))
246 existingInfo.
PasswordSalt = request[
"PasswordSalt"].ToString();
248 if (request.ContainsKey(
"WebLoginKey"))
249 existingInfo.
WebLoginKey = request[
"WebLoginKey"].ToString();
251 if (!m_AuthenticationService.SetAuthInfo(existingInfo))
254 "[AUTHENTICATION SERVER POST HANDLER]: Authentication info store failed for account {0} {1} {2}",
255 existingInfo.PrincipalID);
257 return FailureResult();
260 return SuccessResult();
263 private byte[] FailureResult()
265 XmlDocument doc =
new XmlDocument();
267 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
270 doc.AppendChild(xmlnode);
272 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
275 doc.AppendChild(rootElement);
277 XmlElement result = doc.CreateElement(
"",
"Result",
"");
278 result.AppendChild(doc.CreateTextNode(
"Failure"));
280 rootElement.AppendChild(result);
282 return Util.DocToBytes(doc);
285 private byte[] SuccessResult(
string token)
287 XmlDocument doc =
new XmlDocument();
289 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
292 doc.AppendChild(xmlnode);
294 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
297 doc.AppendChild(rootElement);
299 XmlElement result = doc.CreateElement(
"",
"Result",
"");
300 result.AppendChild(doc.CreateTextNode(
"Success"));
302 rootElement.AppendChild(result);
304 XmlElement t = doc.CreateElement(
"",
"Token",
"");
305 t.AppendChild(doc.CreateTextNode(token));
307 rootElement.AppendChild(t);
309 return Util.DocToBytes(doc);
312 private byte[] ResultToBytes(Dictionary<string, object> result)
314 string xmlString = ServerUtils.BuildXmlResponse(result);
315 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
AuthenticationServerPostHandler(IAuthenticationService service, IConfig config, IServiceAuth auth)
override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Base streamed request handler.
AuthenticationServerPostHandler(IAuthenticationService service)