Skip to content

Commit

Permalink
style: ran prettier on changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhendrickson13 committed May 16, 2024
1 parent 62148d9 commit 36f7e93
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Setup python
uses: actions/setup-python@v3
with:
python-version: '3.10'
python-version: "3.10"
- name: Build mkdocs site
run: |
python3 -m pip install -r ./requirements.txt
Expand Down
84 changes: 46 additions & 38 deletions docs/AUTHENTICATION_AND_AUTHORIZATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,50 @@ methods, custom authentication methods can be added by extending the `Auth` clas
logic.

!!! Note
Multiple authentication methods can be enabled at the same time. If multiple methods are
enabled, the first method that successfully authenticates the user will be used.
Multiple authentication methods can be enabled at the same time. If multiple methods are
enabled, the first method that successfully authenticates the user will be used.

### Basic Authentication (Local database)

Basic authentication is the default and simplest form of authentication and is supported by most HTTP clients. This
method uses the same username and password as your pfSense webConfigurator login and requires the client to send an
`Authorization` header with the value `Basic <base64 encoded username:password>`. The REST API will decode the base64
encoded string and attempt to authenticate the user with the provided credentials. Below are two examples of using
Basic authentication is the default and simplest form of authentication and is supported by most HTTP clients. This
method uses the same username and password as your pfSense webConfigurator login and requires the client to send an
`Authorization` header with the value `Basic <base64 encoded username:password>`. The REST API will decode the base64
encoded string and attempt to authenticate the user with the provided credentials. Below are two examples of using
Basic authentication with `curl`:

Full syntax:

```bash
curl -H "Authorization: Basic YWRtaW46cGZzZW5zZQo=" https://pfsense.example.com/api/v2/firewall/rules
```

Short syntax:

```bash
curl -u admin:pfsense https://pfsense.example.com/api/v2/firewall/rules
```

!!! Note
Currently, the REST API does not support Basic authentication with LDAP or RADIUS authentication server backends.
Only the local database is supported.
Currently, the REST API does not support Basic authentication with LDAP or RADIUS authentication server backends.
Only the local database is supported.

### API Key Authentication

API key authentication is a more secure form of authentication that requires the client to send an `X-API-Key` header
containing a valid API key. These keys are better suited to distribute to systems as they cannot allow webConfigurator
or SSH authentication (like local database credentials can). API keys can be generated in the pfSense webConfigurator
under `System` -> `REST API` -> `Keys` or by `POST` request to /api/v2/auth/key. Below is an example of API Key
authentication using `curl`:
or SSH authentication (like local database credentials can). API keys can be generated in the pfSense webConfigurator
under `System` -> `REST API` -> `Keys` or by `POST` request to /api/v2/auth/key. Below is an example of API Key
authentication using `curl`:

```bash
curl -H "X-API-Key: xxxxxxxxxxxxxxxxxxxxxxx" https://pfsense.example.com/api/v2/firewall/rules
```

!!! Important
API keys utilize the privileges assigned to the user that generated the key.
API keys utilize the privileges assigned to the user that generated the key.
!!! Warning
API keys are sensitive information and should be treated as such. Do not share your API key with anyone you do not
trust. If you believe your API key has been compromised, you can revoke it in the pfSense webConfigurator.
API keys are sensitive information and should be treated as such. Do not share your API key with anyone you do not
trust. If you believe your API key has been compromised, you can revoke it in the pfSense webConfigurator.

### JSON Web Token (JWT) Authentication

