Exemplo n.º 1
0
        public async Task Authorize_WillObtainServerTimeAndPersistTheOffsetFromTheLocalClock()
        {
            var  client           = GetRestClient();
            bool serverTimeCalled = false;

            // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called
            // (By default the library uses DateTimeOffset.UtcNow whe Now() is called)
            var testAblyAuth = new TestAblyAuth(client.Options, client, () =>
            {
                serverTimeCalled = true;
                return(Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30)));
            });

            // RSA10k: If the AuthOption argument’s queryTime attribute is true
            // it will obtain the server time once and persist the offset from the local clock.
            var tokenParams = new TokenParams();

            testAblyAuth.Options.QueryTime = true;
            await testAblyAuth.AuthorizeAsync(tokenParams);

            serverTimeCalled.Should().BeTrue();
            testAblyAuth.GetServerNow().Should().HaveValue();
            const int precision = 1000;

            testAblyAuth.GetServerNow()?.Should().BeCloseTo(await testAblyAuth.GetServerTime(), TimeSpan.FromMilliseconds(precision));   // Allow 1s clock skew
            testAblyAuth.GetServerNow()?.Should().BeCloseTo(DateTimeOffset.UtcNow.AddMinutes(30), TimeSpan.FromMilliseconds(precision)); // Allow 1s clock skew
        }
Exemplo n.º 2
0
        public async Task Authorize_WillObtainServerTimeAndPersist_ShouldShowValuesAreCalculated()
        {
            var client = GetRestClient();

            // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called
            // (By default the library uses DateTimeOffset.UtcNow whe Now() is called)
            var testAblyAuth = new TestAblyAuth(client.Options, client, () => Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30)));

            // RSA10k: If the AuthOption argument’s queryTime attribute is true
            // it will obtain the server time once and persist the offset from the local clock.
            testAblyAuth.Options.QueryTime = true;
            var tokenParams = new TokenParams();

            testAblyAuth.Options.QueryTime = true;
            await testAblyAuth.AuthorizeAsync(tokenParams);

            // to show the values are calculated and not fixed
            // get the current server time offset, pause for a short time,
            // then get it again.
            // The new value should represent a time after the first
            var snapshot = testAblyAuth.GetServerNow();
            await Task.Delay(500);

            testAblyAuth.GetServerNow()?.Should().BeAfter(snapshot.Value);
        }
Exemplo n.º 3
0
        public async Task ShouldKeepCurrentTokenParamsAndOptionsEvenIfCurrentTokenIsValidAndNoNewTokenIsRequested()
        {
            var client = GetRestClient(
                null,
                opts => opts.TokenDetails = new TokenDetails("boo")
            {
                Expires = Now.AddHours(10)
            });

            var testAblyAuth      = new TestAblyAuth(client.Options, client);
            var customTokenParams = TokenParams.WithDefaultsApplied();

            customTokenParams.Ttl       = TimeSpan.FromHours(2);
            customTokenParams.Timestamp = Now.AddHours(1);

            var customAuthOptions = AuthOptions.FromExisting(testAblyAuth.Options);

            customAuthOptions.UseTokenAuth = true;

            await testAblyAuth.AuthorizeAsync(customTokenParams, customAuthOptions);

            var expectedTokenParams = customTokenParams.Clone();

            expectedTokenParams.Timestamp = null;
            testAblyAuth.CurrentTokenParams.Should().BeEquivalentTo(expectedTokenParams);
            testAblyAuth.CurrentAuthOptions.Should().BeSameAs(customAuthOptions);
        }
Exemplo n.º 4
0
        public async Task Authorize_ObtainServerTimeAndPersistOffset_ShouldShowServerTimeIsCalledOnlyOnce()
        {
            var  client           = GetRestClient();
            bool serverTimeCalled = false;

            // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called
            // (By default the library uses DateTimeOffset.UtcNow whe Now() is called)
            var testAblyAuth = new TestAblyAuth(client.Options, client, () =>
            {
                serverTimeCalled = true;
                return(Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30)));
            });

            // RSA10k: If the AuthOption argument’s queryTime attribute is true
            // it will obtain the server time once and persist the offset from the local clock.
            var tokenParams = new TokenParams();

            testAblyAuth.Options.QueryTime = true;
            await testAblyAuth.AuthorizeAsync(tokenParams);

            // to show the values are calculated and not fixed
            // get the current server time offset, pause for a short time,
            // then get it again.
            // The new value should represent a time after the first
            testAblyAuth.GetServerNow();

            // reset flag, used to show ServerTime() is not called again
            serverTimeCalled = false;

            // RSA10k: All future token requests generated directly or indirectly via a call to
            // authorize will not obtain the server time, but instead use the local clock
            // offset to calculate the server time.
            await testAblyAuth.AuthorizeAsync();

            // ServerTime() should not have been called again
            serverTimeCalled.Should().BeFalse();

            // and we should still be getting calculated offsets
            testAblyAuth.GetServerNow().Should().HaveValue();
            const int precision = 1000;

            testAblyAuth.GetServerNow()?.Should().BeCloseTo(await testAblyAuth.GetServerTime(), TimeSpan.FromMilliseconds(precision));
            testAblyAuth.GetServerNow()?.Should().BeCloseTo(DateTimeOffset.UtcNow.AddMinutes(30), TimeSpan.FromMilliseconds(precision));
        }
