public void HandleMessage(Session session, BaseMessage message) { var pObj = message.Payload.ToObject <RegisterRequestPayload>(); var authProvider = _pluginHost.GetAuthProvider(); try { authProvider.CreateUser(pObj.Username, pObj.Password); } catch (Exception e) { BaseMessage errorReply; if (e.Message.Contains("E11000")) { errorReply = OtherUtils.GenerateProtocolError( message, "id_exists", "Username already taken", new Dictionary <string, object>() ); } else { errorReply = OtherUtils.GenerateProtocolError( message, "other", e.ToString(), new Dictionary <string, object>() ); } session.ConnectionHandler.SendMessage(errorReply); return; } BaseMessage reply = new BaseMessage(message, true); var p = new RegisterResponsePayload(); p.UserID = $"@{pObj.Username}@{_pluginHost.GetServerID()}"; reply.Payload = p.ToDictionary(); reply.Ok = true; session.ConnectionHandler.SendMessage(reply); }
public void HandleMessage(Session session, BaseMessage message) { var pObj = message.Payload.ToObject <AuthorizationRequest>(); var authProvider = _pluginHost.GetAuthProvider(); if (!authProvider.GetAuthSupportedMethods().Contains(pObj.Type)) { var reply = OtherUtils.GenerateProtocolError( message, invalidAuthType, "auth type is invalid" ); session.ConnectionHandler.SendMessage(reply); return; } var authData = authProvider.TestAuthFields(pObj.Fields); if (authData.Item1 != null) { BaseMessage reply = new BaseMessage(message, true); if (authData.Item2 != null) { reply.Payload = authData.Item2.ToDictionary(); } reply.Ok = true; session.ConnectionHandler.SendMessage(reply); session.AuthData = authData.Item1; } else { var reply = OtherUtils.GenerateProtocolError( message, errID, "auth credentials isn't valid" ); session.ConnectionHandler.SendMessage(reply); } }