public void SourcesCommandTest_AddWithUserNamePassword()
        {
            using (var preserver = new DefaultConfigurationFilePreserver())
            {
                // Arrange

                string[] args = new string[] {
                    "sources",
                    "Add",
                    "-Name",
                    "test_source",
                    "-Source",
                    "http://test_source",
                    "-UserName",
                    "test_user_name",
                    "-Password",
                    "test_password"
                };

                // Act
                int result = Program.Main(args);

                // Assert
                Assert.Equal(0, result);

                var settings = Settings.LoadDefaultSettings(null, null, null);
                var source   = settings.GetValue("packageSources", "test_source");
                Assert.Equal("http://test_source", source);

                var credentials = settings.GetNestedValues(
                    "packageSourceCredentials", "test_source");
                Assert.Equal(2, credentials.Count);

                Assert.Equal("Username", credentials[0].Key);
                Assert.Equal("test_user_name", credentials[0].Value);

                Assert.Equal("Password", credentials[1].Key);
                var password = EncryptionUtility.DecryptString(credentials[1].Value);
                Assert.Equal("test_password", password);
            }
        }
        public ActionResult BCServiceLogin(string path, string code)
        {
            // check to see if we have a local path.  (do not allow a redirect to another website)
            if (!string.IsNullOrEmpty(path) && (Url.IsLocalUrl(path) || (!_env.IsProduction() && path.Equals("headers"))))
            {
                // diagnostic feature for development - echo headers back.
                if ((!_env.IsProduction()) && path.Equals("headers"))
                {
                    ContentResult contentResult = new ContentResult();
                    contentResult.Content     = LoggingEvents.GetHeaders(Request);
                    contentResult.ContentType = "text/html";
                    return(contentResult);
                }
                return(LocalRedirect(path));
            }
            else
            {
                if (string.IsNullOrEmpty(_configuration["ENABLE_SERVICECARD_TOKEN_TEST"]))
                {
                    string basePath = string.IsNullOrEmpty(_configuration["BASE_PATH"]) ? "" : _configuration["BASE_PATH"];
                    basePath += "/worker-qualification/dashboard";
                    return(Redirect(basePath));
                }
                else
                {
                    string decrypted = EncryptionUtility.DecryptString(code, _encryptionKey);
                    if (decrypted == null)
                    {
                        decrypted = "";
                    }

                    return(new ContentResult
                    {
                        ContentType = "text/html",
                        StatusCode = (int)HttpStatusCode.OK,
                        Content = $"<html><body><h1>Token Parse Test</h1><p>Decrypted text is: {decrypted}</body></html>"
                    });
                }
            }
        }
Exemplo n.º 3
0
 private string GetApiKey()
 {
     return(_apiKeyEncrypted && ApiKey != null
     ? EncryptionUtility.DecryptString(ApiKey)
     : ApiKey);
 }
Exemplo n.º 4
0
        private bool AppIsAuthorised()
        {
            string path = Application.dataPath + "/../" + SecurityFilePath + "/" + SecurityFileName;

            if (!FileUtility.CheckFileExists(path))
            {
                Debug.Log("[SecurityManager] - Security file is missing");
                return(false);
            }

            string fileContents = FileUtility.ReadFile(path);

            if (string.IsNullOrEmpty(fileContents))
            {
                return(false);
            }

            string decryptFileContents;

            try
            {
                Debug.Log(ApplicationManager.proj.ToString());
                decryptFileContents = EncryptionUtility.DecryptString(fileContents, Key);
            }
            catch (Exception)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(decryptFileContents))
            {
                return(false);
            }

            string decryptedStringMinusRnd = decryptFileContents.Substring(3, decryptFileContents.Length - 5).Remove(27, 2).Remove(13, 1); // get the string minus some random characters
            string appDataPath             = Application.dataPath;
            int    startIndex      = appDataPath.LastIndexOf("/", StringComparison.Ordinal);
            string applicationName = appDataPath.Substring(appDataPath.LastIndexOf("/", StringComparison.Ordinal), appDataPath.Length - startIndex).GetStringBetween("/", "_Data");

            string[] decryptedKeys = decryptedStringMinusRnd.Split('!');

            if (decryptedKeys.Length != 4)
            {
                return(false);
            }

            string macAddress = decryptedKeys[0];
            string decryptedApplicationExeName = decryptedKeys[1];
            string decryptedBiosInfo           = decryptedKeys[2];
            string decryptedOSInfo             = decryptedKeys[3];

            if (!VerifyMacAddress(macAddress)) //Check MAC ADDRESS is correct
            {
                Debug.Log("[SecurityManager] - The app was not authorized to run on this device");
                return(false);
            }

            if (decryptedApplicationExeName != applicationName) //Check Application Name is correct
            {
                Debug.Log("[SecurityManager] - This app was not authorized to run");
                return(false);
            }

            if (!VerifyBiosInfo(decryptedBiosInfo)) //Check hardware / bios information is correct
            {
                Debug.Log("[SecurityManager] - The app was not authorized to run on this hardware");
                return(false);
            }

            if (!VerifyOSInfo(decryptedBiosInfo)) //Check OS information is correct
            {
                Debug.Log("[SecurityManager] - The app was not authorized to run on this system");
                return(false);
            }

            return(true);
        }
