Skip to content

Commit 59e12d4

Browse files
authored
Merge pull request #135 from dotkernel/issue121
Added security section with basic guidelines, fixed typos
2 parents a4aadc7 + 66915da commit 59e12d4

File tree

10 files changed

+129
-8
lines changed

10 files changed

+129
-8
lines changed

docs/book/v4/core-features/authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Another table that is pre-populated is the `oauth_scopes` table, with the `api`
4343

4444
### Issuing API Tokens
4545

46-
Token generation in Dotkernel API is done using the `password` `grand_type` scenario, which in this case allows
46+
Token generation in Dotkernel API is done using the `password` `grant_type` scenario, which in this case allows
4747
authentication to an API using the user's credentials (generally a username and password).
4848

4949
The client sends a POST request to the `/security/generate-token` with the following parameters:

docs/book/v5/core-features/authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Another table that is pre-populated is the `oauth_scopes` table, with the `api`
4343

4444
### Issuing API Tokens
4545

46-
Token generation in Dotkernel API is done using the `password` `grand_type` scenario, which in this case allows
46+
Token generation in Dotkernel API is done using the `password` `grant_type` scenario, which in this case allows
4747
authentication to an API using the user's credentials (generally a username and password).
4848

4949
The client sends a POST request to the `/security/generate-token` with the following parameters:

docs/book/v5/openapi/write-documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ To summarize, the typical scenario on working on your own instance of Dotkernel
107107
- create new module (example: `Book`)
108108
- add functionality to your new module (routes, entities, repositories, handlers, services, tests etc)
109109
- create file `OpenAPI.php` in the new module and describe each new endpoint
110-
- generate latest version of documentation file as described [here](./generate-documentation.md)
110+
- generate latest version of documentation file as described [in this tutorial](./generate-documentation.md)

docs/book/v6/core-features/authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Another table that is pre-populated is the `oauth_scopes` table, with the `api`
4343

4444
### Issuing API Tokens
4545

46-
Token generation in Dotkernel API is done using the `password` `grand_type` scenario, which in this case allows
46+
Token generation in Dotkernel API is done using the `password` `grant_type` scenario, which in this case allows
4747
authentication to an API using the user's credentials (generally a username and password).
4848

4949
The client sends a POST request to the `/security/generate-token` with the following parameters:

docs/book/v6/extended-features/handler-structure.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The goal of this update is to implement PSR-15 handlers into Dotkernel API.
55

66
## What is a handler?
77

8-
In DotKernel 6.0, a "handler" is the piece of code that reacts when a user makes a specific request (like visiting a webpage or submitting a form).
8+
In Dotkernel 6.0, a "handler" is the piece of code that reacts when a user makes a specific request (like visiting a webpage or submitting a form).
99
It's basically the "controller" that decides what happens next.
1010

1111
HTTP request handlers are at the core of any web application.
@@ -38,6 +38,6 @@ In this way, the developer can easily figure out the functionality of each handl
3838

3939
## Mapping of the handlers
4040

