Esempio n. 1
0
        public void DecodeAuthorizationToken_DecodesToken()
        {
            var ticks = DateTime.Now.Ticks;
            var data  = new TokenData {
                ConsumerId = 1, Timestamp = ticks, ResourceOwnerId = 3
            };
            var issuer = new OAuthIssuer();

            var token1 = issuer.GenerateAuthorizationToken(data);

            var token = issuer.DecodeAuthorizationToken(token1);

            Assert.AreEqual(1, token.ConsumerId);
            Assert.AreEqual(ticks, token.Timestamp);
            Assert.AreEqual(3, token.ResourceOwnerId);
            Assert.IsNull(token.RedirectUri);

            data = new TokenData {
                ConsumerId = 1, Timestamp = ticks, ResourceOwnerId = 3, RedirectUri = "http://test.com"
            };

            token1 = issuer.GenerateAuthorizationToken(data);

            token = issuer.DecodeAuthorizationToken(token1);

            Assert.AreEqual(1, token.ConsumerId);
            Assert.AreEqual(ticks, token.Timestamp);
            Assert.AreEqual(3, token.ResourceOwnerId);
            Assert.AreEqual("http://test.com", token.RedirectUri);
        }
        public void WhenAuthorizationCodeHasExpired_ThenThrowException()
        {
            var mocker = new AutoMoqer();

            mocker.GetMock <IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
            mocker.GetMock <IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
            mocker.SetInstance <IOAuthIssuer>(new OAuthIssuer());
            var issuer = new OAuthIssuer();
            var token  = issuer.GenerateAuthorizationToken(new TokenData {
                ConsumerId = 1, Timestamp = DateTime.UtcNow.AddHours(-1).Ticks
            });

            mocker.GetMock <IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

            var authorizer = mocker.Resolve <AuthorizationCodeAuthorizer>();

            try
            {
                authorizer.Authorize(mocker.GetMock <IOAuthRequest>().Object);
                Assert.Fail("Exception not thrown");
            }
            catch (OAuthException ex)
            {
                Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
                Assert.IsTrue(ex.ErrorDescription.HasValue());
            }
        }
Esempio n. 3
0
        public void GenerateAuthorizationToken_GeneratesCode()
        {
            var ticks = DateTime.Now.Ticks;
            var data  = new TokenData {
                ConsumerId = 1, ResourceOwnerId = 2, Timestamp = ticks, RedirectUri = "http://www.test.com"
            };
            var issuer = new OAuthIssuer();

            var token1 = issuer.GenerateAuthorizationToken(data);

            data.RedirectUri = null;
            var token2 = issuer.GenerateAuthorizationToken(data);

            data.RedirectUri = "http://test.com";
            var token3 = issuer.GenerateAuthorizationToken(data);

            Assert.AreNotEqual(token1, token2);
            Assert.AreNotEqual(token1, token3);
            Assert.AreNotEqual(token2, token3);
        }
        public void WhenRedirectUriDoesNotMatch_ThenExceptionIsThrown()
        {
            var mocker = new AutoMoqer();

            mocker.GetMock <IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
            mocker.GetMock <IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
            mocker.GetMock <IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500);
            var issuer = new OAuthIssuer();

            mocker.SetInstance <IOAuthIssuer>(issuer);
            var token = issuer.GenerateAuthorizationToken(new TokenData {
                ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks, RedirectUri = "http://test.com"
            });

            mocker.GetMock <IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

            var authorizer = mocker.Resolve <AuthorizationCodeAuthorizer>();


            try
            {
                authorizer.Authorize(mocker.GetMock <IOAuthRequest>().Object);
                Assert.Fail("Exception not thrown");
            }
            catch (OAuthException ex)
            {
                Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
                Assert.IsTrue(ex.ErrorDescription.HasValue());
            }

            mocker.GetMock <IOAuthRequest>().Setup(x => x.RedirectUri).Returns("http://test.com");
            var result = authorizer.Authorize(mocker.GetMock <IOAuthRequest>().Object);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.AccessToken.HasValue());
            Assert.AreEqual(500, result.ExpiresIn);
            Assert.IsTrue(result.RefreshToken.HasValue());
        }
        public void ReturnsAccessToken()
        {
            var mocker = new AutoMoqer();

            mocker.GetMock <IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
            mocker.GetMock <IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
            mocker.GetMock <IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500);
            var issuer = new OAuthIssuer();

            mocker.SetInstance <IOAuthIssuer>(issuer);
            var token = issuer.GenerateAuthorizationToken(new TokenData {
                ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks
            });

            mocker.GetMock <IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

            var authorizer = mocker.Resolve <AuthorizationCodeAuthorizer>();
            var result     = authorizer.Authorize(mocker.GetMock <IOAuthRequest>().Object);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.AccessToken.HasValue());
            Assert.AreEqual(500, result.ExpiresIn);
            Assert.IsTrue(result.RefreshToken.HasValue());
        }
Esempio n. 6
0
        public void GenerateAuthorizationToken_ThrowsExceptionWhenDataIsNull()
        {
            var issuer = new OAuthIssuer();

            issuer.GenerateAuthorizationToken(null);
        }