Exemplo n.º 1
0
        public ActionResult <Boolean> Patch([FromRoute] Guid packageId, [FromRoute] String objectType)
        {
            // Do the authentication process with the given request and helpers
            // to determine the result
            ApiAuthenticationResult authResult =
                WebAuthHelper.AuthenticateApiRequest(
                    packageId,
                    objectType,
                    SessionHandler.PackageRepository,
                    Request
                    );

            // Everything work ok? Then continue to the important bits
            if (authResult.StatusCode != HttpStatusCode.OK)
            {
                return(StatusCode((Int32)authResult.StatusCode, authResult.StatusDescription));
            }

            // Get the body of the request from the stream
            String body = HttpRequestHelper.GetBody(Request);

            if (body == String.Empty)
            {
                return(StatusCode((Int32)HttpStatusCode.BadRequest, "Request body contained no data"));
            }

            // Got to the end so must be ok
            return(StatusCode((Int32)HttpStatusCode.OK, ""));
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            Enum.TryParse(Location, out _location);
            var serviceUrl = new Uri(string.Format(UrlFactory.BaseUrl, Organization, _location, UrlFactory.OrgServiceQueryPath));

            // OAuth authentication, data connection
            if (ParameterSetName == "oauthdata")
            {
                var endpoint = UrlFactory.GetDiscoveryUrl(serviceUrl, ApiType.CustomerEngagement);
                var auth     = new OAuthHelper(endpoint, ClientId, RedirectUri);
                var service  = CrmConnection.GetConnection(serviceUrl, auth.AuthResult.AccessToken);
                SessionState.PSVariable.Set(SessionVariableFactory.OAuthData, auth);
                SessionState.PSVariable.Set(SessionVariableFactory.DataConnection, service);
            }
            // Cookie authentication, data connection
            else if (ParameterSetName == "webauth")
            {
                Enum.TryParse(AuthType, out Models.AuthenticationType authType);
                var webAuthCookies = WebAuthHelper.GetAuthenticatedCookies(serviceUrl.ToString(), authType);
                var service        = CrmConnection.GetConnection(serviceUrl, webAuthCookies);
                SessionState.PSVariable.Set(SessionVariableFactory.WebCookies, webAuthCookies);
                SessionState.PSVariable.Set(SessionVariableFactory.DataConnection, service);
            }
            // OAuth authentication, admin API
            else if (ParameterSetName == "oauthadmin")
            {
                serviceUrl = UrlFactory.GetServiceUrl(_location);
                var endpoint = UrlFactory.GetDiscoveryUrl(serviceUrl, ApiType.Admin);
                var auth     = new OAuthHelper(endpoint, ClientId, RedirectUri);
                SessionState.PSVariable.Set(SessionVariableFactory.OAuthAdmin, auth);
            }
        }
        public void Generate_Valid_Basic_Authentication_Token()
        {
            // Arrange
            String expectedResult = "VXNlcm5hbWU6UGFzc3dvcmQ=";
            String result         = String.Empty;
            String username       = "******";
            String password       = "******";

            // Act
            result = WebAuthHelper.GenerateBasicAuthString(username, password);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 4
0
        public ActionResult <Boolean> Get([FromRoute] Guid packageId, [FromRoute] String objectType)
        {
            // Do the authentication process with the given request and helpers
            // to determine the result
            ApiAuthenticationResult authResult =
                WebAuthHelper.AuthenticateApiRequest(
                    packageId,
                    objectType,
                    SessionHandler.PackageRepository,
                    Request
                    );

            // Everything work ok? Then continue to the important bits
            if (authResult.StatusCode == HttpStatusCode.OK)
            {
                // Use the api definition to get the data connection and
                // definition from the package and then try to connect
                IDataProvider provider = providerFactory.Get(
                    authResult.Package,
                    authResult.Package.DataConnection(authResult.ApiDefinition.DataConnection),
                    authResult.Package.DataDefinition(authResult.ApiDefinition.DataDefinition),
                    true);

                // Are we connected?
                if (provider.Connected)
                {
                    // Return the data with the appropriate filter
                    DataTable results = provider.Read(authResult.Permissions.Filter);

                    // Manage any aliases for the results table
                    ManagedApiHelper.HandleAliases(results, authResult.ApiDefinition.Aliases);

                    // Format the data table as Json
                    return(ManagedApiHelper.ToJson(results));
                }
                else
                {
                    return(StatusCode((Int32)HttpStatusCode.InternalServerError, "Could not connect to the data source"));
                }
            }
            else
            {
                return(StatusCode((Int32)authResult.StatusCode, authResult.StatusDescription));
            }
        }
Exemplo n.º 5
0
        public ActionResult <Boolean> Delete([FromRoute] Guid packageId, [FromRoute] String objectType)
        {
            // Do the authentication process with the given request and helpers
            // to determine the result
            ApiAuthenticationResult authResult =
                WebAuthHelper.AuthenticateApiRequest(
                    packageId,
                    objectType,
                    SessionHandler.PackageRepository,
                    Request
                    );

            // Everything work ok? Then continue to the important bits
            if (authResult.StatusCode == HttpStatusCode.OK)
            {
                return(StatusCode((Int32)HttpStatusCode.OK, ""));
            }
            else
            {
                return(StatusCode((Int32)authResult.StatusCode, authResult.StatusDescription));
            }
        }
        public ApiResponse <String> GenerateBasicAuth([FromBody] BasicAuthTokenRequest request)
        {
            // Create a new response object
            ApiResponse <String> response = new ApiResponse <string>()
            {
                Data = String.Empty, Success = false
            };

            // Check for input
            if ((request.Username ?? String.Empty) == String.Empty)
            {
                response.Messages.Add("No username provided");
            }

            if ((request.Password ?? String.Empty) == String.Empty)
            {
                response.Messages.Add("No password provided");
            }

            // No errors?
            if (response.Messages.Count == 0)
            {
                // Try and get the token from the helper
                response.Data = WebAuthHelper.GenerateBasicAuthString(request.Username, request.Password);
                if (response.Data == String.Empty)
                {
                    response.Messages.Add("No token generated");
                }

                // Success?
                response.Success = (response.Messages.Count == 0) &&
                                   ((response.Data ?? String.Empty) != String.Empty);
            }

            // Send the result back
            return(response);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            Enum.TryParse(Location, out _location);
            var serviceUrl = new Uri(string.Format(UrlFactory.BaseUrl, Organization, _location, UrlFactory.OrgServiceQueryPath));

            // OAuth authentication
            if (ParameterSetName == "oauth")
            {
                var endpoint = UrlFactory.GetDiscoveryUrl(serviceUrl, ApiType.CustomerEngagement);
                var auth     = new OAuthHelper(endpoint, ClientId, RedirectUri);
                _service = CrmConnection.GetConnection(serviceUrl, auth.AuthResult.AccessToken);
            }
            // Cookie authentication
            else if (ParameterSetName == "webauth")
            {
                Enum.TryParse(AuthenticationType, out Models.AuthenticationType authType);
                var webAuthCookies = WebAuthHelper.GetAuthenticatedCookies(serviceUrl.ToString(), authType);
                _service = CrmConnection.GetConnection(new Uri(serviceUrl, UrlFactory.OrgServiceQueryPath), webAuthCookies);
            }

            WriteObject(_service);
        }
Exemplo n.º 8
0
        public ActionResult <Boolean> Post([FromRoute] Guid packageId, [FromRoute] String objectType)
        {
            // Do the authentication process with the given request and helpers
            // to determine the result
            ApiAuthenticationResult authResult =
                WebAuthHelper.AuthenticateApiRequest(
                    packageId,
                    objectType,
                    SessionHandler.PackageRepository,
                    Request
                    );

            // Everything work ok? Then continue to the important bits
            if (authResult.StatusCode != HttpStatusCode.OK)
            {
                return(StatusCode((Int32)authResult.StatusCode, authResult.StatusDescription));
            }

            // Get the body of the request from the stream
            String body = HttpRequestHelper.GetBody(Request);

            if (body == String.Empty)
            {
                return(StatusCode((Int32)HttpStatusCode.BadRequest, "Request body contained no data"));
            }

            // Translate the body
            try
            {
                DataTable data = null;

                // Parse the body to a queryable Json Object (bad formatting will fail it)
                data = ManagedApiHelper.ToDataTable(
                    Request.ContentType.Trim().ToLower(),
                    body,
                    authResult.ApiDefinition,
                    authResult.DataDefinition);

                // Did we get some data from the conversion (depending on the type format)
                if (data != null)
                {
                    // Use the api definition to get the data connection and
                    // definition from the package and then try to connect
                    IDataProvider provider = providerFactory.Get(
                        authResult.Package,
                        authResult.Package.DataConnection(authResult.ApiDefinition.DataConnection),
                        authResult.Package.DataDefinition(authResult.ApiDefinition.DataDefinition),
                        true);

                    // Are we connected?
                    if (provider.Connected)
                    {
                        // Return the data with the appropriate filter
                        // DataTable results = provider.Read(authResult.Permissions.Filter);
                        if (provider.Write(data, ""))
                        {
                        }
                        else
                        {
                            return(StatusCode((Int32)HttpStatusCode.InternalServerError, "Could not write the data"));
                        }
                    }
                    else
                    {
                        return(StatusCode((Int32)HttpStatusCode.InternalServerError, "Could not connect to the data source"));
                    }
                }
                else
                {
                    return(StatusCode((Int32)HttpStatusCode.BadRequest, "Request body contained no valid data or the format was incorrect"));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((Int32)HttpStatusCode.BadRequest, $"Malformed body content in request ({ex.Message})"));
            }

            // Got to the end so must be ok
            return(StatusCode((Int32)HttpStatusCode.OK, ""));
        }