41-
The full mapping of the handlers and their current paths and actions can be found [**here**](https://docs.dotkernel.org/img/api/v6/naming-convention.png).
41+
The full mapping of the handlers and their current paths and actions can be found in the full [naming convention table](https://docs.dotkernel.org/img/api/v6/naming-convention.png).
4242

4343
[![naming-convention-thumbnail](https://docs.dotkernel.org/img/api/v6/naming-convention-thumbnail.png)](https://docs.dotkernel.org/img/api/v6/naming-convention.png)

docs/book/v6/introduction/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Therefore, for every preflight request, there is at least one Router request.
2929
## OAuth 2.0
3030

3131
OAuth 2.0 is an authorization framework that enables applications to obtain limited access to user accounts on your Dotkernel API.
32-
We use [mezzio/mezzio-authentication-oauth2](https://github.com/mezzio/mezzio-authentication-oauth2) which provides OAuth 2.0 authentication for Mezzio and PSR-15 applications by using the [thephpleague/oauth2-server]https://github.com/thephpleague/oauth2-server package.
32+
We use [mezzio/mezzio-authentication-oauth2](https://github.com/mezzio/mezzio-authentication-oauth2) which provides OAuth 2.0 authentication for Mezzio and PSR-15 applications by using the [thephpleague/oauth2-server](https://github.com/thephpleague/oauth2-server) package.
3333

3434
## Email
3535

docs/book/v6/openapi/write-documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ To summarize, the typical scenario on working on your own instance of Dotkernel
107107
- create new module (example: `Book`)
108108
- add functionality to your new module (routes, entities, repositories, handlers, services, tests etc)
109109
- create file `OpenAPI.php` in the new module and describe each new endpoint
110-
- generate latest version of documentation file as described [here](./generate-documentation.md)
110+
- generate latest version of documentation file as described [in this tutorial](./generate-documentation.md)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Basic Security
2+
3+
Dotkernel API provides all necessary tools to implement safe applications, however you will need to manually make use of some of them.
4+
This section will go over the provided tools and any steps you need to follow in order to use them successfully, as well as a few general considerations.
5+
6+
## User Input Validation
7+
8+
In order to validate user input, Dotkernel API makes use of [laminas/laminas-inputfilter](https://github.com/laminas/laminas-inputfilter).
9+
It is strongly recommended that custom functionality parsing user input also make use of input filters to validate the data.
10+
11+
## Content Negotiation
12+
13+
Content negotiation in Dotkernel API is done by a middleware configured using the `config/autoload/content-negotiation.global.php` file.
14+
15+
Whenever an endpoint needs custom `Accept` and/or `Content-Type`, make sure that you set them in the above file.
16+
17+
> Read more about [content negotiation](https://www.dotkernel.com/dotkernel-api/content-negotiation-in-dotkernel-rest-api/) and the way it is implemented in [Dotkernel API](../core-features/content-validation.md).
18+
19+
## Cross-Origin Resource Sharing
20+
21+
Dotkernel API uses [mezzio/mezzio-cors](https://github.com/mezzio/mezzio-cors) to handle CORS details.
22+
The default configuration, found in `config/autoload/cors.local.php`, makes the application accessible by any origin.
23+
24+
Make sure your application specifies only the required origins when in a production environment.
25+
26+
> This step is described in the [CORS](../tutorials/cors.md) tutorial.
27+
28+
## Role-Based Access Control
29+
30+
This project makes use of [mezzio/mezzio-authorization-rbac](../core-features/authorization.md) to handle access control.
31+
32+
The default use cases have already been configured, but any custom functionality will require additional configuration to make sure it is protected.
33+
Update the configuration file of this package (`config/autoload/authorization.global.php`) whenever you add new routes or roles.
34+
35+
## Demo Credentials
36+
37+
Dotkernel API ships with two demo accounts: an admin account (`admin`) and a user account (`[email protected]`),
38+
with public identities and passwords as described in the [token authentication tutorial](https://docs.dotkernel.org/api-documentation/v6/tutorials/token-authentication/).
39+
40+
Make sure to **update** or **remove** these demo accounts in your production environment.
41+
42+
## Error Reporting Endpoint and ErrorReportingTokens
43+
44+
The error reporting endpoint (`/error-report`) is intended to be used by third parties to report errors back to your application.
45+
This endpoint requires its own token type, namely an `ErrorReportingToken`, that is to be added in the configuration file for every application sending reports.
46+
47+
Since these tokens do not have an expiration date, consider periodically refreshing them manually.
48+
49+
This feature is configured using the `config/autoload/error-handling.global.php` file, under the configured `ErrorReportServiceInterface::class` key.
50+
51+
This file is visible to your VCS by default, so take care not to overwrite tokens locally and commit them to your production environment.
52+
Additionally, make sure the `ip_whitelist` or `domain_whitelist` keys are set to your desired values, especially when in a production environment.
53+
54+
> Read more about the [error reporting](../core-features/error-reporting.md) feature.
55+
56+
## OpenAPI Documentation
57+
58+
In order to provide an interactive documentation, Dotkernel API implemented [zircote/swagger-php](https://github.com/zircote/swagger-php).
59+
60+
Make sure **not** to include sensitive data as examples for any of the documented endpoints.
61+
It is not recommended to enable the documentation in a production environment.
62+
63+
> Read more about the [OpenAPI documentation](../openapi/introduction.md).
64+
65+
## PHP Dependencies
66+
67+
Dotkernel API uses `composer` to handle PHP dependencies.
68+
In time, make sure to review any common vulnerabilities and exposures for your dependencies.
69+
70+
> You may also keep an eye on the Dotkernel API changelog for any updates relevant to your project.
71+
72+
## General Considerations
73+
74+
- `*.global.php` and `*.php.dist` configuration files are visible to the VCS, make sure **not** to include sensitive data in commits.
75+
- `*.local.php` configuration files are ignored by the VCS by default and are the recommended place for sensitive data such as API keys.
76+
- Make sure the `development mode` is correctly set - **do not** enable `development mode` in a production environment.
77+
- You can use the following command to check the current status:
78+
79+
```shell
80+
composer development-status
81+
```
82+
83+
- Dotkernel API ships with a [Laminas Continuous Integration](https://github.com/laminas/laminas-continuous-integration-action) GitHub Action,
84+
if you are using a public repository consider keeping it in your custom applications to ensure code quality.
85+
86+
> Read more about using [Laminas Continuous Integration](https://getlaminas.org/blog/2024-08-05-using-laminas-continuous-integration.html).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# OAuth2 Security
2+
3+
Dotkernel API uses the [mezzio/mezzio-authentication-oauth2](https://github.com/mezzio/mezzio-authentication-oauth2) component to provide the OAuth2 authentication service.
4+
As a security stating point, when developing an application using this project make sure you go over the following steps.
5+
6+
## Default OAuth Clients
7+
8+
The project ships with the default OAuth clients `admin` and `frontend` with passwords equal to their names, as described in the [Authentication](https://docs.dotkernel.org/api-documentation/v6/core-features/authentication/) guide.
9+
10+
These clients **must not** remain unchanged in your production environment, as they are a security risk -
11+
ensure you deleted them or updated the passwords.
12+
13+
## OAuth Token Lifetime and Refresh Hygiene
14+
15+
The configuration for OAuth2 tokens can be edited in `config/autoload/local.php` under the `authentication` key.
16+
17+
By default, the lifetimes of the `access` and `refresh` tokens are set to one day and one month respectively.
18+
Make sure to adjust their values in accordance to your application's needs, with lower values being generally safer.
19+
20+
> If your application requires it, you can revoke user OAuth tokens before their expiration by making use of the `revokeTokens` method of `UserService`.
21+
> Read more about the available [configuration options](https://docs.mezzio.dev/mezzio-authentication-oauth2/v1/intro/#configuration).
22+
23+
## Autogeneration of Cryptographic Keys
24+
25+
Dotkernel API makes use of the `./vendor/bin/generate-oauth2-keys` command from `mezzio-authentication-oauth2` to automatically regenerate the
26+
public/private key pair used to verify the transmitted JWTs.
27+
This process is done after each `composer update` (or `composer install` with no lock file), as specified in `composer.json` under the `scripts.post-update-cmd` key.
28+
29+
While hidden to the VCS by default, keep in mind not to commit any local keys.
30+
31+
> Autogeneration of keys can be disabled by simply removing the `php ./vendor/bin/generate-oauth2-keys` command from the mentioned key.
32+
> While not related to Dotkernel API itself, do ensure that the directory containing the keys is properly secured.

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ nav:
6868
- "Render Documentation": v6/openapi/render-documentation.md
6969
- "Use Documentation": v6/openapi/use-documentation.md
7070
- "Getting Help": v6/openapi/getting-help.md
71+
- Basic Security:
72+
- "Basic Security": v6/security/basic-security.md
73+
- "OAuth2 Security": v6/security/oauth2-security.md
7174
- Reference:
7275
- "Anonymize Accounts": v6/reference/account-anonymization.md
7376
- v5:

0 commit comments

Comments
 (0)