Exemplo n.º 5
0
        public async Task Authorize_WillObtainServerTimeAndPersist_ShouldUserServerTimeEvenIfFurtherRequestsDoNotHaveQueryTimeSetToTrue()
        {
            var client           = GetRestClient();
            var serverTimeCalled = 0;

            // configure the AblyAuth test wrapper to return UTC+30m when ServerTime() is called
            // (By default the library uses DateTimeOffset.UtcNow whe Now() is called)
            var testAblyAuth = new TestAblyAuth(client.Options, client, () =>
            {
                Interlocked.Increment(ref serverTimeCalled);
                return(Task.FromResult(DateTimeOffset.UtcNow.AddMinutes(30)));
            });

            // RSA10k: If the AuthOption argument’s queryTime attribute is true
            // it will obtain the server time once and persist the offset from the local clock.
            testAblyAuth.Options.QueryTime = true;
            var tokenParams = new TokenParams();

            testAblyAuth.Options.QueryTime = true;
            await testAblyAuth.AuthorizeAsync(tokenParams);

            // intercept the (mocked) HttpRequest so we can get a reference to the AblyRequest
            TokenRequest tokenRequest = null;
            var          exFunc       = client.ExecuteHttpRequest;

            client.ExecuteHttpRequest = request =>
            {
                tokenRequest = request.PostData as TokenRequest;
                return(exFunc(request));
            };

            // demonstrate that we don't need QueryTime set to get a server time offset
            testAblyAuth.Options.QueryTime = false;
            await testAblyAuth.AuthorizeAsync(tokenParams);

            // offset should be cached
            serverTimeCalled.Should().Be(1);

            // the TokenRequest timestamp should have been set using the offset
            tokenRequest.Timestamp.Should().HaveValue();
            tokenRequest.Timestamp.Should().BeCloseTo(await testAblyAuth.GetServerTime(), TimeSpan.FromMilliseconds(1000));
        }
Exemplo n.º 6
0
        public async Task Authorize_UseRequestTokenToCreateTokensAndPassesTokenParamsAndAuthOptions()
        {
            var client       = GetRestClient();
            var testAblyAuth = new TestAblyAuth(client.Options, client);

            testAblyAuth.Options.UseTokenAuth = true;
            var customAuthOptions = testAblyAuth.Options;
            var customTokenParams = TokenParams.WithDefaultsApplied();

            customTokenParams.Ttl = TimeSpan.FromHours(2);

            await testAblyAuth.AuthorizeAsync(customTokenParams, customAuthOptions);

            testAblyAuth.RequestTokenCalled.Should().BeTrue("Token creation was not delegated to RequestToken");
            testAblyAuth.LastRequestTokenParams.Should().BeSameAs(customTokenParams);
            testAblyAuth.LastRequestAuthOptions.Should().BeSameAs(customAuthOptions);
        }
Exemplo n.º 7
0
        public async Task ShouldKeepTokenParamsAndAuthOptionsExceptForceAndCurrentTimestamp()
        {
            var client            = GetRestClient();
            var testAblyAuth      = new TestAblyAuth(client.Options, client);
            var customTokenParams = TokenParams.WithDefaultsApplied();

            _ = customTokenParams.Merge(new TokenParams {
                Ttl = TimeSpan.FromHours(2), Timestamp = Now.AddHours(1)
            });
            var customAuthOptions = AuthOptions.FromExisting(testAblyAuth.Options);

            customAuthOptions.UseTokenAuth = true;

            await testAblyAuth.AuthorizeAsync(customTokenParams, customAuthOptions);

            var expectedTokenParams = customTokenParams.Clone();

            expectedTokenParams.Timestamp = null;
            testAblyAuth.CurrentTokenParams.Should().BeEquivalentTo(expectedTokenParams);

            testAblyAuth.CurrentAuthOptions.Should().BeSameAs(customAuthOptions);
            testAblyAuth.CurrentTokenParams.Timestamp.Should().Be(null);
        }