1657166220
API Plugin for CakePHP
The CakePHP-API Plugin allows to easily expose a versioned API in a few lines of code.
CakePHP | CakeDC Api Plugin | Tag | Notes |
---|---|---|---|
^3.7 | master | 7.0.0 | stable |
^3.7 | develop | - | unstable |
3.6 | master | 6.0.1 | stable |
3.5 | 5.x | 5.0.0 | stable |
3.4 | 4.x | 4.0.0 | stable |
3.3 | 3.x | 3.3 | stable |
Service is central part of any API that concentrates on all features (operations) related to some application entity. Service define list of actions (operations) that performed on an associated entity. For example: for RESTful service, there could be 4 default operations defined using HTTP verbs for each of CRUD operations.
Each Service could be a separate class located in the src/Service folder, or the request could be passed through default FallbackService (which implements default behavior). Fallback service is defined by setting Api.ServiceFallback, and by default this is \CakeDC\Api\Service\FallbackService.
To create a new Service: first, extend the CakeDC\Api\Service\Service class. The name of your service class should be the name of the Service, followed by "Service" suffix.
namespace App\Service;
use CakeDC\Api\Service\Service;
class FooService extends Service {
// your code
}
The filename should follow the same nomenclature. However, the location of the file itself depends upon your versioning strategy.
Service layer contemplates a versioned API if it is enabled by settings. Enabling it allows you to scale your Services by versioning them as your applications grows. The benefit of this isn't so obvious when developing your application internally, but becomes very important if you expose your service as an external API.
The version of your Service is determined by convention, this being the name of the subdirectory under app/Service/
, or in a plugin. For example, if the first version of your ExampleService
were to be "v1", then you'd create file app/Service/v1/ExampleService.php
in App\Service\v1
namespace.
You can then create a new version of your Service by simply creating a new class under app/Service/v2/ExampleService.php
. Services may also be loaded from plugins using dot notation.
Returning back to newly created FooService class. It must have declared loadRoutes
method that defines this service behavior. Inside this method we should not use default Router class, because we should not rewrite the current cakephp router states while we analyze our service url. Instead, api plugin provide ApiRouter
.
Imagine we want to have a /foo/publish action that accepts HTTP POST requests. In this case we would define the loadRoutes
function.
public function loadRoutes()
{
ApiRouter::scope('/', $this->routerDefaultOptions(), function (RouteBuilder $routes) {
$routes->extensions($this->_extensions);
$options = [
'map' => [
'publish' => ['action' => 'publish', 'method' => 'POST', 'path' => ''],
]
];
$routes->resources($this->name(), $options);
});
}
Here, we will provide two strategies to load actions.
The first strategy is based on service location namespace. In this case, all actions should be located in Action sub-namespace.
So if we have App\Service\FooService
service and want to add publish
action, then we should create the file App\Service\Action\FooPublishAction
. Here, Action
belongs to namespace App\Service\Action
and class defined is 3 parts: Foo is a camelized service name, Publish is a camelized action name, and Action is a suffix.
The second loading strategy could be achieved using $_actionsClassMap
property. It contains a map of action names and full class names, eg.
protected $_actionsClassMap = [
'index' => '\CakeDC\Api\Service\Action\CrudIndexAction',
'view' => '\CakeDC\Api\Service\Action\CrudViewAction',
];
Each service action defined extending Action class.
Action has execution life cycle.
There are two ways to action business logic.
First way is to define action
method in user's action class, where arguments are named the same as input api endpoint parameters (similar to what was dones in the enterprise plugin).
Another way is to have action logic located in execute
method. In this case, method does not accept any parameters the user should interact with.
If action requires input data validation, then it must overload validates()
method that returns boolean result of validation, or could throw ValidationException
. Methods like index, or view, do not require any validation in their lifecycle, and returns true
by default.
NOTE: action validation is not the same as model level validation. The purpose of action validation is to validate action input data and check the correctness and consistency. In case it is invalid, it would prevent stop action execution.
Action.beforeExecute Action.beforeProcess Action.onAuth Action.beforeValidate Action.afterProcess
Crud service defines actions and parameters for RESTful crud API.
Nested Crud service gentting parent params from routing system. If it is present, Nested extension will be loaded for all actions.
Falback service is default implementation of Nested Crud that defines routes for 1-level deep nesting.
Listing service provides list of everything available in system services.
Actions are decorated by some functionality implemented during its life flow. Such decorators are called extensions and are provided by the api plugin.
Different extensions can return additional info that extends when it is returned by API data. In this case, extensions append payload data into Result object, which is used by renderers to build final output.
This way extensions, like pagination or hateoas, inteact with caller.
A Request parser provides the logic required by a Service to resolve requests for input data.
Each Service class defines it's request parser in the parserClass
options property, and by default is populated from the Api.parser setting.
A Renderer provides the logic required by a Service to process the response and handle errors..
Each Service class defines it's renderer in the rendererClass
options property, and by default is populated from the Api.renderer setting.
Suported next renderers:
Each JSend object on top level has result and data items. Additionally all metadata is appended here too.
Links is information about how current api endpoint relates to other endpoints.
crud actions are defined by links here:
By default the Service automatically handles many of the common errors in a request. The following are the status codes returned by the API.
Installation You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require cakedc/cakephp-api
Ensure the API Plugin is loaded in your config/bootstrap.php file
Plugin::load('CakeDC/Api', ['bootstrap' => true, 'routes' => true]);
You can configure the API overwriting the api.php, how? we need to create an api.php file in the config folder. You can copy the existing configuration file under vendor/cakedc/cakephp-api/config/api.php
and customize it for your application. Remember to load the new configuration file in bootstrap.php
Plugin::load('CakeDC/Api', ['bootstrap' => false, 'routes' => true]);
Configure::load('api');
We rely on CakeDC/Users plugin for Auth, in case you need to define Auth for your API, ensure CakeDC/Users plugin is installed and loaded from your bootstrap.php
.
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['bootstrap' => false, 'routes' => true]);
Check more details about how CakeDC/Users plugin could be configured here: https://github.com/CakeDC/users/blob/master/Docs/Documentation/Configuration.md
Api.ServiceFallback - if defined class name service will loaded using this fallback class if service does not exists.
Any service that is loaded as a fallback service or specific service is possible to setup using configuration file.
In the config/api.php part of the Api.Service section, you can define service options.
Lets consider how one can configure options for ArticlesService
.
Configuration are hierarchical in the next sense:
Api.Service.default.options
section.Api.Service.articles.options
section.Now let consider a case where we have service with defined version number 2. In this case, there are many ways to configure service supported:
Api.Service.default.options
section.Api.Service.v2.default.options
section.articles
) service for defined version within the application in Api.Service.v2.articles.options
section.All defined options are overridden from up to down in described order. This allows us to have common service settings, and overwrite then in bottom level.
Any action that can be loaded as a default action defined in fallback class, or specific action class, can be configured using configuration file.
Lets consider how one can configure options for IndexAction
of ArticlesService
.
Configuration are hierarchical in the next sense:
Api.Service.default.Action.default
section.index
action for all services within the application in the Api.Service.default.Action.index
section.articles
) service in the Api.Service.articles.Action.default
section.index
action in the specific (articles
) service in the Api.Service.articles.Action.index
section.Now lets consider a case where we have service with defined version number 1. In this case, there are more ways to configure action supported:
Api.Service.default.Action.default
section.index
action for all services within the application in the Api.Service.default.Action.index
section.Api.Service.v1.default.Action.default
section.index
action for all services for defined version of the application in the Api.Service.v1.default.Action.index
section.articles
) service for defined version in the Api.Service.v1.articles.Action.index
section.index
action in the specific (articles
) service for defined version in the Api.Service.articles.Action.index
section.All defined options are merged from up to down in described order. This allows for configuration of shared behavior for all actions or for a specific subset of actions provided by the services.
See it in auth.md
Service is central part of any API that concentrates on all features (operations) realted to some application entity. Service define list of actions (operations) that is performed on an associated entity. Example: for RESTful service, there could be 4 default operations using HTTP verbs for each of CRUD operations.
Each Service could be a separate class located in the src/Service folder, or a request could be passed through the default FallbackService that implements default behavior. Fallback service is defined by setting Api.ServiceFallback and by default this is \CakeDC\Api\Service\FallbackService.
$_actionsClassMap
The most trival way to define custom action namespace for CRUD action is using $_actionsClassMap, which defines the map between action type and namespace.
protected $_actionsClassMap = [
'index' => '\App\Service\Blogs\IndexAction',
];
Redefining this variable - it is easy to disable default CRUD actions like DELETE.
mapAction
.More complex routes defined with mapAction method. This route accepts action's alias, action class name, and router config. The most logical way to define routes is the Service::intialize method.
Lets define /posts/:id/:flagtype route that accepts POST request.
use App\Posts\FlagAction;
...
public function initialize()
{
parent::initialize();
$this->mapAction('flag', FlagAction::class, [
'method' => ['POST'],
'mapCors' => true,
'path' => ':id/:flag'
]);
}
In some cases you will want to define the route variable type. Perhaps you want to accept only two types of flags: 'spam' and 'inappropriate'.
In this case, we should define variable regex and pass it to router definition.
public function routerDefaultOptions()
{
$options = parent::routerDefaultOptions();
$append = [
'connectOptions' => [
'flag' => 'spam|inappropriate',
]
];
$options = Hash::merge($options, $append);
return $options;
}
Defining router this way requests that other values in the flag field would be completely rejected and action execution would not happen.
Access to variables defined in action router is very simple.
For actions that have :id param, the easiest way is to get the value from $_id
property.
If you need to read additional parameters from parsed router, then you should read them from the parsed route data that is available from actions using the getRoute()
method.
So if we want to read the flag type in action, we can do this:
public function execute()
{
$flag = Hash::get($this->getRoute(), 'flag');
...
}
There are many situations where we are required to implement api that is more complex then just CRUD. In this case of course, we can't use default fallback magic.
Here, we should implement custom service.
The application level service should be in App\Service namespace. Plugins service would go to PluginName\Service namespace.
namespace App\Service;
use CakeDC\Api\Service\CrudService as ApiService;
class BlogsService extends ApiService {
}
These services based on api plugin configuration will accesible using /api/blogs/... url.
It is important to define which actions are supported by the service and inject them into the service router.
One way to do it is to use the intialize method in service class.
public function initialize()
{
parent::initialize();
$this->mapAction('stats', \App\Service\Blogs\StatsAction::class, ['method' => ['GET'], 'mapCors' => true]);
}
On the previous step we reffered to StatsAction. This action is available with the /api/blogs/stats GET route.
Let's define it here:
namespace App\Api\Service\Action\Stats;
use CakeDC\Api\Exception\ValidationException;
use CakeDC\Api\Service\Action\Action;
class StatsAction extends Action
{
public function validates()
{
return true;
}
public function execute()
{
return [
'stats' => 'everything good'
];
}
}
All actions are decorated by some functionality attached to their life flow. Such decorators are called extensions, and are provided with the API plugin.
Cors Purpose - all actions. Purpose: to provide access to the API from another domain. @todo implement configuration from extension config.
CrudAutocompleteList Preparing lists for autocompleters on the client side.
Nested Purpose - all CRUD actions. Provides filtering for (index and view) and patches form data (for add/edit requests) for nested services.
CrudRelations Purpose - index and view actions. Checks for include_relations
parameter, and if it is present, adds related models using ORM contain method to returned data.
CrudHateoas Purpose - all CRUD actions. Metadata - links
. For each CRUD actions, populates links
metadata (see metadata section in this document). Each action has diferent links associated. They are similar to what we have in baked view pages. So index action has a link to add page, and edit page has links to view, delete and index. Additionaly if this service has nested services, links to them also will be appended in a similar way.
Filter Purpose - index action. Checks for parameters named the same as field names and construct conditions. Allows diferent suffixes to field names in order to specify like or comparissons. @todo discuss structures.
FilterParameter Purpose - index action. Checks for filters
parameter, which describes complex queries such as JSON object and construct condition. @todo discuss structures.
CursorPaginate Purpose - index action. Checks for count
, max_id
or since_id
params by default, but allows for override with settings. Provides paginate interface with count
, max_id
or by count
, since_id
parameters that allows for Twitter-style pagination for data that is actively added on the fly. It is a requirement that the table has fields which are unique per item and ordered (it could utilize timestamp or numeric primary key).
Paginate Purpose - index action. Metadata - pagination
. Checks for page
, limit
params by default, but allows override with settings. Provides paginate interface with page
, limit
paramters as input.
ExtendedSort Purpose - index action. Metadata - pagination
. Checka for sort
param by default, but allows for override with settings. Accept sort options as JSON object. Modify query for index action using ORM order()
method based on sort
. Where data in sort params are JSON packed objects with fields and orders as key and values. Orders can be 'asc' or 'desc'.
Sort Purpose - index action. Checks for sort
, direction
params by default, but allows for override with settings. Modify query for index action using ORM order()
method based sort
and direction
params.
What are the renderers?
We have four renderers available for your API. They are:
Above you saw what are the available renderers, then you can click in the links to see how they works.
Collection actions allow us to process many entities on the same endpoint call. This feature was created to allow easy integration with bulk actions, as well as the ability to delete/edit/add many entities.
Adding collections to your own service is easy, use the initialize()
method of your Service class.
$this->mapAction('bulkAdd', AddEditAction::class, [
'method' => ['POST'],
'mapCors' => true,
'path' => 'bulk'
]);
$this->mapAction('bulkEdit', AddEditAction::class, [
'method' => ['PUT'],
'mapCors' => true,
'path' => 'bulk'
]);
$this->mapAction('bulkDelete', DeleteAction::class, [
'method' => ['DELETE'],
'mapCors' => true,
'path' => 'bulk'
]);
If you need additional features, you can extend the specific actions and override the mapping configuration to use your own implementation.
Here are a couple examples, curl based
curl --request POST \
--url http://collections.3dev/api/posts/bulk \
--header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
--form '0[title]=edit existing post title for id 15' \
--form '1[title]=this is a new post' \
--form '0[id]=15'
curl --request DELETE \
--url http://collections.3dev/api/posts/bulk \
--header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
--form '1[id]=14' \
--form '0[id]=15'
For documentation, as well as tutorials, see the Docs directory of this repository.
For bugs and feature requests, please use the issues section of this repository.
Commercial support is also available, contact us for more information.
This repository follows the CakeDC Plugin Standard. If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our Contribution Guidelines for detailed instructions.
Author: CakeDC
Source Code: https://github.com/CakeDC/cakephp-api
License: View license
1595396220
As more and more data is exposed via APIs either as API-first companies or for the explosion of single page apps/JAMStack, API security can no longer be an afterthought. The hard part about APIs is that it provides direct access to large amounts of data while bypassing browser precautions. Instead of worrying about SQL injection and XSS issues, you should be concerned about the bad actor who was able to paginate through all your customer records and their data.
Typical prevention mechanisms like Captchas and browser fingerprinting won’t work since APIs by design need to handle a very large number of API accesses even by a single customer. So where do you start? The first thing is to put yourself in the shoes of a hacker and then instrument your APIs to detect and block common attacks along with unknown unknowns for zero-day exploits. Some of these are on the OWASP Security API list, but not all.
Most APIs provide access to resources that are lists of entities such as /users
or /widgets
. A client such as a browser would typically filter and paginate through this list to limit the number items returned to a client like so:
First Call: GET /items?skip=0&take=10
Second Call: GET /items?skip=10&take=10
However, if that entity has any PII or other information, then a hacker could scrape that endpoint to get a dump of all entities in your database. This could be most dangerous if those entities accidently exposed PII or other sensitive information, but could also be dangerous in providing competitors or others with adoption and usage stats for your business or provide scammers with a way to get large email lists. See how Venmo data was scraped
A naive protection mechanism would be to check the take count and throw an error if greater than 100 or 1000. The problem with this is two-fold:
skip = 0
while True: response = requests.post('https://api.acmeinc.com/widgets?take=10&skip=' + skip), headers={'Authorization': 'Bearer' + ' ' + sys.argv[1]}) print("Fetched 10 items") sleep(randint(100,1000)) skip += 10
To secure against pagination attacks, you should track how many items of a single resource are accessed within a certain time period for each user or API key rather than just at the request level. By tracking API resource access at the user level, you can block a user or API key once they hit a threshold such as “touched 1,000,000 items in a one hour period”. This is dependent on your API use case and can even be dependent on their subscription with you. Like a Captcha, this can slow down the speed that a hacker can exploit your API, like a Captcha if they have to create a new user account manually to create a new API key.
Most APIs are protected by some sort of API key or JWT (JSON Web Token). This provides a natural way to track and protect your API as API security tools can detect abnormal API behavior and block access to an API key automatically. However, hackers will want to outsmart these mechanisms by generating and using a large pool of API keys from a large number of users just like a web hacker would use a large pool of IP addresses to circumvent DDoS protection.
The easiest way to secure against these types of attacks is by requiring a human to sign up for your service and generate API keys. Bot traffic can be prevented with things like Captcha and 2-Factor Authentication. Unless there is a legitimate business case, new users who sign up for your service should not have the ability to generate API keys programmatically. Instead, only trusted customers should have the ability to generate API keys programmatically. Go one step further and ensure any anomaly detection for abnormal behavior is done at the user and account level, not just for each API key.
APIs are used in a way that increases the probability credentials are leaked:
If a key is exposed due to user error, one may think you as the API provider has any blame. However, security is all about reducing surface area and risk. Treat your customer data as if it’s your own and help them by adding guards that prevent accidental key exposure.
The easiest way to prevent key exposure is by leveraging two tokens rather than one. A refresh token is stored as an environment variable and can only be used to generate short lived access tokens. Unlike the refresh token, these short lived tokens can access the resources, but are time limited such as in hours or days.
The customer will store the refresh token with other API keys. Then your SDK will generate access tokens on SDK init or when the last access token expires. If a CURL command gets pasted into a GitHub issue, then a hacker would need to use it within hours reducing the attack vector (unless it was the actual refresh token which is low probability)
APIs open up entirely new business models where customers can access your API platform programmatically. However, this can make DDoS protection tricky. Most DDoS protection is designed to absorb and reject a large number of requests from bad actors during DDoS attacks but still need to let the good ones through. This requires fingerprinting the HTTP requests to check against what looks like bot traffic. This is much harder for API products as all traffic looks like bot traffic and is not coming from a browser where things like cookies are present.
The magical part about APIs is almost every access requires an API Key. If a request doesn’t have an API key, you can automatically reject it which is lightweight on your servers (Ensure authentication is short circuited very early before later middleware like request JSON parsing). So then how do you handle authenticated requests? The easiest is to leverage rate limit counters for each API key such as to handle X requests per minute and reject those above the threshold with a 429 HTTP response.
There are a variety of algorithms to do this such as leaky bucket and fixed window counters.
APIs are no different than web servers when it comes to good server hygiene. Data can be leaked due to misconfigured SSL certificate or allowing non-HTTPS traffic. For modern applications, there is very little reason to accept non-HTTPS requests, but a customer could mistakenly issue a non HTTP request from their application or CURL exposing the API key. APIs do not have the protection of a browser so things like HSTS or redirect to HTTPS offer no protection.
Test your SSL implementation over at Qualys SSL Test or similar tool. You should also block all non-HTTP requests which can be done within your load balancer. You should also remove any HTTP headers scrub any error messages that leak implementation details. If your API is used only by your own apps or can only be accessed server-side, then review Authoritative guide to Cross-Origin Resource Sharing for REST APIs
APIs provide access to dynamic data that’s scoped to each API key. Any caching implementation should have the ability to scope to an API key to prevent cross-pollution. Even if you don’t cache anything in your infrastructure, you could expose your customers to security holes. If a customer with a proxy server was using multiple API keys such as one for development and one for production, then they could see cross-pollinated data.
#api management #api security #api best practices #api providers #security analytics #api management policies #api access tokens #api access #api security risks #api access keys
1601381326
We’ve conducted some initial research into the public APIs of the ASX100 because we regularly have conversations about what others are doing with their APIs and what best practices look like. Being able to point to good local examples and explain what is happening in Australia is a key part of this conversation.
The method used for this initial research was to obtain a list of the ASX100 (as of 18 September 2020). Then work through each company looking at the following:
With regards to how the APIs are shared:
#api #api-development #api-analytics #apis #api-integration #api-testing #api-security #api-gateway
1604399880
I’ve been working with Restful APIs for some time now and one thing that I love to do is to talk about APIs.
So, today I will show you how to build an API using the API-First approach and Design First with OpenAPI Specification.
First thing first, if you don’t know what’s an API-First approach means, it would be nice you stop reading this and check the blog post that I wrote to the Farfetchs blog where I explain everything that you need to know to start an API using API-First.
Before you get your hands dirty, let’s prepare the ground and understand the use case that will be developed.
If you desire to reproduce the examples that will be shown here, you will need some of those items below.
To keep easy to understand, let’s use the Todo List App, it is a very common concept beyond the software development community.
#api #rest-api #openai #api-first-development #api-design #apis #restful-apis #restful-api
1598083582
As more companies realize the benefits of an API-first mindset and treating their APIs as products, there is a growing need for good API product management practices to make a company’s API strategy a reality. However, API product management is a relatively new field with little established knowledge on what is API product management and what a PM should be doing to ensure their API platform is successful.
Many of the current practices of API product management have carried over from other products and platforms like web and mobile, but API products have their own unique set of challenges due to the way they are marketed and used by customers. While it would be rare for a consumer mobile app to have detailed developer docs and a developer relations team, you’ll find these items common among API product-focused companies. A second unique challenge is that APIs are very developer-centric and many times API PMs are engineers themselves. Yet, this can cause an API or developer program to lose empathy for what their customers actually want if good processes are not in place. Just because you’re an engineer, don’t assume your customers will want the same features and use cases that you want.
This guide lays out what is API product management and some of the things you should be doing to be a good product manager.
#api #analytics #apis #product management #api best practices #api platform #api adoption #product managers #api product #api metrics
1602851580
Recently, I worked with my team at Postman to field the 2020 State of the API survey and report. We’re insanely grateful to the folks who participated—more than 13,500 developers and other professionals took the survey, helping make this the largest and most comprehensive survey in the industry. (Seriously folks, thank you!) Curious what we learned? Here are a few insights in areas that you might find interesting:
Whether internal, external, or partner, APIs are perceived as reliable—more than half of respondents stated that APIs do not break, stop working, or materially change specification often enough to matter. Respondents choosing the “not often enough to matter” option here came in at 55.8% for internal APIs, 60.4% for external APIs, and 61.2% for partner APIs.
When asked about the biggest obstacles to producing APIs, lack of time is by far the leading obstacle, with 52.3% of respondents listing it. Lack of knowledge (36.4%) and people (35.1%) were the next highest.
#api #rest-api #apis #api-first-development #api-report #api-documentation #api-reliability #hackernoon-top-story