Example #1
0
        private bool CheckAuthenticationRequest(RemoteClient client, JToken msg)
        {
            if (msg == null || !msg.HasValues)
            {
                Logger.Warn("WifiRemote: Client sent empty authentication String");
                return(false);
            }

            AuthMethod auth = AllowedAuth;

            if (auth == AuthMethod.None)
            {
                // Every auth request is valid for AuthMethod.None
                client.UserId          = null; //Use current client or logged in user
                client.IsAuthenticated = true;
                return(true);
            }

            JObject message = (JObject)msg;

            // For AuthMethod.Both we have to check which method was choosen.
            if (AllowedAuth == AuthMethod.Both)
            {
                if (message["AuthMethod"] == null)
                {
                    Logger.Warn("WifiRemote: Client {0} authentification failed, no AuthMethod submitted", client);
                    return(false);
                }
                else
                {
                    String authString = (string)message["AuthMethod"];
                    if (authString != null)
                    {
                        if (authString.Equals("userpass"))
                        {
                            auth = AuthMethod.UserPassword;
                        }
                        else if (authString.Equals("passcode"))
                        {
                            auth = AuthMethod.Passcode;
                        }
                        else
                        {
                            Logger.Warn("WifiRemote: Client " + client.ToString() + " authentification failed, invalid authMethod '" + authString + "'");
                            return(false);
                        }
                    }
                }
            }

            // Check user credentials
            if (auth == AuthMethod.UserPassword)
            {
                if (message["User"] != null && message["Password"] != null)
                {
                    String user = (string)message["User"];
                    String pass = (string)message["Password"];
                    var    id   = VerifyUser(user, pass);
                    if (id != null)
                    {
                        client.AuthenticatedBy = auth;
                        client.User            = user;
                        client.Password        = pass;
                        client.UserId          = id.Value;
                        client.IsAuthenticated = true;
                        Logger.Debug("WifiRemote: Client " + client.ToString() + " successfully authentificated by username and password");
                        return(true);
                    }
                }
            }
            else if (auth == AuthMethod.Passcode)
            {
                if (message["PassCode"] != null)
                {
                    String pass = (string)message["PassCode"];
                    if (pass.Equals(this.PassCode))
                    {
                        client.AuthenticatedBy = auth;
                        client.PassCode        = pass;
                        client.UserId          = null; //Use current client or logged in user
                        client.IsAuthenticated = true;
                        Logger.Debug("WifiRemote: Client " + client.ToString() + " successfully authentificated by passcode");
                        return(true);
                    }
                }
            }

            Logger.Warn("WifiRemote: Client " + client.ToString() + " authentification failed");
            return(false);
        }
 /// <summary>
 /// Sets the remote client associated with the socket
 /// </summary>
 /// <param name="socket">socket</param>
 /// <param name="client">remote clien</param>
 public static void SetRemoteClient(this AsyncSocket socket, RemoteClient client)
 {
     RemoteClients.TryAdd(socket, client);
 }
Example #3
0
        /// <summary>
        /// Read a message from the client.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        /// <param name="tag"></param>
        private void NewSocket_DidRead(AsyncSocket sender, byte[] data, long tag)
        {
            string msg = null;

            try
            {
                msg = Encoding.UTF8.GetString(data);

                //comment this out to log all received commands
                //Logger.Debug("WifiRemote: " + msg);

                // Get json object
                JObject      message = JObject.Parse(msg);
                string       type    = (string)message["Type"];
                RemoteClient client  = sender.GetRemoteClient();

                // Autologin handling
                // Has to be activated in WifiRemote configuration.
                string clientKey = (string)message["AutologinKey"];

                // Key is set: try to authenticate by AutoLoginKey
                if (clientKey != null && !client.IsAuthenticated)
                {
                    if (AutologinTimeout > 0)
                    {
                        AutoLoginToken token = new AutoLoginToken(clientKey, client);
                        // the client token is in the list
                        foreach (AutoLoginToken aToken in _loginTokens)
                        {
                            if (aToken.Key == token.Key)
                            {
                                // Check if the autologin key was issued within the timeout
                                TimeSpan elapsed = DateTime.Now - aToken.Issued;
                                client.IsAuthenticated = (elapsed.Minutes < AutologinTimeout);
                                client = aToken.Client;

                                // Renew the timeout
                                aToken.Issued = DateTime.Now;
                            }
                        }

                        // MediaPortal was rebooted (will wipe all AutoLoginKeys) or
                        // autologin time out period is over (configurable in settings).
                        //
                        // Tell the client to reauthenticate.
                        if (!client.IsAuthenticated)
                        {
                            Logger.Debug("WifiRemote: AutoLoginToken timed out. Client needs to reauthenticate.");
                            TellClientToReAuthenticate(sender);
                            return;
                        }
                    }
                    else
                    {
                        Logger.Debug("WifiRemote: AutoLogin is disabled but client tried to auto-authenticate.");
                        TellClientToReAuthenticate(sender);
                        return;
                    }
                }

                // The client is already authentificated or we don't need authentification
                if (type != null && client.IsAuthenticated && type != "identify")
                {
                    Func <JObject, SocketServer, AsyncSocket, Task <bool> > function;
                    if (MessageType.TryGetValue(type, out function))
                    {
                        Logger.Debug("WifiRemote: MessageType: {0} got called", type);
                        function.Invoke(message, this, sender);
                    }
                    else
                    {
                        Logger.Warn("WifiRemote: Couldn't get MessageType: {0}", type);
                    }
                }
                else
                {
                    // user is not yet authenticated
                    if (type == "identify")
                    {
                        // Save client name if supplied
                        if (message["Name"] != null)
                        {
                            client.ClientName = (string)message["Name"];
                        }

                        // Save client description if supplied
                        if (message["Description"] != null)
                        {
                            client.ClientDescription = (string)message["Description"];
                        }

                        // Save application name if supplied
                        if (message["Application"] != null)
                        {
                            client.ApplicationName = (string)message["Application"];
                        }

                        // Save application version if supplied
                        if (message["Version"] != null)
                        {
                            client.ApplicationVersion = (string)message["Version"];
                        }

                        // Authentication
                        if (AllowedAuth == AuthMethod.None || CheckAuthenticationRequest(client, message["Authenticate"]))
                        {
                            // User successfully authenticated
                            sender.GetRemoteClient().IsAuthenticated = true;
                            SendAuthenticationResponse(sender, true);
                            SendMessageOverviewInformation.Send(sender);
                        }
                        else
                        {
                            // Client sends a message other then authenticate when not yet
                            // authenticated or authenticate failed
                            SendAuthenticationResponse(sender, false);
                        }
                    }
                    else
                    {
                        // Client needs to authenticate first
                        TellClientToReAuthenticate(sender);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error("WifiRemote: Communication Error", e);
            }

            // Continue listening
            sender.Read(AsyncSocket.CRLFData, -1, 0);
        }
Example #4
0
 public AutoLoginToken(string key, RemoteClient client)
 {
     Key    = key;
     Issued = DateTime.Now;
     Client = client;
 }