private static SapoBrokerClient.Encoding.Thrift.Messages.Authentication getAuthMessage(NetMessage message)
        {
            NetAuthentication netAuth = message.Action.AuthenticationMessage;

            SapoBrokerClient.Encoding.Thrift.Messages.Authentication auth = new SapoBrokerClient.Encoding.Thrift.Messages.Authentication();
            auth.Action_id           = netAuth.ActionId;
            auth.Authentication_type = netAuth.AuthenticationType;
            if (netAuth.Roles != null)
            {
                List <string> roles = netAuth.Roles as List <string>;
                if (roles == null)
                {
                    roles = new List <string>(netAuth.Roles);
                }
                auth.Roles = roles;
            }
            auth.Token   = netAuth.Token;
            auth.User_id = netAuth.UserId;

            return(auth);
        }
        public bool Authenticate(ICredentialsProvider provider)
        {
            log.Info("Authenticating");
            if (provider == null)
            {
                throw new ArgumentNullException("AuthenticationInfo can not be null in order to authenticate.");
            }
            AuthenticationInfo authInfoToUse = null;
            try
            {
                authInfoToUse = provider.GetCredentials();
                if (authInfoToUse == null)
                    throw new InvalidCredentialsException("Credential provider returned null");
            }
            catch (InvalidCredentialsException ice)
            {
                log.Error("Failed to obtain credentials.", ice);
                return false;
            }

            // save important information
            lock (this)
            {
                this.provider = provider;
                this.clientAuthInfo = authInfoToUse;
                this.usingAuth = true;
            }

            // build NetMessage

            string actionId = System.Guid.NewGuid().ToString();

            NetAuthentication netAuth = new NetAuthentication(authInfoToUse.Token, authInfoToUse.UserAuthenticationType);
            if ((authInfoToUse.Roles != null) && (authInfoToUse.Roles.Count != 0))
                netAuth.Roles = authInfoToUse.Roles;
            if (authInfoToUse.UserId != null)
            {
                netAuth.UserId = authInfoToUse.UserId;
            }
            netAuth.ActionId = actionId;

            NetAction netAction = new NetAction(NetAction.ActionType.AUTH);
            netAction.AuthenticationMessage = netAuth;
            NetMessage msg = new NetMessage(netAction);

            // build waitable object
            WaitMessageAccepted waitMsgAccepted = new WaitMessageAccepted();
            AcceptRequest acceptRequest = new AcceptRequest(actionId, waitMsgAccepted, 7000);

            //send message
            HandleOutgoingMessage(msg, acceptRequest);

            // wait for response
            lock (waitMsgAccepted.SyncObject)
            {
                Monitor.Wait(waitMsgAccepted.SyncObject);
            }
            if (waitMsgAccepted.WaitResult != WaitMessageAccepted.Result.Accepted)
            {
                log.Error("Authenticatation failed. Reason: " + waitMsgAccepted.WaitResult);
                return false;
            }

            log.Info("Authenticated");

            return true;
        }