Exemplo n.º 1
0
        public async Task RefreshTokenAsync_WhenCalledWithSupportedTokenType_ReturnsActionResultFromRefreshTokenAsyncOnTokenHelperForTokenType()
        {
            TokenType           tokenType    = _fixture.Create <TokenType>();
            IActionResult       actionResult = new Mock <IActionResult>().Object;
            ITokenHelper        tokenHelper  = BuildTokenHelperMock(tokenType, actionResult).Object;
            ITokenHelperFactory sut          = CreateSut(new[] { tokenHelper });

            HttpContext   httpContext = CreateHttpContext();
            string        resultUrl   = _fixture.Create <string>();
            IActionResult result      = await sut.RefreshTokenAsync(tokenType, httpContext, resultUrl);

            Assert.That(result, Is.EqualTo(actionResult));
        }
Exemplo n.º 2
0
        public async Task RefreshTokenAsync_WhenCalledWithSupportedTokenType_AssertRefreshTokenAsyncWasCalledOnTokenHelperForTokenType()
        {
            TokenType           tokenType       = _fixture.Create <TokenType>();
            Mock <ITokenHelper> tokenHelperMock = BuildTokenHelperMock(tokenType);
            ITokenHelperFactory sut             = CreateSut(new[] { tokenHelperMock.Object });

            HttpContext httpContext = CreateHttpContext();
            string      resultUrl   = _fixture.Create <string>();
            await sut.RefreshTokenAsync(tokenType, httpContext, resultUrl);

            tokenHelperMock.Verify(m => m.RefreshTokenAsync(
                                       It.Is <HttpContext>(value => value == httpContext),
                                       It.Is <string>(value => string.CompareOrdinal(value, resultUrl) == 0)),
                                   Times.Once);
        }
        public void OnActionExecuting(ActionExecutingContext context)
        {
            NullGuard.NotNull(context, nameof(context));

            ControllerActionDescriptor controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor == null || controllerActionDescriptor.MethodInfo == null)
            {
                return;
            }

            AcquireTokenAttribute acquireTokenAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttribute <AcquireTokenAttribute>();

            if (acquireTokenAttribute == null)
            {
                return;
            }

            TokenType tokenType = acquireTokenAttribute.TokenType;

            IToken token = GetTokenAsync(tokenType, context.HttpContext).GetAwaiter().GetResult();

            if (token != null)
            {
                return;
            }

            IRefreshableToken refreshableToken = GetRefreshableTokenAsync(tokenType, context.HttpContext).GetAwaiter().GetResult();

            if (refreshableToken == null)
            {
                context.Result = _tokenHelperFactory.AuthorizeAsync(tokenType, context.HttpContext, context.HttpContext.Request.GetDisplayUrl())
                                 .GetAwaiter()
                                 .GetResult();
                return;
            }

            if (refreshableToken.HasExpired == false)
            {
                return;
            }

            context.Result = _tokenHelperFactory.RefreshTokenAsync(tokenType, context.HttpContext, context.HttpContext.Request.GetDisplayUrl())
                             .GetAwaiter()
                             .GetResult();
        }
Exemplo n.º 4
0
        public void RefreshTokenAsync_WhenCalledWithUnsupportedTokenType_ThrowsNotSupportedException()
        {
            ITokenHelperFactory sut = CreateSut();

            TokenType             tokenType = _fixture.Create <TokenType>();
            NotSupportedException result    = Assert.ThrowsAsync <NotSupportedException>(async() => await sut.RefreshTokenAsync(tokenType, CreateHttpContext(), _fixture.Create <string>()));

            Assert.That(result.Message, Is.EqualTo($"The token type '{tokenType}' is not supported within the method 'RefreshTokenAsync'."));
        }
Exemplo n.º 5
0
        public void RefreshTokenAsync_WhenReturnUrlIsWhiteSpace_ThrowsArgumentNullException()
        {
            ITokenHelperFactory sut = CreateSut();

            ArgumentNullException result = Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.RefreshTokenAsync(_fixture.Create <TokenType>(), CreateHttpContext(), " "));

            Assert.That(result.ParamName, Is.EqualTo("returnUrl"));
        }
Exemplo n.º 6
0
        public void RefreshTokenAsync_WhenHttpContextIsNull_ThrowsArgumentNullException()
        {
            ITokenHelperFactory sut = CreateSut();

            ArgumentNullException result = Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.RefreshTokenAsync(_fixture.Create <TokenType>(), null, _fixture.Create <string>()));

            Assert.That(result.ParamName, Is.EqualTo("httpContext"));
        }