예제 #1
0
        private Challenge ProcessStep1(XmppStream stream, Response response, XmppHandlerContext ctx)
        {
            var step     = new Step2(response.TextBase64);
            var userName = step.Username;
            var user     = ctx.UserManager.GetUser(new Jid(userName, stream.Domain, null));

            log.DebugFormat("User {0} {1}. Realm={2}", userName, user == null ? "not found" : user.ToString(), step.Realm);

            if (user != null && string.Compare(stream.Domain, step.Realm, StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (step.Authorize(userName, user.Password))
                {
                    log.DebugFormat("User authorized");
                    lock (authData)
                    {
                        authData[stream.Id].UserName = userName;
                    }
                    var challenge = new Challenge();
                    challenge.TextBase64 = string.Format("rspauth={0}", step.CalculateResponse(userName, user.Password, string.Empty));
                    return(challenge);
                }
                else
                {
                    log.DebugFormat("User not authorized");
                }
            }
            return(null);
        }
예제 #2
0
        public XmppHandlerResult ProcessElement(Response element, XmppSession session, XmppHandlerContext context)
        {
            var authStep = session.AuthData as AuthData;

            if (authStep == null)
            {
                return(Error(session, FailureCondition.temporary_auth_failure));
            }

            if (authStep.Step == AuthStep.Step1)
            {
                var step = new Step2(element.TextBase64);
                var user = context.Storages.Users.GetUser(step.Username);

                if (user != null &&
                    string.Compare(session.Jid.Server, step.Realm, StringComparison.OrdinalIgnoreCase) == 0 &&
                    step.Authorize(step.Username, user.Password))
                {
                    var challenge = new Challenge
                    {
                        TextBase64 = string.Format("rspauth={0}", step.CalculateResponse(step.Username, user.Password, string.Empty))
                    };
                    authStep.DoStep(step.Username);
                    return(Send(session, challenge));
                }
                else
                {
                    return(Error(session, FailureCondition.not_authorized));
                }
            }
            else if (authStep.Step == AuthStep.Step2)
            {
                session.Authenticate(authStep.UserName);
                session.Connection.Reset();
                return(Send(session, new Success()));
            }
            else
            {
                return(Error(session, FailureCondition.temporary_auth_failure));
            }
        }