private void ProcessAuthIQSet(XmppStream stream, AuthIq iq, XmppHandlerContext context)
		{
			if (string.IsNullOrEmpty(iq.Query.Username) || string.IsNullOrEmpty(iq.Query.Resource))
			{
				context.Sender.SendTo(stream, XmppStanzaError.ToNotAcceptable(iq));
				return;
			}

			bool authorized = false;
			if (!string.IsNullOrEmpty(iq.Query.Digest))
			{
				authorized = AuthDigest(iq.Query.Username, iq.Query.Digest, stream, context.UserManager);
			}
			if (!string.IsNullOrEmpty(iq.Query.Password))
			{
				authorized = AuthPlain(iq.Query.Username, iq.Query.Password, stream, context.UserManager);
			}
			if (authorized)
			{
				stream.Authenticate(iq.Query.Username);

				var answer = new IQ(IqType.result);
				answer.Id = iq.Id;
				answer.To = iq.From;
				answer.From = iq.To;
				context.Sender.SendTo(stream, answer);
			}
			else
			{
				context.Sender.SendTo(stream, XmppStanzaError.ToNotAuthorized(iq));
			}
		}
Beispiel #2
0
 public XmppStream GetOrCreateNewStream(string connectionId)
 {
     if (string.IsNullOrEmpty(connectionId))
     {
         throw new ArgumentNullException("connectionId");
     }
     try
     {
         locker.EnterWriteLock();
         var stream = new XmppStream(connectionId);
         if (streams.ContainsKey(connectionId))
         {
             var oldStream = streams[connectionId];
             if (oldStream.Authenticated)
             {
                 stream.Authenticate(oldStream.User);
             }
         }
         streams[connectionId] = stream;
         return(stream);
     }
     finally
     {
         locker.ExitWriteLock();
     }
 }
		public override void ElementHandle(XmppStream stream, Element element, XmppHandlerContext context)
		{
			if (stream.Authenticated) return;

			var user = context.AuthManager.RestoreUserToken(((TMToken)element).Value);
			if (!string.IsNullOrEmpty(user))
			{
				stream.Authenticate(user);
				context.Sender.ResetStream(stream);
				context.Sender.SendTo(stream, new Success());
			}
			else
			{
				context.Sender.SendToAndClose(stream, XmppFailureError.NotAuthorized);
			}
		}
Beispiel #4
0
        public XmppStream GetOrCreateNewStream(string connectionId)
        {
            if (string.IsNullOrEmpty(connectionId))
            {
                throw new ArgumentNullException("connectionId");
            }

            var stream = new XmppStream(connectionId);

            streams.AddOrUpdate(connectionId, stream, (id, old) =>
            {
                if (old.Authenticated)
                {
                    stream.Authenticate(old.User);
                }
                return(stream);
            });
            return(stream);
        }
		public XmppStream GetOrCreateNewStream(string connectionId)
		{
			if (string.IsNullOrEmpty(connectionId)) throw new ArgumentNullException("connectionId");
			try
			{
                locker.EnterWriteLock();
				var stream = new XmppStream(connectionId);
				if (streams.ContainsKey(connectionId))
				{
					var oldStream = streams[connectionId];
					if (oldStream.Authenticated) stream.Authenticate(oldStream.User);
				}
				streams[connectionId] = stream;
				return stream;
			}
			finally
			{
				locker.ExitWriteLock();
			}
		}
 private Success ProcessStep2(XmppStream stream, Response response, XmppHandlerContext ctx)
 {
     lock (authData)
     {
         stream.Authenticate(authData[stream.Id].UserName);
         authData.Remove(stream.Id);
     }
     ctx.Sender.ResetStream(stream);
     return new Success();
 }
Beispiel #7
0
        private void ProcessResponse(XmppStream stream, Response response, XmppHandlerContext context)
        {
            AuthData authStep;
            lock (authData)
            {
                authData.TryGetValue(stream.Id, out authStep);
            }

            if (authStep == null)
            {
                context.Sender.SendToAndClose(stream, XmppFailureError.TemporaryAuthFailure);
                return;
            }
            if (!authStep.IsPlain)
            {
                if (authStep.Step == AuthStep.Step1)
                {
                    var challenge = ProcessStep1(stream, response, context);
                    if (challenge != null)
                    {
                        context.Sender.SendTo(stream, challenge);
                        authStep.DoStep();
                    }
                    else
                    {
                        context.Sender.SendToAndClose(stream, XmppFailureError.NotAuthorized);
                    }
                }
                else if (authStep.Step == AuthStep.Step2)
                {
                    var success = ProcessStep2(stream, response, context);
                    context.Sender.SendTo(stream, success);
                }
                else
                {
                    context.Sender.SendToAndClose(stream, XmppFailureError.TemporaryAuthFailure);
                }
            }
            else
            {
                if (authStep.IsAuth)
                {
                    lock (authData)
                    {
                        stream.Authenticate(authData[stream.Id].UserName);
                        authData.Remove(stream.Id);
                    }
                    log.DebugFormat("User authorized");
                    context.Sender.ResetStream(stream);
                    context.Sender.SendTo(stream, new Success());
                }
                else
                {
                    log.DebugFormat("User not authorized");
                    context.Sender.SendTo(stream, new Failure(FailureCondition.not_authorized));
                }
            }
        }