Example #1
0
        public void AuthServiceMsgs_Msg_AuthMsgAndAck()
        {
            EnhancedStream       es = new EnhancedMemoryStream();
            AuthMsg              authMsgIn, authMsgOut;
            AuthAck              authAckIn, authAckOut;
            string               rsaKey = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
            SymmetricKey         saClient, saServer;
            string               r, a, p;
            AuthenticationResult authResult;

            authMsgOut = new AuthMsg(AuthMsg.EncryptCredentials(rsaKey, "realm", "account", "password", out saClient));

            Msg.Save(es, authMsgOut);
            es.Position = 0;
            authMsgIn   = (AuthMsg)Msg.Load(es);

            AuthMsg.DecryptCredentials(rsaKey, authMsgIn.EncryptedCredentials, out r, out a, out p, out saServer);

            Assert.AreEqual("realm", r);
            Assert.AreEqual("account", a);
            Assert.AreEqual("password", p);

            authAckOut = new AuthAck(AuthAck.EncryptResult(saServer, new AuthenticationResult(AuthenticationStatus.Authenticated, "Test", TimeSpan.FromMinutes(25))));

            es.SetLength(0);
            Msg.Save(es, authAckOut);
            es.Position = 0;
            authAckIn   = (AuthAck)Msg.Load(es);

            authResult = AuthAck.DecryptResult(saClient, authAckIn.EncryptedResult);
        }
Example #2
0
        /// <summary>
        /// Handles the async reception of authentication responses.
        /// </summary>
        /// <param name="ar">The async result instance.</param>
        private void OnAuth(IAsyncResult ar)
        {
            var     arAuth = (AuthAsyncResult)ar.AsyncState;
            AuthAck ack;

            Assertion.Test(arAuth.OpState == AuthOpState.AuthPending);

            using (TimedLock.Lock(this))
            {
                try
                {
                    if (!isOpen)
                    {
                        throw new AuthenticationException(NotOpenMsg);
                    }

                    ack           = (AuthAck)router.EndQuery(ar);
                    arAuth.Result = AuthAck.DecryptResult(arAuth.SymmetricKey, ack.EncryptedResult);

                    arAuth.Notify();

                    // Cache the result

                    if (cache != null)
                    {
                        if (arAuth.Result.Status == AuthenticationStatus.Authenticated)
                        {
                            if (successTTL > TimeSpan.Zero)
                            {
                                cache.Add(GetCacheKey(arAuth.Realm, arAuth.Account, arAuth.Password), arAuth.Result, successTTL);
                            }
                        }
                        else
                        {
                            if (failTTL > TimeSpan.Zero)
                            {
                                cache.Add(GetCacheKey(arAuth.Realm, arAuth.Account, arAuth.Password), arAuth.Result, failTTL);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    arAuth.Notify(e);
                }
            }
        }