public async Task IfTokenExpiredRefreshTokenIsRetrievedAndUsedToRefreshTheAccessToken()
        {
            // arrange
            const string tokenName    = "Test Token";
            var          refreshToken = Guid.NewGuid().ToString();
            var          fixture      = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName);

            var token = fixture.TokenGenerator.GenerateToken(DateTime.UtcNow.AddMinutes(-15),
                                                             DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow.AddMinutes(-17));
            var tokenDataDummy  = new TokenData(token);
            var newAccessToken  = fixture.TokenGenerator.GenerateToken();
            var newRefreshToken = Guid.NewGuid().ToString();

            fixture.TokenStorageMock
            .Setup(t => t.RetrieveAccessTokenAsync(tokenName))
            .ReturnsAsync(tokenDataDummy);
            fixture.TokenStorageMock
            .Setup(t => t.RetrieveRefreshTokenAsync(tokenName))
            .ReturnsAsync(refreshToken);

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthRefreshSetup(newAccessToken, newRefreshToken);
            var sut = fixture.Instance;

            // act
            var result = await sut.CheckSigninAsync(tokenName);

            // assert
            result.Token.Should().Be(newAccessToken);
            fixture.TokenStorageMock.Verify(t => t.StoreAccessTokenAsync(tokenName, newAccessToken), Times.Once);
            fixture.TokenStorageMock.Verify(t => t.StoreRefreshTokenAsync(tokenName, newRefreshToken), Times.Never);
            fixture.HttpMock.Dispose();
        }
        public async Task SigninFailureReturnsNullAndLogsTheIssue()
        {
            // arrange
            var          loggerMock   = LoggerFixture.SetupLogCatcher();
            const string tokenName    = "Test Token";
            var          refreshToken = Guid.NewGuid().ToString();
            var          fixture      = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName, true);

            var token = fixture.TokenGenerator.GenerateToken();

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthSetup(token, refreshToken);
            var sut = fixture.Instance;

            // act
            var result = await sut.CheckSigninAsync(tokenName);

            // assert
            result.Should().BeNull();
            loggerMock.Verify(t => t.Error("Error occured during user authentication. {Error}", It.IsAny <string>()),
                              Times.Once);

            fixture.HttpMock.HttpMock.Dispose();
        }
        public async Task IfThereAreNoTokensSigninIsExecutedToAcquireTokens()
        {
            // arrange
            const string tokenName    = "Test Token";
            var          refreshToken = Guid.NewGuid().ToString();
            var          fixture      = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName, true);

            var token = fixture.TokenGenerator.GenerateToken();

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthSetup(token, refreshToken);
            fixture.UserInfoSetup();
            var sut = fixture.Instance;

            // act
            var result = await sut.CheckSigninAsync(tokenName);

            // assert
            result.Token.Should().Be(token);
            fixture.TokenStorageMock.Verify(t => t.StoreAccessTokenAsync(tokenName, token), Times.Once);
            fixture.TokenStorageMock.Verify(t => t.StoreRefreshTokenAsync(tokenName, refreshToken), Times.Once);

            fixture.HttpMock.Dispose();
        }
Exemple #4
0
        public async Task IfTokenExpiresWithinTheRefreshIntervalItIsRefreshed()
        {
            // arrange
            const string tokenName    = "Test Token";
            var          refreshToken = Guid.NewGuid().ToString();
            var          fixture      = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName, true);

            var token           = fixture.TokenGenerator.GenerateToken(30);
            var tokenDataDummy  = new TokenData(token);
            var newAccessToken  = fixture.TokenGenerator.GenerateToken(75);
            var newRefreshToken = Guid.NewGuid().ToString();

            fixture.TokenStorageMock
            .Setup(t => t.RetrieveAccessTokenAsync(tokenName))
            .ReturnsAsync(tokenDataDummy);
            fixture.TokenStorageMock
            .Setup(t => t.RetrieveRefreshTokenAsync(tokenName))
            .ReturnsAsync(refreshToken);

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthRefreshSetup(newAccessToken, newRefreshToken);
            var sut = fixture.Instance;

            // act
            await sut.CheckSigninAsync(tokenName);

            // assert
            fixture.TokenStorageMock.Verify(t => t.StoreRefreshTokenAsync(tokenName, newRefreshToken), Times.Once);
            fixture.HttpMock.Dispose();
        }
Exemple #5
0
        public async Task IfRefreshIntervalIsMadeLongerThanTokensRemainingTimeTheTokenIsRefreshed()
        {
            // arrange
            var          refreshTimeout = TimeSpan.FromSeconds(100);
            const string tokenName      = "Test Token";
            var          refreshToken   = Guid.NewGuid().ToString();
            var          fixture        = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName, true);

            var token = fixture.TokenGenerator.GenerateToken(DateTime.UtcNow.AddMinutes(-15),
                                                             DateTime.UtcNow.AddSeconds(150), DateTime.UtcNow.AddMinutes(-17));
            var       tokenDataDummy  = new TokenData(token);
            var       newAccessToken  = fixture.TokenGenerator.GenerateToken(75);
            var       newRefreshToken = Guid.NewGuid().ToString();
            var       raisingToken    = string.Empty;
            TokenData refreshedToken  = null;

            fixture.TokenStorageMock
            .Setup(t => t.RetrieveAccessTokenAsync(tokenName))
            .ReturnsAsync(tokenDataDummy);
            fixture.TokenStorageMock
            .Setup(t => t.RetrieveRefreshTokenAsync(tokenName))
            .ReturnsAsync(refreshToken);

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthRefreshSetup(newAccessToken, newRefreshToken);
            var sut = fixture.Instance;

            sut.SetupRefreshTimeSpan(refreshTimeout);
            sut.TokenRefreshed += (sender, pair) =>
            {
                raisingToken   = pair.Key;
                refreshedToken = pair.Value;
            };

            // act
            await sut.CheckSigninAsync(tokenName);

            sut.SetupRefreshTimeSpan(TimeSpan.FromSeconds(200));
            await Task.Delay(TimeSpan.FromSeconds(10));

            // assert
            raisingToken.Should().Be(tokenName);
            refreshedToken.Token.Should().Be(newAccessToken);
            fixture.HttpMock.Dispose();
        }
