private ParentScope CreateParentScope(IFileContainer javascriptContainer) { List<string> scriptsToEval = new List<string>(); List<string> requestedScripts = new List<string>(new string[] { "/API/AJAX_serverside.js", "/API/json2.js" }); string fileType = null; StringBuilder javascriptBuilder = new StringBuilder(); foreach (string line in javascriptContainer.CastFileHandler<ITextHandler>().ReadLines()) { // find file type if (line.Trim().StartsWith("// FileType:")) fileType = line.Substring(12).Trim(); // Find dependant scripts else if (line.Trim().StartsWith("// Scripts:")) foreach (string script in line.Substring(11).Split(',')) requestedScripts.Add(script.Trim()); else javascriptBuilder.AppendFormat("{0}\n", line); } string javascript = javascriptBuilder.ToString(); Dictionary<string, MethodInfo> functionsInScope = new Dictionary<string, MethodInfo>(); List<KeyValuePair<IFileContainer, DateTime>> loadedScriptsModifiedTimes = new List<KeyValuePair<IFileContainer, DateTime>>(); ISession ownerSession = FileHandlerFactoryLocator.SessionManagerHandler.CreateSession(); try { ownerSession.Login(javascriptContainer.Owner); IWebConnection ownerWebConnection = new BlockingShellWebConnection( FileHandlerFactoryLocator.WebServer, ownerSession, javascriptContainer.FullPath, null, null, new CookiesFromBrowser(), CallingFrom.Web, WebMethod.GET); IEnumerable<ScriptAndMD5> dependantScriptsAndMD5s = FileHandlerFactoryLocator.WebServer.WebComponentResolver.DetermineDependantScripts( requestedScripts, ownerWebConnection); // Load static methods that are passed into the Javascript environment as-is foreach (Type javascriptFunctionsType in GetTypesThatHaveJavascriptFunctions(fileType)) foreach (MethodInfo method in javascriptFunctionsType.GetMethods(BindingFlags.Static | BindingFlags.Public)) functionsInScope[method.Name] = method; // Load all dependant scripts foreach (ScriptAndMD5 dependantScript in dependantScriptsAndMD5s) { string scriptName = dependantScript.ScriptName; if (scriptName.Contains("?")) scriptsToEval.Add(ownerWebConnection.ShellTo(scriptName).ResultsAsString); else { IFileContainer scriptFileContainer = FileHandlerFactoryLocator.FileSystemResolver.ResolveFile(scriptName); if (scriptFileContainer.FileHandler is ITextHandler) { loadedScriptsModifiedTimes.Add(new KeyValuePair<IFileContainer, DateTime>(scriptFileContainer, scriptFileContainer.LastModified)); scriptsToEval.Add(scriptFileContainer.CastFileHandler<ITextHandler>().ReadAll()); } else scriptsToEval.Add(ownerWebConnection.ShellTo(scriptName).ResultsAsString); } } loadedScriptsModifiedTimes.Add(new KeyValuePair<IFileContainer, DateTime>(javascriptContainer, javascriptContainer.LastModified)); // Construct Javascript to shell to the "base" webHandler HashSet<Type> webHandlerTypes = new HashSet<Type>(FileHandlerFactoryLocator.WebHandlerPlugins); if (null != fileType) webHandlerTypes.Add(FileHandlerFactoryLocator.WebHandlerClasses[fileType]); string baseWrapper = GetJavascriptWrapperForBase("base", webHandlerTypes); scriptsToEval.Add(baseWrapper); scriptsToEval.Add(javascript); scriptsToEval.Add("if (this.options) options; else null;"); ParentScope parentScope = new ParentScope(loadedScriptsModifiedTimes, functionsInScope); Dictionary<string, object> data = new Dictionary<string, object>(); data["Scripts"] = scriptsToEval; data["Functions"] = functionsInScope.Keys; _SubProcess.CreateParentScope(parentScope.ParentScopeId, Thread.CurrentThread.ManagedThreadId, data); return parentScope; } finally { FileHandlerFactoryLocator.SessionManagerHandler.EndSession(ownerSession.SessionId); } }
/// <summary> /// Establishes trust between a user and another server /// </summary> /// <param name="sender"></param> /// <param name="receiveNotificationEndpoint"></param> /// <param name="callback"></param> private void BeginEstablishTrust( IUserOrGroup sender, string receiveNotificationEndpoint, string establishTrustEndpoint, Action<string> callback, Action<Exception> errorCallback) { // Make sure the timer is created if (null == EstablishTrustDataTimer) { Timer timer = new Timer(EstablishTrustDataCleanup, null, 600000, 600000); if (null != Interlocked.CompareExchange<Timer>(ref EstablishTrustDataTimer, timer, null)) timer.Dispose(); } // Get the avatar byte[] avatar; ISession session = FileHandlerFactoryLocator.SessionManagerHandler.CreateSession(); try { IWebConnection webConnection = new BlockingShellWebConnection( FileHandlerFactoryLocator.WebServer, session, "/Users/" + sender.Name + ".user?Method=GetAvatar", null, null, null, CallingFrom.Web, WebMethod.GET); IWebResults webResults = webConnection.ShellTo("/Users/" + sender.Name + ".user?Method=GetAvatar"); using (Stream stream = webResults.ResultsAsStream) { avatar = new byte[stream.Length]; stream.Read(avatar, 0, avatar.Length); } } finally { FileHandlerFactoryLocator.SessionManagerHandler.EndSession(session.SessionId); } // Hold on to callback information for use after trust is established string token; using (TimedLock.Lock(EstablishTrustDatasByToken)) { do token = Convert.ToBase64String(SRandom.NextBytes(100)); while (EstablishTrustDatasByToken.ContainsKey(token)); EstablishTrustDatasByToken[token] = new EstablishTrustData(); } HttpWebClient httpWebClient = new HttpWebClient(); httpWebClient.BeginPost( establishTrustEndpoint, delegate(HttpResponseHandler httpResponseHandler) { if (httpResponseHandler.StatusCode == System.Net.HttpStatusCode.Created) { string senderToken; using (TimedLock.Lock(EstablishTrustDatasByToken)) { senderToken = EstablishTrustDatasByToken[token].SenderToken; EstablishTrustDatasByToken.Remove(token); } this.persistedUserManagerData.Write(userManagerData => { var user = userManagerData.GetUser(sender.Id); string oldSenderToken; if (user.receiveNotificationSenderTokensByEndpoint.TryGetValue(receiveNotificationEndpoint, out oldSenderToken)) user.receiveNotificationEndpointsBySenderToken.Remove(oldSenderToken); user.receiveNotificationEndpointsBySenderToken[senderToken] = receiveNotificationEndpoint; user.receiveNotificationSenderTokensByEndpoint[receiveNotificationEndpoint] = senderToken; }); callback(senderToken); } else errorCallback(new ParticleException.CouldNotEstablishTrust("Couldn't establish trust: " + httpResponseHandler.AsString())); }, errorCallback, new KeyValuePair<string, string>("senderIdentity", sender.Identity), new KeyValuePair<string, string>("token", token), new KeyValuePair<string, string>("avatar", Convert.ToBase64String(avatar)), new KeyValuePair<string, string>("loginURL", string.Format("http://{0}/Users/UserDB?Method=OpenIDLogin", FileHandlerFactoryLocator.HostnameAndPort)), new KeyValuePair<string, string>("loginURLOpenID", "openid_url"), new KeyValuePair<string, string>("loginURLWebFinger", "openid_url"), new KeyValuePair<string, string>("loginURLRedirect", "redirect"), GenerateSecurityTimestamp()); }