Exemplo n.º 5
0
        public async Task TestBuildSnapApp()
        {
            await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem);

            var nugetOrgFeed = new SnapNugetFeed
            {
                Name            = "nuget.org",
                Source          = new Uri(NuGetConstants.V3FeedUrl),
                ProtocolVersion = NuGetProtocolVersion.V3,
                Username        = "******",
                Password        = "******",
                ApiKey          = "myapikey"
            };

            var updateFeedHttp = new SnapHttpFeed
            {
                Source = new Uri("https://mydynamicupdatefeed.com")
            };

            var testChannel = new SnapChannel
            {
                Name       = "test",
                PushFeed   = nugetOrgFeed,
                UpdateFeed = nugetOrgFeed,
                Current    = true
            };

            var stagingChannel = new SnapChannel
            {
                Name       = "staging",
                PushFeed   = nugetOrgFeed,
                UpdateFeed = updateFeedHttp
            };

            var productionChannel = new SnapChannel
            {
                Name       = "production",
                PushFeed   = nugetOrgFeed,
                UpdateFeed = nugetOrgFeed
            };

            var snapAppBefore = new SnapApp
            {
                Id      = "demoapp",
                MainExe = "demoapp",
                InstallDirectoryName = "demoapp",
                SuperVisorId         = Guid.NewGuid().ToString(),
                Version  = new SemanticVersion(1, 0, 0),
                Channels = new List <SnapChannel>
                {
                    testChannel,
                    stagingChannel,
                    productionChannel
                },
                Target = new SnapTarget
                {
                    Os        = OSPlatform.Windows,
                    Framework = "netcoreapp2.1",
                    Rid       = "win-x64",
                    Shortcuts = new List <SnapShortcutLocation>
                    {
                        SnapShortcutLocation.Desktop,
                        SnapShortcutLocation.Startup
                    },
                    PersistentAssets = new List <string>
                    {
                        "subdirectory",
                        "myjsonfile.json"
                    }
                }
            };

            var snapApps = new SnapApps(snapAppBefore);

            var snapAppAfter = snapApps.BuildSnapApp(snapAppBefore.Id, snapAppBefore.Target.Rid,
                                                     snapAppBefore.BuildNugetSources(nugetTempDirectory), _fileSystem);

            snapAppAfter.Version = snapAppBefore.Version.BumpMajor();

            // Generic
            Assert.Equal(snapAppBefore.Id, snapAppAfter.Id);
            Assert.Equal(snapAppBefore.InstallDirectoryName, snapAppAfter.InstallDirectoryName);
            Assert.Equal(snapAppBefore.MainExe, snapAppAfter.MainExe);
            Assert.NotNull(snapAppAfter.MainExe);
            Assert.Equal(snapAppBefore.SuperVisorId, snapAppAfter.SuperVisorId);
            Assert.True(snapAppBefore.Version < snapAppAfter.Version);

            // Target
            Assert.NotNull(snapAppBefore.Target);
            Assert.NotNull(snapAppAfter.Target);
            Assert.Equal(snapAppBefore.Target.Os, snapAppAfter.Target.Os);
            Assert.Equal(snapAppBefore.Target.Rid, snapAppAfter.Target.Rid);
            Assert.NotNull(snapAppBefore.Target.Framework);
            Assert.NotNull(snapAppAfter.Target.Framework);
            Assert.Equal(snapAppBefore.Target.Framework, snapAppAfter.Target.Framework);
            Assert.Equal(snapAppBefore.Target.Rid, snapAppAfter.Target.Rid);
            Assert.Equal(snapAppBefore.Target.Shortcuts, snapAppAfter.Target.Shortcuts);
            Assert.Equal(snapAppBefore.Target.PersistentAssets, snapAppAfter.Target.PersistentAssets);

            // Channels
            Assert.Equal(snapAppBefore.Channels.Count, snapAppAfter.Channels.Count);
            for (var index = 0; index < snapAppAfter.Channels.Count; index++)
            {
                var lhsChannel = snapAppBefore.Channels[index];
                var rhsChannel = snapAppAfter.Channels[index];

                Assert.Equal(lhsChannel.Name, rhsChannel.Name);
                Assert.NotNull(lhsChannel.PushFeed);
                Assert.NotNull(rhsChannel.PushFeed);
                Assert.NotNull(lhsChannel.UpdateFeed);
                Assert.NotNull(rhsChannel.UpdateFeed);

                if (index == 0)
                {
                    Assert.True(lhsChannel.Current);
                    Assert.True(rhsChannel.Current);
                }
                else
                {
                    Assert.False(lhsChannel.Current);
                    Assert.False(rhsChannel.Current);
                }

                var lhsNugetPushFeed = lhsChannel.PushFeed;
                var rhsNugetPushFeed = rhsChannel.PushFeed;

                Assert.Equal(lhsNugetPushFeed.Name, rhsNugetPushFeed.Name);
                Assert.Equal(lhsNugetPushFeed.Source, rhsNugetPushFeed.Source);
                Assert.Equal(lhsNugetPushFeed.ProtocolVersion, rhsNugetPushFeed.ProtocolVersion);
                Assert.Equal(lhsNugetPushFeed.ApiKey, rhsNugetPushFeed.ApiKey);
                Assert.Equal(lhsNugetPushFeed.Username, rhsNugetPushFeed.Username);

                // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
                if (lhsNugetPushFeed.IsPasswordEncryptionSupported())
                {
                    Assert.Equal(EncryptionUtility.DecryptString(lhsNugetPushFeed.Password), rhsNugetPushFeed.Password);
                }
                else
                {
                    Assert.Equal(lhsNugetPushFeed.Password, rhsNugetPushFeed.Password);
                }

                var lhsUpdateFeed = lhsChannel.UpdateFeed;
                var rhsUpdateFeed = rhsChannel.UpdateFeed;

                switch (rhsUpdateFeed)
                {
                case SnapNugetFeed rhsNugetUpdateFeed:
                    var lhsNugetUpdateFeed = (SnapNugetFeed)lhsUpdateFeed;
                    Assert.Equal(lhsNugetUpdateFeed.Name, rhsNugetUpdateFeed.Name);
                    Assert.Equal(lhsNugetUpdateFeed.Source, rhsNugetUpdateFeed.Source);
                    Assert.Equal(lhsNugetUpdateFeed.ProtocolVersion, rhsNugetUpdateFeed.ProtocolVersion);
                    Assert.Equal(lhsNugetUpdateFeed.ApiKey, rhsNugetUpdateFeed.ApiKey);
                    Assert.Equal(lhsNugetUpdateFeed.Username, rhsNugetUpdateFeed.Username);
                    Assert.Equal(
                        lhsNugetUpdateFeed.IsPasswordEncryptionSupported()
                                ? EncryptionUtility.DecryptString(lhsNugetUpdateFeed.Password)
                                : lhsNugetUpdateFeed.Password, rhsNugetUpdateFeed.Password);
                    break;

                case SnapHttpFeed rhsHttpUpdateFeed:
                    var lhsHttpUpdateFeed = (SnapHttpFeed)lhsUpdateFeed;
                    Assert.NotNull(lhsHttpUpdateFeed.Source);
                    Assert.NotNull(rhsHttpUpdateFeed.Source);
                    Assert.Equal(lhsHttpUpdateFeed.Source, rhsHttpUpdateFeed.Source);
                    break;

                default:
                    throw new NotSupportedException(rhsUpdateFeed.GetType().ToString());
                }
            }
        }
