void APIConnectionHandler::handleGetCommand(QLocalSocket *sock)
{
    odb::database *db = DB::getDB();
    Application connectedApp = getConnectedApp(sock);
    sock->waitForReadyRead();
    int passwordId = sock->readLine().trimmed().toInt();
    ApplicationPasswordResult passwordResult (db->query<ApplicationPassword>
                                              (ApplicationPasswordQuery::app == connectedApp.getAppId() &&
                                               ApplicationPasswordQuery::passwordID == passwordId));

    if ( passwordResult.size() != 0 )
    {

        bool allow = startMessageViewerAndGetResult("an application want's to get a password, allow or reject?");

        if ( allow )
        {
            ApplicationPassword applicationPassword(*passwordResult.begin());

            sock->write(QVariant(applicationPassword.getUsername().get().c_str()).toByteArray());
            sock->write("\n");
            std::vector<unsigned char> dbEncryptedPassword = applicationPassword.getPassword();
            string encryptedPassword(dbEncryptedPassword.begin(),  dbEncryptedPassword.end());
            string decryptedPassword = enc->decrypt(encryptedPassword);
            sock->write(QVariant(decryptedPassword.c_str()).toByteArray());
            sock->write("\n");
            sock->flush();
            sock->close();
            return;
        }
    }
    sock->write("\n\n");
    sock->flush();
    sock->close();
}
VncServerClient::AuthState ServerAuthenticationManager::performLogonAuthentication( VncServerClient* client,
																					VariantArrayMessage& message )
{
	switch( client->authState() )
	{
	case VncServerClient::AuthInit:
	{
		CryptoCore::PrivateKey privateKey = CryptoCore::KeyGenerator().createRSA( CryptoCore::RsaKeySize );

		client->setPrivateKey( privateKey.toPEM() );

		CryptoCore::PublicKey publicKey = privateKey.toPublicKey();

		if( VariantArrayMessage( message.ioDevice() ).write( publicKey.toPEM() ).send() )
		{
			return VncServerClient::AuthPassword;
		}
		else
		{
			qDebug( "ServerAuthenticationManager::performLogonAuthentication(): failed to send public key" );
			return VncServerClient::AuthFinishedFail;
		}
	}

	case VncServerClient::AuthPassword:
	{
		CryptoCore::PrivateKey privateKey = CryptoCore::PrivateKey::fromPEM( client->privateKey() );

		CryptoCore::SecureArray encryptedPassword( message.read().toByteArray() );

		CryptoCore::SecureArray decryptedPassword;

		if( privateKey.decrypt( encryptedPassword,
								&decryptedPassword,
								CryptoCore::DefaultEncryptionAlgorithm ) == false )
		{
			qWarning( "ServerAuthenticationManager::performLogonAuthentication(): failed to decrypt password" );
			return VncServerClient::AuthFinishedFail;
		}

		AuthenticationCredentials credentials;
		credentials.setLogonUsername( client->username() );
		credentials.setLogonPassword( QString::fromUtf8( decryptedPassword.toByteArray() ) );

		if( LogonAuthentication::authenticateUser( credentials ) )
		{
			qDebug( "ServerAuthenticationManager::performLogonAuthentication(): SUCCESS" );
			return VncServerClient::AuthFinishedSuccess;
		}

		qDebug( "ServerAuthenticationManager::performLogonAuthentication(): FAIL" );
		return VncServerClient::AuthFinishedFail;
	}

	default:
		break;
	}

	return VncServerClient::AuthFinishedFail;
}