Skip to content

Commit 4a2bdd1

Browse files
author
Jared Hendrickson
committed
Completed transition to OOP, starting docs and tests
1 parent 66a1656 commit 4a2bdd1

32 files changed

Lines changed: 1831 additions & 180 deletions

README.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
---
22
# Introduction
3-
pfSense API is a fast, safe, full-fledged HTTP API. This works by leveraging the same PHP functions and processes used by pfSense's webConfigurator into API endpoints to create, read, update and delete pfSense configurations. All API endpoints enforce input validation to prevent invalid configurations from being made. Configurations made via API are properly written to the master XML configuration and the correct backend configurations are made preventing the need for a reboot. All this results in the fastest, safest, and easiest way to automate pfSense!
3+
pfSense API is a fast, safe, full-fledged HTTP API. This works by leveraging the same PHP functions and processes used
4+
by pfSense's webConfigurator into API endpoints to create, read, update and delete pfSense configurations. All API
5+
endpoints enforce input validation to prevent invalid configurations from being made. Configurations made via API are
6+
properly written to the master XML configuration and the correct backend configurations are made preventing the need for
7+
a reboot. All this results in the fastest, safest, and easiest way to automate pfSense!
48

59
# Installation
610
To install pfSense API, simply run the following command from the pfSense shell:<br>
7-
`pkg add https://github.com/jaredhendrickson13/pfsense-api/releases/download/v0.0.2/pfSense-2-4-pkg-API-0.0_2.txz`<br>
11+
`pkg add https://github.com/jaredhendrickson13/pfsense-api/releases/download/v0.0.3/pfSense-2-4-pkg-API-0.0_3.txz`<br>
812

913
To uninstall, run the following command:<br>
1014
`pkg delete pfSense-pkg-API`<br>
1115

12-
_Note: if you do not have shell access to pfSense, you can still install via the webConfigurator by navigating to 'Diagnostics > Command Prompt' and enter the commands there_
16+
_Note: if you do not have shell access to pfSense, you can still install via the webConfigurator by navigating to
17+
'Diagnostics > Command Prompt' and enter the commands there_
1318

1419
# Requirements
1520
- pfSense 2.4.4 or later is supported
16-
- pfSense API requires a local user account in pfSense. The same permissions required to make configurations in the webConfigurator are required to make calls to the API endpoints
17-
- While not an enforced requirement, it is STRONGLY recommended that you configure pfSense to use HTTPS instead of HTTP. This ensures that login credentials and/or API tokens remain secure in-transit
21+
- pfSense API requires a local user account in pfSense. The same permissions required to make configurations in the
22+
webConfigurator are required to make calls to the API endpoints
23+
- While not an enforced requirement, it is STRONGLY recommended that you configure pfSense to use HTTPS instead of HTTP.
24+
This ensures that login credentials and/or API tokens remain secure in-transit
1825

