Skip to content

Commit 96ef7e4

Browse files
author
Jared Hendrickson
committed
Updated contribution doc, started painful migration from function based API calls to object oriented design, small framework adjustments
1 parent 4db2f26 commit 96ef7e4

16 files changed

Lines changed: 398 additions & 563 deletions

File tree

CONTRIBUTING.md

Lines changed: 212 additions & 102 deletions
Large diffs are not rendered by default.

pfSense-pkg-API/Makefile

Lines changed: 2 additions & 430 deletions
Large diffs are not rendered by default.

pfSense-pkg-API/files/etc/inc/api.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ function get_carp_if_status() {
13041304
// Enables CARP interfaces
13051305
function enable_carp($enable) {
13061306
// Local variables
1307-
global $err_lib, $config;
1307+
global $config;
13081308
$vip_arr = $config['virtualip']['vip'];
13091309
$no_action = (is_carp_enabled() === $enable) ? true : false; // Check if a change is even requried
13101310
// Disable if $enable is false, enable if $enable is true

pfSense-pkg-API/files/etc/inc/api/api_calls/APIAccessToken.inc renamed to pfSense-pkg-API/files/etc/inc/api/api_models/APIAccessToken.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class APIAccessToken extends APIBaseModel {
2323
}
2424
}
2525

26-
# Override action subclass to create a JWT and return it to the user
26+
# Override action subclass to create a JWT and return it to the user after successful validation
2727
public function action() {
2828
$jwt = APITools\create_jwt($this->client->username);
2929
return APIResponse\get(0, ["token" => $jwt]);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
6+
class APIFirewallNat extends APIBaseModel {
7+
# Create our method constructor
8+
public function __construct() {
9+
parent::__construct();
10+
$this->methods = ["GET"];
11+
$this->validators = [];
12+
}
13+
14+
public function action() {
15+
global $config;
16+
// Check that we have a NAT configuration
17+
if (!empty($config["nat"])) {
18+
$this->validated_data = $config["nat"];
19+
}
20+
return APIResponse\get(0, $this->validated_data);
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
6+
class APIStatusCarp extends APIBaseModel {
7+
# Create our method constructor
8+
public function __construct() {
9+
parent::__construct();
10+
$this->privileges = ["page-all", "page-status-carp"];
11+
$this->methods = ["GET"];
12+
$this->validators = [];
13+
}
14+
15+
public function action() {
16+
global $config;
17+
$carp_status = [];
18+
$carp_status["enable"] = APITools\is_carp_enabled();
19+
$carp_status["maintenance_mode"] = isset($config["virtualip_carp_maintenancemode"]);
20+
$carp_status["interfaces"] = APITools\get_carp_if_status();
21+
return APIResponse\get(0, $carp_status);
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
6+
class APIStatusCarpModify extends APIBaseModel {
7+
# Create our method constructor
8+
public function __construct() {
9+
parent::__construct();
10+
$this->privileges = ["page-all", "page-status-carp"];
11+
$this->methods = ["POST"];
12+
$this->validators = [$this->validate_payload()];
13+
}
14+
15+
private function validate_payload() {
16+
// Check if user specified enable value
17+
if (isset($this->initial_data['enable'])) {
18+
// Check if value is true or false
19+
if (boolval($this->initial_data['enable'])) {
20+
$enable = true;
21+
} else {
22+
$enable = false;
23+
}
24+
$this->validated_data["enable"] = $enable;
25+
}
26+
// Check if user specified maintenance mode value
27+
if (isset($this->initial_data['maintenance_mode'])) {
28+
// Check if value is true or false
29+
if (boolval($this->initial_data['maintenance_mode'])) {
30+
$mm_enable = true;
31+
} else {
32+
$mm_enable = false;
33+
}
34+
$this->validated_data["maintenance_mode"] = $mm_enable;
35+
}
36+
}
37+
38+
public function action() {
39+
// Add our CARP settings
40+
$_SESSION["Username"] = $this->client->username;
41+
interfaces_carp_set_maintenancemode($this->validated_data["maintenance_mode"]);
42+
APITools\enable_carp($this->validated_data["enable"]);
43+
return APIResponse\get(0, $this->validated_data);
44+
}
45+
}

pfSense-pkg-API/files/etc/inc/api/framework/APIAuth.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class APIAuth {
6161

6262
# Attempts to authenticate using API token authentication
6363
private function authenticate_token() {
64-
if (APITools\authenticate_token($this->request["client-id"], $this->request["client-id"]) === true) {
64+
if (APITools\authenticate_token($this->request["client-id"], $this->request["client-token"]) === true) {
6565
$this->username = pack("H*", $this->request["client-id"]);
6666
// Ensure user is not disabled
6767
if (APITools\is_user_disabled($this->request["client-id"]) === false) {

pfSense-pkg-API/files/etc/inc/api/framework/APIBaseModel.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class APIBaseModel {
3131
# This function is intended to be overridden by an API model extended class
3232
# Any configuration writes, system configurations, etc should be added when overriding this base class
3333
# If this class is not overridden a 500 unexpected error is returned
34-
return APIResponse\get(1);
34+
return APIResponse\get(10);
3535
}
3636

3737
public function validate() {

pfSense-pkg-API/files/etc/inc/api/framework/APIResponse.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ function get($id, $data=[]) {
6565
"return" => $id,
6666
"message" => "Authentication mode must be set to JWT to enable access token authentication",
6767
],
68+
10 => [
69+
"status" => "server error",
70+
"code" => "500",
71+
"return" => $id,
72+
"message" => "Your API request was valid but no actions were specified for this endpoint",
73+
]
6874
];
6975

7076
$response = $responses[$id];

0 commit comments

Comments
 (0)