Beispiel #1
0
        protected async override Task <int> ExecuteCommand(LoginCommand arguments)
        {
            Console.WriteLine("Logging out...");
            logger.LogInformation("Logging out... with {Arguments}", arguments);

            var userToken = await tokenStorage.LoadUserTokenAsync(); //Fetch before deleting tokens

            logger.LogInformation("Loaded {UserToken}", userToken);

            Console.WriteLine($"Deleting tokens...");
            await tokenStorage.DeleteAsync();

            Console.WriteLine($"Deleted tokens.");

            Console.WriteLine($"Deleting public keys...");
            await licensePublicKeyStorage.DestroyAsync();

            Console.WriteLine($"Deleted public keys.");

            Console.WriteLine($"Deleting stored licenses...");
            await licenseStorage.DeleteAsync();

            Console.WriteLine($"Deleted stored licenses.");

            if (userToken is null)
            {
                Console.WriteLine("User was not logged in. Can not revoke tokens...");
                Console.WriteLine("Logout was successful.");
                return(0);
            }

            var disco = await discoveryProvider.FetchDiscoveryDocument();

            var revokeRefreshTokenResult = await RevokeRefreshToken(userToken, disco);

            if (revokeRefreshTokenResult > 0)
            {
                return(revokeRefreshTokenResult);
            }
            var revokeAccessTokenResult = await RevokeAccessToken(userToken, disco);

            if (revokeAccessTokenResult > 0)
            {
                return(revokeAccessTokenResult);
            }

            Console.WriteLine("Logout was successful.");

            return(0);
        }
Beispiel #2
0
        protected async override Task <int> ExecuteCommand(LoginCommand arguments)
        {
            Console.WriteLine("Logging in...");
            logger.LogInformation("Logging in... with {Arguments}", arguments);
            if (!arguments.NoCache)
            {
                var userToken = await tokenStorage.LoadUserTokenAsync();

                logger.LogInformation("Loaded {UserToken}", userToken);
                if (userToken != null)
                {
                    userToken = await refreshTokenHandler.RefreshTokenAsync(userToken);

                    logger.LogInformation("Refreshed {UserToken}", userToken);
                    if (userToken != null)
                    {
                        Console.WriteLine("Login successful!");
                        logger.LogInformation("Login successful!");
                        await StoreToken(arguments, userToken);

                        return(0);
                    }
                }
            }

            var disco = await discoveryProvider.FetchDiscoveryDocument();

            var request = new DeviceAuthorizationRequest
            {
                Address  = disco.DeviceAuthorizationEndpoint,
                ClientId = configurationProvider.ClientId,
                Scope    = configurationProvider.Scope
            };

            logger.LogInformation("RequestDeviceAuthorizationAsync {Request}", request);

            var result = await httpClient.RequestDeviceAuthorizationAsync(request);

            if (result.IsError)
            {
                Console.WriteLine(result.Error);
                var ex = new Exception(result.Error);
                logger.LogError(ex, "RequestDeviceAuthorizationAsync {Request} {Response}", request, result);
                throw ex;
            }
            else
            {
                logger.LogInformation("RequestDeviceAuthorizationAsync {Request} {Response}", request, result);
            }

            Console.WriteLine($"Visit: {result.VerificationUri}");
            Console.WriteLine();
            Console.WriteLine("And enter this code");
            Console.WriteLine("-------------------");
            Console.WriteLine($"-    {result.UserCode}    -");
            Console.WriteLine("-------------------");

            var fetchToken = true;
            var interval   = (result.Interval == 0 ? 5 : result.Interval) * 1000;
            var spinner    = new ConsoleSpinner();

            while (fetchToken)
            {
                spinner.Turn();
                Console.Write(" Fetching token....");
                var tokenResponse = await httpClient.RequestDeviceTokenAsync(new DeviceTokenRequest
                {
                    Address    = disco.TokenEndpoint,
                    ClientId   = configurationProvider.ClientId,
                    DeviceCode = result.DeviceCode,
                });

                if (tokenResponse.IsError)
                {
                    if (tokenResponse.Error == "authorization_pending" || tokenResponse.Error == "slow_down")
                    {
                        await Task.Delay(interval);
                    }
                    else
                    {
                        spinner.ClearLine();
                        Console.WriteLine(tokenResponse.Error);
                        throw new Exception(tokenResponse.Error);
                    }
                }
                else
                {
                    var userToken = new UserToken(
                        tokenResponse.AccessToken,
                        tokenResponse.RefreshToken,
                        tokenResponse.IdentityToken,
                        DateTime.UtcNow.AddSeconds(tokenResponse.ExpiresIn)
                        );

                    spinner.ClearLine();

                    Console.WriteLine("Login successful!");

                    await StoreToken(arguments, userToken);

                    return(0);
                }
            }

            return(0);
        }