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

            var token1 = issuer.GenerateRefreshToken(data);

            var token = issuer.DecodeRefreshToken(token1);

            Assert.AreEqual(1, token.ConsumerId);
            Assert.AreEqual(2, token.ResourceOwnerId);
            Assert.AreEqual(ticks, token.Timestamp);
        }
        public void WhenDataIsValid_ThenNewTokenIsCreated()
        {
            var mocker = new AutoMoqer();

            mocker.GetMock <IOAuthRequest>().Setup(x => x.ContentType).Returns(ContentType.FormEncoded);
            mocker.GetMock <IOAuthRequest>().Setup(x => x.ClientId).Returns("clientid");
            mocker.GetMock <IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.RefreshToken);
            mocker.GetMock <IConsumerRepository>().Setup(x => x.GetByClientId("clientid")).Returns(new ConsumerImpl {
                ConsumerId = 12, ClientId = "clientid", Secret = "clientsecret"
            });
            mocker.GetMock <IResourceOwnerRepository>().Setup(x => x.IsConsumerApproved(10, 12)).Returns(true);
            mocker.GetMock <IOAuthRequest>().Setup(x => x.ClientSecret).Returns("clientsecret");
            mocker.SetInstance <IOAuthIssuer>(new OAuthIssuer());
            var issuer = new OAuthIssuer();

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

            var token = issuer.GenerateRefreshToken(new TokenData {
                ConsumerId = 12, ResourceOwnerId = 10, Timestamp = 1
            });

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


            var newToken = authorizer.Authorize(mocker.GetMock <IOAuthRequest>().Object);

            Assert.IsNotNull(newToken);

            var accessTokenData = issuer.DecodeAccessToken(newToken.AccessToken);

            Assert.IsNotNull(accessTokenData);
            Assert.AreEqual(10, accessTokenData.ResourceOwnerId);
            Assert.IsTrue(accessTokenData.Timestamp > DateTimeOffset.UtcNow.AddMinutes(-5).Ticks);

            var refreshTokenData = issuer.DecodeRefreshToken(newToken.RefreshToken);

            Assert.IsNotNull(refreshTokenData);
            Assert.AreEqual(12, refreshTokenData.ConsumerId);
            Assert.AreEqual(10, refreshTokenData.ResourceOwnerId);
            Assert.IsTrue(refreshTokenData.Timestamp > DateTimeOffset.UtcNow.AddMinutes(-5).Ticks);
        }
Esempio n. 3
0
        public void DecodeRefreshtoken_ThrowsExceptionWhenDataIsNull()
        {
            var issuer = new OAuthIssuer();

            issuer.DecodeRefreshToken(null);
        }