Expand All @@ -72,9 +74,9 @@ curl -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxx" https://pfsense.example.
```

!!! Important
JWTs are only valid for a limited time (default 1 hour) and must be regenerated after they expire. The expiration
interval can be configured in the REST API settings within the pfSense webConfigurator or via a `PATCH` request to
the `/api/v2/system/restapi/settings` endpoint's `jwt_exp` field.
JWTs are only valid for a limited time (default 1 hour) and must be regenerated after they expire. The expiration
interval can be configured in the REST API settings within the pfSense webConfigurator or via a `PATCH` request to
the `/api/v2/system/restapi/settings` endpoint's `jwt_exp` field.

### Custom Authentication

Expand All @@ -87,35 +89,41 @@ needs to return a boolean value indicating if the user is authenticated or not.

namespace RESTAPI\Auth;

require_once('RESTAPI/autoloader.inc');
require_once "RESTAPI/autoloader.inc";

use RESTAPI\Core\Auth;

class MyCustomAuth extends Auth {
# Defines a human-readable name for your custom auth method
public ?string $verbose_name = 'My Custom Auth';

# Tells OpenAPI how to use your custom auth method in the Swagger documentation. In this example, we are telling
# OpenAPI to check for an authentication secret in the HTTP header named 'custom-header-name'
public array $security_scheme = ['type' => 'apiKey', 'in' => 'header', 'name' => 'custom-header-name'];

public function _authenticate(): bool {
# Add your custom authentication logic here
if ($my_custom_auth_logic) {
return true;
}

# Return false if authentication fails
return false;
class MyCustomAuth extends Auth
{
# Defines a human-readable name for your custom auth method
public ?string $verbose_name = "My Custom Auth";

# Tells OpenAPI how to use your custom auth method in the Swagger documentation. In this example, we are telling
# OpenAPI to check for an authentication secret in the HTTP header named 'custom-header-name'
public array $security_scheme = [
"type" => "apiKey",
"in" => "header",
"name" => "custom-header-name",
];

public function _authenticate(): bool
{
# Add your custom authentication logic here
if ($my_custom_auth_logic) {
return true;
}

# Return false if authentication fails
return false;
}
}
```

Once your custom Auth class is created, simply place it at `/usr/local/pkg/RESTAPI/Auth/MyCustomAuth.inc` and it will
automatically become an available authentication method in the REST API settings.

!!! Note
Rename `MyCustomAuth` to any name you like, but make sure the class name matches the file name.
Rename `MyCustomAuth` to any name you like, but make sure the class name matches the file name.

## Authorization

Expand All @@ -124,6 +132,6 @@ However, a unique privilege is generated for each API endpoint and it's associat
be assigned to users the same way as any other privilege in pfSense.

!!! Note
The REST API will respect the `WebCfg - All pages` and `User - Config: Deny Config Write` privileges. Users with
the `WebCfg - All pages` privilege will have full access to the REST API. Users with the `User - Config: Deny
Config Write` privilege will be allowed to make API calls but attempts to modify the configuration will be denied.
The REST API will respect the `WebCfg - All pages` and `User - Config: Deny Config Write` privileges. Users with
the `WebCfg - All pages` privilege will have full access to the REST API. Users with the `User - Config: Deny
Config Write` privilege will be allowed to make API calls but attempts to modify the configuration will be denied.
18 changes: 9 additions & 9 deletions docs/COMMON_CONTROL_PARAMETERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ parameters you can use:
parameter may not be supported by all endpoints.

!!! Tip
Instead of forcing the API to wait for changes to apply, it is recommended to leave this parameter set to `true`
and periodically check the status of the changes using a `GET` request to the applicable apply endpoint.
Instead of forcing the API to wait for changes to apply, it is recommended to leave this parameter set to `true`
and periodically check the status of the changes using a `GET` request to the applicable apply endpoint.
!!! Warning
Setting this parameter to `false` may cause the API to hang if the changes take a long time to apply. In severe cases,
this may result in your API request receiving a timeout. It is recommended to use this parameter with caution and
only when necessary.
Setting this parameter to `false` may cause the API to hang if the changes take a long time to apply. In severe cases,
this may result in your API request receiving a timeout. It is recommended to use this parameter with caution and
only when necessary.

## apply

- Type: Boolean
- Default: `false`
- Description: This parameter allows you to control the default API behavior of _not_ applying changes to the pfSense
configuration immediately. Setting this parameter to `true` will force the API to apply the changes immediately. This
is useful when you are making a small change and want to apply it immediately without needing to make a separate API
call to the applicable apply endpoint. This may not be applicable to all endpoints as some always apply changes
is useful when you are making a small change and want to apply it immediately without needing to make a separate API
call to the applicable apply endpoint. This may not be applicable to all endpoints as some always apply changes
immediately.

## placement
Expand All @@ -40,5 +40,5 @@ parameters you can use:
force specific sorting attributes that may override this parameter.

