public string DoAuthentication()
        {
            var privateKey = salesforceSettings.GetPrivateKey();

            try
            {
                var authClient = new JwtAuthenticationClient(salesforceSettings.ApiVersion, salesforceSettings.IsProduction);
                authClient.JwtPrivateKeyAsync(
                    salesforceSettings.ClientId,
                    privateKey,
                    salesforceSettings.Passphrase,
                    salesforceSettings.Username,
                    salesforceSettings.TokenEndpoint
                    ).Wait();

                return(authClient.AccessToken);
            }
            catch (Exception ex)
            {
                var exceptions          = ExceptionsHelper.GetExceptionDetailsAsString(ex);
                var errorMessageBuilder = new StringBuilder();
                errorMessageBuilder.AppendLine("Authentication to Salesforce failed.");
                // TODO
                // Do more parsing and handling of exceptions here.
                errorMessageBuilder.Append(exceptions);

                throw new AuthenticationFailedException(errorMessageBuilder.ToString());
            }
        }
Example #2
0
        public async Task Authenticate_WithUnencryptedKey_Success()
        {
            var authClient = new JwtAuthenticationClient();
            await authClient.JwtUnencryptedPrivateKeyAsync(
                "jgasgdjasgdajsgdjs",
                CommonHelpers.LoadFromFile("TestKeys/server.key"),
                "user",
                $"http://localhost:{_authServerFixture.PublicPort}/services/oauth2/token"
                );

            Assert.Equal("jhjhdjashdjashdjashdjashdjasdhsjadhasjdhj", authClient.AccessToken);
            Assert.Equal("https://my-org.salesforce.com", authClient.InstanceUrl);
        }
Example #3
0
        public async Task Authenticate_WithUnencryptedKey_401Exception()
        {
            var authClient = new JwtAuthenticationClient();
            var assertion  = await Assert.ThrowsAsync <ForceAuthenticationException>(async() =>
            {
                await authClient.JwtUnencryptedPrivateKeyAsync(
                    "jgasgdjasgdajsgdjs",
                    CommonHelpers.LoadFromFile("TestKeys/server.key"),
                    "user-error",
                    $"http://localhost:{_authServerFixture.PublicPort}/services/oauth2/token"
                    );
            });

            Assert.Equal("invalid_grant: an error description", assertion.Message);
        }
Example #4
0
        public async Task Authenticate_WithUnencryptedKey_404Exception()
        {
            var authClient = new JwtAuthenticationClient();
            var assertion  = await Assert.ThrowsAsync <ForceAuthenticationException>(async() =>
            {
                await authClient.JwtUnencryptedPrivateKeyAsync(
                    "jgasgdjasgdajsgdjs",
                    CommonHelpers.LoadFromFile("TestKeys/server.key"),
                    "user",
                    $"http://localhost:{_authServerFixture.PublicPort}/servicesx/oauth2/token"
                    );
            });

            // TODO Somewhat strange error in case of 404, needs further investigation
            Assert.Equal(
                "Unexpected character encountered while parsing value: <. Path '', line 0, position 0.",
                assertion.Message
                );
        }