public WebAccessRequestResponse GetAccessRequestStatus(string token) { WebAccessRequestResponse response = new WebAccessRequestResponse(); if (accessRequestDialogs[token] != null) { response.UserHasResponded = accessRequestDialogs[token].UserHasResponded; response.Username = accessRequestDialogs[token].SelectedUser; response.IsAllowed = accessRequestDialogs[token].SelectedUser != null; } return(response); }
private String RequestAccessThroughPrivateUSS(string token, string client, string ip, CancellationTokenSource cancelToken) { IPrivateUserSessionService channel = null; try { var factory = new ClientFactory <IPrivateUserSessionService>(); channel = factory.CreateLocalTcpConnection(9751, "MPExtended/UserSessionServicePrivate"); // request access bool result = channel.RequestAccess(token, client, ip, Configuration.Authentication.Users.Select(x => x.Username).ToList()); String selectedUser = null; if (result) { while (!cancelToken.IsCancellationRequested) { WebAccessRequestResponse response = channel.GetAccessRequestStatus(token); if (response.UserHasResponded) { selectedUser = response.IsAllowed ? response.Username : null; break; } Thread.Sleep(500); } } if (cancelToken.IsCancellationRequested) { channel.CancelAccessRequest(token); } // close channel (channel as ICommunicationObject).Close(); return(selectedUser); } catch (Exception ex) { Log.Error(String.Format("Failed to request access for client {0} at {1} through USS", client, ip), ex); return(ERROR); } }
public WebAccessRequestResponse CreateAccessRequest(string clientName) { // generate the request string ip = WCFUtil.GetClientIPAddress(); string token = RandomGenerator.GetRandomString(40); WebAccessRequestResponse request = new WebAccessRequestResponse() { ClientName = clientName, IsAllowed = false, UserHasResponded = false, Token = token }; if (!Configuration.Services.AccessRequestEnabled) { Log.Info("User access request from {0} (claims to be client {1}) denied because user access requests are disabled on this system (check configuration)", ip, clientName); request.UserHasResponded = true; request.Token = null; return(request); } requests[token] = request; Log.Info("Received access request from {0} (claims to be client {1}), gave it token {2}", ip, clientName, token); CancellationTokenSource cancelToken = new CancellationTokenSource(); cancelTokens[token] = cancelToken; // ask the user askUserTasks[token] = Task.Factory.StartNew(delegate() { String result = null; if (Mediaportal.IsMediaPortalRunning() && WifiRemote.IsInstalled) { Log.Debug("Requesting user response through WifiRemote"); result = RequestAccessThroughWifiRemote(clientName, ip, cancelToken); } else { Log.Debug("Requesting user response through USS"); result = RequestAccessThroughPrivateUSS(token, clientName, ip, cancelToken); } Log.Debug("Finish asking user about access request with token {0}: {1}", token, result); // make sure that the request is still active (not cancelled) if (requests.ContainsKey(token)) { lock (requests[token]) { // set the necessary flags var matchingUsers = Configuration.Authentication.Users.Where(x => x.Username == result); requests[token].ErrorDuringProcessing = result == ERROR || !matchingUsers.Any(); requests[token].IsAllowed = !requests[token].ErrorDuringProcessing && result != null; requests[token].UserHasResponded = true; if (matchingUsers.Any()) { Log.Info("Sending account {0} in response to access request {1}", matchingUsers.First().Username, token); requests[token].Username = matchingUsers.First().Username; requests[token].Password = matchingUsers.First().GetPassword(); } else if (result == ERROR) { Log.Error("Failure during access request for token {0}", token); } else if (result != null) { Log.Warn("Didn't find a user named '{0}' - something strange is going on!", result); } } } return(true); }, cancelToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); // return the token to the client return(request); }