Exemplo n.º 6
0
        public async Task TestBuildSnapFeedsFromNugetPackageSources(NuGetProtocolVersion protocolVersion)
        {
            await using var nugetTempDirectory = new DisposableDirectory(_baseFixture.WorkingDirectory, _fileSystem);

            var feedUrl = protocolVersion switch
            {
                NuGetProtocolVersion.V2 => NuGetConstants.V2FeedUrl,
                NuGetProtocolVersion.V3 => NuGetConstants.V3FeedUrl,
                _ => throw new NotSupportedException(protocolVersion.ToString())
            };

            var snapNugetFeed = new SnapNugetFeed
            {
                Name            = "nuget.org",
                ProtocolVersion = protocolVersion,
                Source          = new Uri(feedUrl),
                Username        = "******",
                Password        = "******",
                ApiKey          = "myapikey"
            };

            var snapChannel = new SnapChannel
            {
                Name       = "test",
                PushFeed   = snapNugetFeed,
                UpdateFeed = snapNugetFeed
            };

            var snapApp = new SnapApp
            {
                Channels = new List <SnapChannel> {
                    snapChannel
                }
            };

            var nugetPackageSources = snapApp.BuildNugetSources(nugetTempDirectory);

            Assert.NotNull(nugetPackageSources.Settings);
            Assert.Single(nugetPackageSources.Items);

            var snapFeeds = snapApp.BuildNugetSources(nugetTempDirectory);

            Assert.NotNull(snapFeeds.Settings);
            Assert.Single(snapFeeds.Items);

            var snapNugetFeedAfter = snapFeeds.Items.Single();

            Assert.Equal(snapNugetFeed.Name, snapNugetFeedAfter.Name);
            Assert.Equal((int)snapNugetFeed.ProtocolVersion, snapNugetFeedAfter.ProtocolVersion);
            Assert.Equal(snapNugetFeed.Source, snapNugetFeedAfter.SourceUri);
            Assert.Equal(snapNugetFeed.Username, snapNugetFeedAfter.Credentials.Username);
            var credential = snapNugetFeedAfter.Credentials;

            if (nugetPackageSources.IsPasswordEncryptionSupported())
            {
                Assert.False(credential.IsPasswordClearText);
                Assert.Equal(EncryptionUtility.DecryptString(snapNugetFeed.Password), credential.Password);
                Assert.Equal(snapNugetFeed.Password, credential.PasswordText);
            }
            else
            {
                Assert.True(credential.IsPasswordClearText);
                Assert.Equal(snapNugetFeed.Password, credential.Password);
            }
            Assert.Equal(snapNugetFeed.ApiKey, snapNugetFeedAfter.GetDecryptedValue(nugetPackageSources, ConfigurationConstants.ApiKeys));
        }