1926
# Authentication
20-
By default, pfSense API uses the same credentials as the webConfigurator. This behavior allows you to configure pfSense from the API out of the box, and user passwords may be changed from the API to immediately add additional security if needed. Alternatively, you can configure pfSense API to create secure API client IDs and tokens for API users. To generate, or delete API keys you can navigate to `System > API` in the UI after installation, and change the authentication mode to `API Token`.
27+
By default, pfSense API uses the same credentials as the webConfigurator. This behavior allows you to configure pfSense
28+
from the API out of the box, and user passwords may be changed from the API to immediately add additional security if
29+
needed. After installation, you can navigate to System > API in the pfSense webConfigurator to configure API
30+
authentication.
31+
32+
To authenticate your API call, follow the instructions for your configured authentication mode:
33+
34+
- Local Database (default) : Uses the same credentials as the pfSense webConfigurator. To authenticate API calls, simply
35+
add a `client-id` value containing your username and a `client-token` value containing your password to your payload. For
36+
example `{"client-id": "admin", "client-token": "pfsense"}`
37+
38+
- JWT : Requires a bearer token to be included in the `Authorization` header of your request. To receive a bearer token,
39+
you may make a POST request to /api/v1/access_token/ and include a `client-id` value containing your pfSense username
40+
and a `client-token` value containing your pfSense password to your payload. For example
41+
`{"client-id": "admin", "client-token": "pfsense"}`. Once you have your bearer token, you can authenticate your API
42+
call by adding it to the request's authorization header. (e.g. `Authorization: Bearer xxxxxxxx.xxxxxxxxx.xxxxxxxx`)
43+
44+
- API Token : Uses standalone tokens generated via the UI. These are better suited to distribute to systems as they are
45+
revocable and will only allow API authentication and not UI or SSH authentication (like the local database credentials).
46+
To generate or revoke credentials, navigate to System > API within the UI and ensure the Authentication Mode is set to
47+
API token. Then you should have the options to configure API Token generation, generate new tokens, and revoke existing
48+
tokens. Once you have your API token, you may authenticate your API call by adding a `client-id` value containing your
49+
API token client ID and a `client-token` value containing your API token client token to your payload.
50+
(e.g. `{"client-id": "cccdj-311s", "client-token": "42jkjl-k234jlk1b38123kj3kjl-ffwzzuilaei"}`
2151

2252
# Response Codes
2353
`200 (OK)` : API call succeeded<br>
2454
`400 (Bad Request)` : An error was found within your requested parameters<br>
25-
`401 (Unauthorized)` : API client has not completed authentiation or authorization successfully<br>
55+
`401 (Unauthorized)` : API client has not completed authentication or authorization successfully<br>
2656
`403 (Forbidden)` : The API endpoint has refused your call. Commonly due to your access settings found in `System > API`<br>
2757
`404 (Not found)` : Either the API endpoint or requested data was not found<br>
2858
`500 (Server error)` : The API endpoint encountered an unexpected error processing your API request<br>
2959

3060
# Error Codes
31-
A full list of error codes can be found by navigating to /api/v1/system/api/errors/ after installation. This will return JSON data containing each error code and their corresponding error message. No authentication is required to view the error code library. This also makes API integration with third-party software easy as the API error codes and messages are always just an HTTP call away!
61+
A full list of error codes can be found by navigating to /api/v1/system/api/errors/ after installation. This will return
62+
JSON data containing each error code and their corresponding error message. No authentication is required to view the
63+
error code library. This also makes API integration with third-party software easy as the API error codes and messages
64+
are always just an HTTP call away!
3265

3366
# Rate limit
34-
There is no limit to API calls at this time
67+
There is no limit to API calls at this time. Alternatively, you enable the API in read-only mode to only allow endpoints
68+
with read access within System > API.
File renamed without changes.

pfSense-pkg-API/docs/index.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
# Introduction
3+
pfSense API is a fast, safe, full-fledged HTTP API. This works by leveraging the same PHP functions and processes used
4+
by pfSense's webConfigurator into API endpoints to create, read, update and delete pfSense configurations. All API
5+
endpoints enforce input validation to prevent invalid configurations from being made. Configurations made via API are
6+
properly written to the master XML configuration and the correct backend configurations are made preventing the need for
7+
a reboot. All this results in the fastest, safest, and easiest way to automate pfSense!
8+
9+
# Installation
10+
To install pfSense API, simply run the following command from the pfSense shell:<br>
11+
`pkg add https://github.com/jaredhendrickson13/pfsense-api/releases/download/v0.0.3/pfSense-2-4-pkg-API-0.0_3.txz`<br>
12+
13+
To uninstall, run the following command:<br>
14+
`pkg delete pfSense-pkg-API`<br>
15+
16+
_Note: if you do not have shell access to pfSense, you can still install via the webConfigurator by navigating to
17+
'Diagnostics > Command Prompt' and enter the commands there_
18+
19+
# Requirements
20+
- pfSense 2.4.4 or later is supported
21+
- pfSense API requires a local user account in pfSense. The same permissions required to make configurations in the
22+
webConfigurator are required to make calls to the API endpoints
23+
- While not an enforced requirement, it is STRONGLY recommended that you configure pfSense to use HTTPS instead of HTTP.
24+
This ensures that login credentials and/or API tokens remain secure in-transit
25+
26+
# Authentication
27+
By default, pfSense API uses the same credentials as the webConfigurator. This behavior allows you to configure pfSense
28+
from the API out of the box, and user passwords may be changed from the API to immediately add additional security if
29+
needed. After installation, you can navigate to System > API in the pfSense webConfigurator to configure API
30+
authentication.
31+
32+
To authenticate your API call, follow the instructions for your configured authentication mode:
33+
34+
- Local Database (default) : Uses the same credentials as the pfSense webConfigurator. To authenticate API calls, simply
35+
add a `client-id` value containing your username and a `client-token` value containing your password to your payload. For
36+
example `{"client-id": "admin", "client-token": "pfsense"}`
37+
38+
- JWT : Requires a bearer token to be included in the `Authorization` header of your request. To receive a bearer token,
39+
you may make a POST request to /api/v1/access_token/ and include a `client-id` value containing your pfSense username
40+
and a `client-token` value containing your pfSense password to your payload. For example
41+
`{"client-id": "admin", "client-token": "pfsense"}`. Once you have your bearer token, you can authenticate your API
42+
call by adding it to the request's authorization header. (e.g. `Authorization: Bearer xxxxxxxx.xxxxxxxxx.xxxxxxxx`)
43+
44+
- API Token : Uses standalone tokens generated via the UI. These are better suited to distribute to systems as they are
45+
revocable and will only allow API authentication and not UI or SSH authentication (like the local database credentials).
46+
To generate or revoke credentials, navigate to System > API within the UI and ensure the Authentication Mode is set to
47+
API token. Then you should have the options to configure API Token generation, generate new tokens, and revoke existing
48+
tokens. Once you have your API token, you may authenticate your API call by adding a `client-id` value containing your
49+
API token client ID and a `client-token` value containing your API token client token to your payload.
50+
(e.g. `{"client-id": "cccdj-311s", "client-token": "42jkjl-k234jlk1b38123kj3kjl-ffwzzuilaei"}`
51+
52+
# Response Codes
53+
`200 (OK)` : API call succeeded<br>
54+
`400 (Bad Request)` : An error was found within your requested parameters<br>
55+
`401 (Unauthorized)` : API client has not completed authentication or authorization successfully<br>
56+
`403 (Forbidden)` : The API endpoint has refused your call. Commonly due to your access settings found in `System > API`<br>
57+
`404 (Not found)` : Either the API endpoint or requested data was not found<br>
58+
`500 (Server error)` : The API endpoint encountered an unexpected error processing your API request<br>
59+
60+
# Error Codes
61+
A full list of error codes can be found by navigating to /api/v1/system/api/errors/ after installation. This will return
62+
JSON data containing each error code and their corresponding error message. No authentication is required to view the
63+
error code library. This also makes API integration with third-party software easy as the API error codes and messages
64+
are always just an HTTP call away!
65+
66+
# Rate limit
67+
There is no limit to API calls at this time. Alternatively, you enable the API in read-only mode to only allow endpoints
68+
with read access within System > API.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
6+
class APIInterfaces extends APIBaseModel {
7+
# Create our method constructor
8+
public function __construct() {
9+
parent::__construct();
10+
$this->methods = ["GET"];
11+
$this->privileges = ["page-all", "page-interfaces-assignnetworkports"];
12+
13+
}
14+
15+
public function action() {
16+
return APIResponse\get(0, $this->config["interfaces"]);
17+
}
18+
}

0 commit comments

Comments
 (0)