Exemple #6
0
        public async Task FailureToGetATokenWithTimerRefreshDoesNotBroadcastARefreshEvent()
        {
            // arrange
            var          loggerDummy    = LoggerFixture.SetupLogCatcher();
            var          refreshTimeout = TimeSpan.FromSeconds(10);
            const string tokenName      = "Test Token";
            var          refreshToken   = Guid.NewGuid().ToString();
            var          fixture        = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName, true);

            var token = fixture.TokenGenerator.GenerateToken(DateTime.UtcNow.AddMinutes(-15),
                                                             DateTime.UtcNow.AddSeconds(15), DateTime.UtcNow.AddMinutes(-17));
            var tokenDataDummy = new TokenData(token);
            var wasRefreshed   = false;

            fixture.TokenStorageMock
            .Setup(t => t.RetrieveAccessTokenAsync(tokenName))
            .ReturnsAsync(tokenDataDummy);
            fixture.TokenStorageMock
            .Setup(t => t.RetrieveRefreshTokenAsync(tokenName))
            .ReturnsAsync(refreshToken);

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthRefreshSetup(null, null);
            var sut = fixture.Instance;

            sut.SetupRefreshTimeSpan(refreshTimeout);
            sut.TokenRefreshed += (sender, pair) => { wasRefreshed = true; };

            // act
            await sut.CheckSigninAsync(tokenName);

            await Task.Delay(TimeSpan.FromSeconds(10));

            // assert
            wasRefreshed.Should().BeFalse();
            loggerDummy.Verify(
                t => t.Warning(
                    "Attempting to refresh access token failed. No further auto-refreshes will occur for {TokenName}",
                    tokenName), Times.Once);

            fixture.HttpMock.Dispose();
        }
        public async Task FailureDuringRefreshTokenRetrievalWillResultInFullTokenRetrieval()
        {
            // arrange
            var          loggerMock   = LoggerFixture.SetupLogCatcher();
            const string tokenName    = "Test Token";
            var          refreshToken = Guid.NewGuid().ToString();
            var          fixture      = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName);

            var token = fixture.TokenGenerator.GenerateToken(DateTime.UtcNow.AddMinutes(-15),
                                                             DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow.AddMinutes(-17));
            var tokenDataDummy = new TokenData(token);
            var newAccessToken = fixture.TokenGenerator.GenerateToken();

            fixture.TokenStorageMock
            .Setup(t => t.RetrieveAccessTokenAsync(tokenName))
            .ReturnsAsync(tokenDataDummy);
            fixture.TokenStorageMock
            .Setup(t => t.RetrieveRefreshTokenAsync(tokenName))
            .ReturnsAsync(refreshToken);

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthRefreshSetup(newAccessToken, refreshToken, true);
            fixture.AuthSetup(newAccessToken, refreshToken);
            fixture.UserInfoSetup();
            var sut = fixture.Instance;

            // act
            await sut.CheckSigninAsync(tokenName);

            // assert
            loggerMock.Verify(t =>
                              t.Error("Error occured while trying to refresh Access Token. {Error}", It.IsAny <string>()));
            //result.Should().NotBeNull();
            loggerMock.Verify(t => t.Error("Error occured during user authentication. {Error}", It.IsAny <string>()),
                              Times.Once);

            fixture.HttpMock.HttpMock.Dispose();
        }
Exemple #8
0
        public async Task IfTokenExpiresAfterRefreshIntervalItIsAutomaticallyRefreshed()
        {
            // arrange
            var          refreshTimeout = TimeSpan.FromSeconds(10);
            const string tokenName      = "Test Token";
            var          refreshToken   = Guid.NewGuid().ToString();
            var          fixture        = new TokenManagerFixture();
            await fixture.SetupServerDataAsync(tokenName, true);

            var token = fixture.TokenGenerator.GenerateToken(DateTime.UtcNow.AddMinutes(-15),
                                                             DateTime.UtcNow.AddSeconds(15), DateTime.UtcNow.AddMinutes(-17));
            var tokenDataDummy  = new TokenData(token);
            var newAccessToken  = fixture.TokenGenerator.GenerateToken(75);
            var newRefreshToken = Guid.NewGuid().ToString();

            fixture.TokenStorageMock
            .Setup(t => t.RetrieveAccessTokenAsync(tokenName))
            .ReturnsAsync(tokenDataDummy);
            fixture.TokenStorageMock
            .Setup(t => t.RetrieveRefreshTokenAsync(tokenName))
            .ReturnsAsync(refreshToken);

            fixture.WellKnownSetup();
            fixture.SetupCertificates();
            fixture.AuthRefreshSetup(newAccessToken, newRefreshToken);
            var sut = fixture.Instance;

            sut.SetupRefreshTimeSpan(refreshTimeout);

            // act
            await sut.CheckSigninAsync(tokenName);

            await Task.Delay(TimeSpan.FromSeconds(10));

            // assert
            fixture.TokenStorageMock.Verify(t => t.StoreRefreshTokenAsync(tokenName, newRefreshToken), Times.Once);
            fixture.HttpMock.Dispose();
        }