public static async Task EnterpriseDeviceCommand(this IEnterpriseContext context, EnterpriseDeviceOptions arguments)
        {
            if (arguments.AutoApprove.HasValue)
            {
                context.AutoApproveAdminRequests = arguments.AutoApprove.Value;
                Console.WriteLine($"Automatic Admin Device Approval is {(context.AutoApproveAdminRequests ? "ON" : "OFF")}");
            }

            if (string.IsNullOrEmpty(arguments.Command))
            {
                arguments.Command = "list";
            }

            if (arguments.Force || context.DeviceForAdminApprovals == null)
            {
                await context.GetEnterpriseData("devices_request_for_admin_approval");
            }

            if (context.DeviceForAdminApprovals == null || context.DeviceForAdminApprovals.Length == 0)
            {
                Console.WriteLine("There are no pending devices");
                return;
            }

            var cmd = arguments.Command.ToLowerInvariant();

            switch (cmd)
            {
            case "list":
                var tab = new Tabulate(4)
                {
                    DumpRowNo = false
                };
                Console.WriteLine();
                tab.AddHeader("Email", "Device ID", "Device Name", "Client Version");
                foreach (var device in context.DeviceForAdminApprovals)
                {
                    if (!context.Enterprise.TryGetUserById(device.EnterpriseUserId, out var user))
                    {
                        continue;
                    }

                    tab.AddRow(user.Email, ConnectedContext.TokenToString(device.EncryptedDeviceToken.Base64UrlDecode()), device.DeviceName, device.ClientVersion);
                }

                tab.Sort(1);
                tab.Dump();
                break;

            case "approve":
            case "deny":
                if (string.IsNullOrEmpty(arguments.Match))
                {
                    Console.WriteLine($"{arguments.Command} command requires device ID or user email parameter.");
                }
                else
                {
                    var devices = context.DeviceForAdminApprovals
                                  .Where(x =>
                    {
                        if (arguments.Match == "all")
                        {
                            return(true);
                        }
                        var deviceId = ConnectedContext.TokenToString(x.EncryptedDeviceToken.Base64UrlDecode());
                        if (deviceId.StartsWith(arguments.Match))
                        {
                            return(true);
                        }

                        if (!context.Enterprise.TryGetUserById(x.EnterpriseUserId, out var user))
                        {
                            return(false);
                        }
                        return(user.Email == arguments.Match);
                    }).ToArray();

                    if (devices.Length > 0)
                    {
                        if (cmd == "approve")
                        {
                            await context.ApproveAdminDeviceRequests(devices);
                        }
                        else
                        {
                            await context.DenyAdminDeviceRequests(devices);
                        }
                    }
                    else
                    {
                        Console.WriteLine($"No device found matching {arguments.Match}");
                    }
                }

                break;
            }
        }
Exemplo n.º 2
0
        private async Task DoLogin(LoginOptions options)
        {
            var username      = options.Username;
            var isSsoProvider = options.IsSsoProvider;

            if (isSsoProvider)
            {
                if (string.IsNullOrEmpty(username))
                {
                    Console.Write("Enter SSO Provider: ");
                    username = await Program.GetInputManager().ReadLine();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(username))
                {
                    Console.Write("Enter Username: "******"Username: " + username);
                }
            }

            if (string.IsNullOrEmpty(username))
            {
                return;
            }

            try
            {
                if (isSsoProvider)
                {
                    await _auth.LoginSso(username);
                }
                else
                {
                    _auth.ResumeSession = options.Resume;
                    if (options.IsSsoPassword)
                    {
                        _auth.AlternatePassword = true;
                    }
                    var passwords = new List <string>();

                    if (!string.IsNullOrEmpty(options.Password))
                    {
                        passwords.Add(options.Password);
                    }

                    var uc = _auth.Storage.Users.Get(username);
                    if (!string.IsNullOrEmpty(uc?.Password))
                    {
                        passwords.Add(uc.Password);
                    }

                    await _auth.Login(username, passwords.ToArray());
                }

                if (_auth.IsAuthenticated())
                {
                    var connectedCommands = new ConnectedContext(_auth);
                    NextState = connectedCommands;
                }
            }
            catch (KeeperCanceled)
            {
            }
        }