Ejemplo n.º 1
0
        public void ExecuteDeviceAuthorizationAfterExpiry()
        {
            const string clientId                = "device";
            ITokenClient tokenClient             = null !;
            DeviceAuthorizationResponse response = null !;

            "Given a token client".x(
                () =>
            {
                tokenClient = new TokenClient(
                    TokenCredentials.AsDevice(),
                    _fixture.Client,
                    new Uri(WellKnownOpenidConfiguration));

                Assert.NotNull(tokenClient);
            });

            "When a device requests authorization".x(
                async() =>
            {
                var genericResponse = await tokenClient.GetAuthorization(new DeviceAuthorizationRequest(clientId))
                                      .ConfigureAwait(false) as Option <DeviceAuthorizationResponse> .Result;

                Assert.NotNull(genericResponse);

                response = genericResponse.Item;
            });

            Option <GrantedTokenResponse> expiredPoll = null !;

            "and the device polls the token server after expiry".x(
                async() =>
            {
                expiredPoll = await tokenClient.GetToken(
                    TokenRequest.FromDeviceCode(clientId, response.DeviceCode, 7))
                              .ConfigureAwait(false);
            });

            "then error shows request expiry".x(
                async() =>
            {
                Assert.IsType <Option <GrantedTokenResponse> .Error>(expiredPoll);
                Assert.Equal(
                    ErrorCodes.ExpiredToken,
                    (expiredPoll as Option <GrantedTokenResponse> .Error).Details.Title);
            });
        }
Ejemplo n.º 2
0
        public void ExecuteDeviceAuthorizationFlowWithUserApproval()
        {
            const string clientId    = "device";
            ITokenClient tokenClient = null !;
            DeviceAuthorizationResponse           response    = null !;
            GrantedTokenResponse                  token       = null !;
            Task <Option <GrantedTokenResponse> > pollingTask = null !;

            "Given a token client".x(
                () =>
            {
                tokenClient = new TokenClient(
                    TokenCredentials.AsDevice(),
                    _fixture.Client,
                    new Uri(WellKnownOpenidConfiguration));

                Assert.NotNull(tokenClient);
            });

            "and an access token".x(
                async() =>
            {
                var authClient = new TokenClient(
                    TokenCredentials.FromClientCredentials(clientId, "client"),
                    _fixture.Client,
                    new Uri(WellKnownOpenidConfiguration));
                var tokenResponse = await authClient.GetToken(
                    TokenRequest.FromPassword("user", "password", new[] { "openid" }))
                                    .ConfigureAwait(false);

                Assert.IsType <Option <GrantedTokenResponse> .Result>(tokenResponse);

                token = (tokenResponse as Option <GrantedTokenResponse> .Result).Item;
            });

            "When a device requests authorization".x(
                async() =>
            {
                var genericResponse = await tokenClient.GetAuthorization(new DeviceAuthorizationRequest(clientId))
                                      .ConfigureAwait(false);

                Assert.IsType <Option <DeviceAuthorizationResponse> .Result>(genericResponse);

                response = (genericResponse as Option <DeviceAuthorizationResponse> .Result).Item;
            });

            "and the device polls the token server".x(
                async() =>
            {
                pollingTask = tokenClient.GetToken(
                    TokenRequest.FromDeviceCode(clientId, response.DeviceCode, response.Interval));

                Assert.False(pollingTask.IsCompleted);
            });

            "and user successfully posts user code".x(
                async() =>
            {
                var client = _fixture.Client();
                var msg    = new HttpRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri(response.VerificationUri),
                    Content    = new FormUrlEncodedContent(
                        new[] { new KeyValuePair <string, string>("code", response.UserCode) })
                };
                msg.Headers.Authorization = new AuthenticationHeaderValue(token.TokenType, token.AccessToken);

                var approval = await client.SendAsync(msg).ConfigureAwait(false);

                Assert.Equal(HttpStatusCode.OK, approval.StatusCode);
            });

            "then token is returned from polling".x(
                async() =>
            {
                var tokenResponse = await pollingTask.ConfigureAwait(false);

                Assert.IsType <Option <GrantedTokenResponse> .Result>(tokenResponse);
            });
        }