Пример #1
0
        private static void Execute(TestOptions opts)
        {
            try
            {
                var config = new LoggerConfiguration();
                config.WriteTo.ColoredConsole(outputTemplate: "{Message:lj}{NewLine}{Exception}");

                if (opts.Verbose)
                {
                    config.MinimumLevel.Debug();
                }
                else
                {
                    config.MinimumLevel.Information();
                }

                Log.Logger = config.CreateLogger();

                TestConnectExceptions(opts.Appliance);

                ISafeguardConnection connection;
                if (!string.IsNullOrEmpty(opts.Username))
                {
                    var password = HandlePassword(opts.ReadPassword);
                    connection = Safeguard.Connect(opts.Appliance, opts.IdentityProvider, opts.Username, password,
                                                   opts.ApiVersion, opts.Insecure);
                }
                else if (opts.Anonymous)
                {
                    connection = Safeguard.Connect(opts.Appliance, opts.ApiVersion, opts.Insecure);
                }
                else
                {
                    throw new Exception("Must specify Anonymous or Username");
                }

                Log.Debug($"Access Token Lifetime Remaining: {connection.GetAccessTokenLifetimeRemaining()}");

                TestApiExceptions(connection);

                connection.LogOut();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Fatal exception occurred");
                Environment.Exit(1);
            }
        }
        public static ISafeguardConnection Connect(string appliance, string primaryProviderId = "", string secondaryProviderId = "", string username = "", int port = 8400)
        {
            Log.Debug("Calling RSTS for primary authentication");
            var te = new TokenExtractor(appliance);

            if (te.Show(primaryProviderId, secondaryProviderId, username, port))
            {
                if (string.IsNullOrEmpty(te.AccessToken?.ToInsecureString()))
                {
                    throw new SafeguardDotNetException("Unable to obtain access token from redirect");
                }
                Log.Debug("Posting second RSTS access token to login response service");
                var responseObject = PostLoginResponse(appliance, te.AccessToken);
                var statusValue    = responseObject.GetValue("Status")?.ToString();
                if (statusValue != null && !statusValue.Equals("Success"))
                {
                    throw new SafeguardDotNetException($"Error response status {statusValue} from login response service");
                }
                using (var accessToken = responseObject.GetValue("UserToken")?.ToString().ToSecureString())
                    return(Safeguard.Connect(appliance, accessToken, DefaultApiVersion, true));
            }

            throw new SafeguardDotNetException("Unable to correctly manipulate the browser for Safeguard login");
        }