!!! Warning
Use caution when setting placement of objects which may be sensitive to order such as firewall rules. Placing the
object in the wrong location may have unintended consequences such as blocking all traffic or allowing all traffic.
Use caution when setting placement of objects which may be sensitive to order such as firewall rules. Placing the
object in the wrong location may have unintended consequences such as blocking all traffic or allowing all traffic.
26 changes: 13 additions & 13 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Development and Contribution Guide

To get started with development on the pfSense REST API package, you will need to have a good understanding of the
package's structure, object-oriented PHP, and how to contribute to the project. This guide will help you get started
To get started with development on the pfSense REST API package, you will need to have a good understanding of the
package's structure, object-oriented PHP, and how to contribute to the project. This guide will help you get started
with the REST API package and provide you with the information you need to develop effectively.

## Project Structure
Expand All @@ -13,12 +13,12 @@ Below is a brief overview of the project's structure:

- `RESTAPI/`: The main namespace for the REST API package.
- `.resources/`: While not a PHP namespace, this directory contains a common place resources and tools:
- `cache/`: Contains the cache files for the REST API package. Cache files are typically JSON datasets that are
- `cache/`: Contains the cache files for the REST API package. Cache files are typically JSON datasets that are
populated by `\RESTAPI\Core\Cache` classes and are refreshed on the schedule defined in the class.
- `includes/`: Contains additional PHP libraries and classes required by the REST API package. Because pfSense does
not include a PHP package manager, these libraries are installed to this directory via composer when the package
is built.
- `scripts/`: Contains helper scripts for the REST API package. These scripts are used to automate tasks such as
not include a PHP package manager, these libraries are installed to this directory via composer when the package
is built.
- `scripts/`: Contains helper scripts for the REST API package. These scripts are used to automate tasks such as
generating OpenAPI documentation, running tests, and more.
- `Auth/`: Contains the \RESTAPI\Core\Auth child classes
- `Caches/`: Contains the \RESTAPI\Core\Cache child classes
Expand All @@ -32,24 +32,24 @@ Below is a brief overview of the project's structure:
- `Responses/`: Contains the \RESTAPI\Core\Response child classes
- `Tests/`: Contains the \RESTAPI\Core\TestCase child classes
- `Validators/`: Contains the \RESTAPI\Core\Validator child classes
- `autoloader.inc`: The autoloader script for the REST API package. This file can be included in your .php or .inc
- `autoloader.inc`: The autoloader script for the REST API package. This file can be included in your .php or .inc
file's `require_once` statements to automatically load all RESTAPI classes.