Exemplo n.º 7
0
        public void TestBuildNugetSourcesFromSnapApp(NuGetProtocolVersion protocolVersion)
        {
            string feedUrl;

            switch (protocolVersion)
            {
            case NuGetProtocolVersion.V2:
                feedUrl = NuGetConstants.V2FeedUrl;
                break;

            case NuGetProtocolVersion.V3:
                feedUrl = NuGetConstants.V3FeedUrl;
                break;

            default:
                throw new NotSupportedException(protocolVersion.ToString());
            }

            var snapNugetFeed = new SnapNugetFeed
            {
                Name            = "nuget.org",
                ProtocolVersion = protocolVersion,
                Source          = new Uri(feedUrl),
                Username        = "******",
                Password        = "******",
                ApiKey          = "myapikey"
            };

            var snapChannel = new SnapChannel
            {
                Name       = "test",
                PushFeed   = snapNugetFeed,
                UpdateFeed = snapNugetFeed
            };

            var snapApp = new SnapApp
            {
                Channels = new List <SnapChannel> {
                    snapChannel
                }
            };

            var nuGetPackageSources = snapApp.BuildNugetSources(_baseFixture.NugetTempDirectory);

            Assert.Single(nuGetPackageSources.Items);

            var packageSource = nuGetPackageSources.Items.Single();

            Assert.True(packageSource.IsEnabled);
            Assert.True(packageSource.IsOfficial);
            Assert.False(packageSource.IsPersistable);
            Assert.False(packageSource.IsMachineWide);

            Assert.Equal(snapNugetFeed.Name, packageSource.Name);
            Assert.Equal(snapNugetFeed.Source.ToString(), packageSource.TrySourceAsUri.ToString());
            Assert.Equal((int)snapNugetFeed.ProtocolVersion, packageSource.ProtocolVersion);
            Assert.NotNull(packageSource.Credentials);

            var credential = packageSource.Credentials;

            Assert.Equal(snapNugetFeed.Username, credential.Username);
            if (nuGetPackageSources.IsPasswordEncryptionSupported())
            {
                Assert.False(credential.IsPasswordClearText);
                Assert.Equal(EncryptionUtility.DecryptString(snapNugetFeed.Password), credential.Password);
                Assert.Equal(snapNugetFeed.Password, credential.PasswordText);
            }
            else
            {
                Assert.True(credential.IsPasswordClearText);
                Assert.Equal(snapNugetFeed.Password, credential.Password);
            }
            Assert.Equal(snapNugetFeed.Name, credential.Source);

            Assert.Equal(snapNugetFeed.ApiKey, packageSource.GetDecryptedValue(nuGetPackageSources, ConfigurationConstants.ApiKeys));
        }