!!! Tip
The full PHP API reference for this package including documentation for all applicable classes can be
found [here](https://pfrest.org/php_reference/).
The full PHP API reference for this package including documentation for all applicable classes can be
found [here](https://pfrest.org/php_reference/).

## Style Guidelines

This projects uses opinionated code formatters to ensure a consistent code style across the project, this also ensures
that contributions are easier to review and maintain. The project uses the following code formatters:

- [Prettier](https://prettier.io) with the [PHP plugin](https://github.com/prettier/plugin-php) for PHP files
- From the project root, run `npm install` to install Prettier and the Prettier-PHP plugin.
- From the project root, run `./node_modules/.bin/prettier --write .` to format all files in the project.
- From the project root, run `npm install` to install Prettier and the Prettier-PHP plugin.
- From the project root, run `./node_modules/.bin/prettier --write .` to format all files in the project.
- [Black](https://black.readthedocs.io/en/stable/) for Python files
- From the project root, run `pip install -r requirements.txt` to install Black.
- From the project root, run `black .` to format all Python files in the project.
- From the project root, run `pip install -r requirements.txt` to install Black.
- From the project root, run `black .` to format all Python files in the project.

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion docs/GLOSSARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ Swagger is a set of tools for building, documenting, and consuming REST APIs. Sw
## Validator

A Validator is a class that is used to validate the data associated with a Field. Validators are used to ensure that the Field's value meets certain criteria before it is applied to the pfSense configuration. Validators can check for things like data type, length,
and format.
and format.
24 changes: 12 additions & 12 deletions docs/INSTALL_AND_CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Overall, the REST API package is designed to be as lightweight as possible and s
run pfSense. It's recommended to follow Netgate's [minimum hardware requirements](https://docs.netgate.com/pfsense/en/latest/hardware/minimum-requirements.html).

!!! Warning
While the package should behavior identically on architectures other than amd64, it has only been tested on amd64
builds of pfSense. Support on other architectures is not guaranteed.
While the package should behavior identically on architectures other than amd64, it has only been tested on amd64
builds of pfSense. Support on other architectures is not guaranteed.

### Supported pfSense versions

- pfSense CE 2.7.2 (amd64)
- pfSense Plus 24.03 (amd64)

!!! Tip
Don't see your version of pfSense? Older versions of pfSense may be supported by older versions of this package.
Check the [releases page](https://github.com/jaredhendrickson13/pfsense-api/releases).
Don't see your version of pfSense? Older versions of pfSense may be supported by older versions of this package.
Check the [releases page](https://github.com/jaredhendrickson13/pfsense-api/releases).

## Installing the package

Expand All @@ -30,13 +30,13 @@ pkg-static -C /dev/null add https://github.com/jaredhendrickson13/pfsense-api/re
```

!!! Note
You may need to customize the installation command to reference the package built for your pfSense version. Check
the [releases page](https://github.com/jaredhendrickson13/pfsense-api/releases) to find the package built for
your version of pfSense.
You may need to customize the installation command to reference the package built for your pfSense version. Check
the [releases page](https://github.com/jaredhendrickson13/pfsense-api/releases) to find the package built for
your version of pfSense.

!!! Important
When updating pfSense, **you must reinstall this package afterward** as pfSense removes unofficial packages during
system updates.
When updating pfSense, **you must reinstall this package afterward** as pfSense removes unofficial packages during
system updates.

## Configuring the package

Expand All @@ -53,8 +53,8 @@ pfsense-restapi delete
```

!!! Note
In the event that you are unable to use the `pfsense-restapi` command, you can manually remove the package by
running `pkg-static delete pfSense-pkg-RESTAPI`.
In the event that you are unable to use the `pfsense-restapi` command, you can manually remove the package by
running `pkg-static delete pfSense-pkg-RESTAPI`.

## Updating the package

Expand All @@ -70,4 +70,4 @@ If you need to revert or upgrade the package to a specific version, you can do s

```bash
pfsense-restapi revert <version>
```
```
14 changes: 7 additions & 7 deletions docs/QUERIES_AND_FILTERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Queries

Queries can be used to filter the data that is returned from the API based on specific criteria. Queries are passed as
query parameters in the URL and are formatted as `key=value`. Multiple queries can be passed in a single request by
Queries can be used to filter the data that is returned from the API based on specific criteria. Queries are passed as
query parameters in the URL and are formatted as `key=value`. Multiple queries can be passed in a single request by
separating them with an ampersand `&`. There are a couple of requirements for using queries with the REST API:

- Queries are only available for `GET` requests to endpoints.
Expand All @@ -22,8 +22,8 @@ query filter is specified.

- Name: `exact`
- Examples:
- `https://pfsense.example.com/api/v2/examples?fieldname=example`
- `https://pfsense.example.com/api/v2/examples?fieldname__exact=example`
- `https://pfsense.example.com/api/v2/examples?fieldname=example`
- `https://pfsense.example.com/api/v2/examples?fieldname__exact=example`

### Starts With (startswith)

Expand Down Expand Up @@ -85,10 +85,10 @@ fields.
## Pagination

Pagination can be used to limit the number of items returned in a single request. Pagination is controlled by two query
parameters: `limit` and `offset`. The `limit` parameter specifies the number of items to return, and the `offset`
parameters: `limit` and `offset`. The `limit` parameter specifies the number of items to return, and the `offset`
parameter specifies the starting index of the items to return. Pagination is useful when working with large datasets to
reduce the amount of data returned in a single request.

!!! Important
By default, the REST API does not paginate responses. If you want to paginate the response, you must include the
`limit` and `offset` query parameters in your request.
By default, the REST API does not paginate responses. If you want to paginate the response, you must include the
`limit` and `offset` query parameters in your request.
Loading

0 comments on commit 36f7e93

Please sign in to comment.