Skip to content

Commit be5e7a9

Browse files
author
Jared Hendrickson
committed
Working on OOP framework for API models
1 parent 058259e commit be5e7a9

5 files changed

Lines changed: 63 additions & 88 deletions

File tree

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
<?php
2-
require_once("api/APITools.inc");
3-
require_once("api/APIResponse.inc");
4-
require_once("api/APIAuth.inc");
5-
6-
class APIAccessToken {
7-
private $client;
8-
private $methods;
9-
private $req_privs;
10-
public $errors;
11-
public $valid_data;
2+
require_once("api/framework/APITools.inc");
3+
require_once("api/framework/APIBaseModel.inc");
4+
require_once("api/framework/APIResponse.inc");
5+
require_once("api/framework/APIAuth.inc");
126

7+
class APIAccessToken extends APIBaseModel {
138
# Create our method constructor
149
public function __construct() {
15-
$this->req_privs = [];
10+
parent::__construct();
1611
$this->methods = ["GET"];
17-
$this->client = new APIAuth($this->req_privs);
18-
$this->errors = [];
19-
$this->valid_data = [];
12+
$this->validators = ["validateAuthMode"];
2013
}
2114

2215
# Validate our API configurations auth mode (must be JWT)
@@ -29,53 +22,9 @@ class APIAccessToken {
2922
}
3023
}
3124

32-
# Validate our request
33-
public function validate($validate_auth=true, $validate_http_method=true) {
34-
# Validate authentication
35-
if ($validate_auth === true) {
36-
# Add error if user is not authenticated
37-
if (!$this->client->is_authenticated) {
38-
$this->errors[] = APIResponse\get(3);
39-
}
40-
# Add error if user is not authorized
41-
if (!$this->client->is_authorized) {
42-
$this->errors[] = APIResponse\get(4);
43-
}
44-
}
45-
46-
# Validate HTTP method
47-
if ($validate_http_method === true) {
48-
49-
}
50-
51-
# Run our field/conditional validators
52-
$this->validateAuthMode();
53-
54-
# Check if we have errors in our error array
55-
if (count($this->errors) === 0) {
56-
return true;
57-
} else {
58-
return false;
59-
}
60-
}
61-
62-
# Run our call. This method will return an assoc array containing the API response results
63-
public function call() {
64-
# Check if our request is valid
65-
if ($this->validate()) {
66-
$jwt = api_create_jwt($this->client->username);
67-
return ApiResponse\get(0, [["token" => $jwt]]);
68-
} else {
69-
return $this->errors[0];
70-
}
71-
}
72-
73-
# Listen for client requests. This method should executed on the API endpoint.
74-
public function listen() {
75-
# RUN API CALL
76-
$resp = $this->call();
77-
http_response_code($resp["code"]);
78-
echo json_encode($resp) . PHP_EOL;
79-
exit();
25+
# Override action subclass to create a JWT and return it to the user
26+
public function action() {
27+
$jwt = api_create_jwt($this->client->username);
28+
return APIResponse\get(0, ["token" => $jwt]);
8029
}
8130
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require_once("api/APITools.inc");
2+
require_once("api/framework/APITools.inc");
33

44

55
# Creates an object capable of verifying authentication and authorization based on the configuration
@@ -26,9 +26,8 @@ class APIAuth {
2626

2727
# AUTHENTICATION #
2828
# Attempts to authenticate using local database authentication
29-
private function authenticateLocalDatabase() {
29+
private function authenticate_local_database() {
3030
$this->username = $this->request["client-id"];
31-
var_dump(authenticate_user("admin", "pfsense"));
3231
// Authenticate against local database
3332
if (authenticate_user($this->username, $this->request["client-token"])) {
3433
// Ensure user is not disabled
@@ -42,7 +41,7 @@ class APIAuth {
4241
}
4342

4443
# Attempts to authenticate using JWT authentication
45-
private function authenticateJWT() {
44+
private function authenticate_jwt() {
4645
$auth_header = explode(" ", $_SERVER["HTTP_AUTHORIZATION"]);
4746
$token_type = $auth_header[0];
4847
$token = $auth_header[1];
@@ -62,7 +61,7 @@ class APIAuth {
6261
}
6362

6463
# Attempts to authenticate using API token authentication
65-
private function authenticateToken() {
64+
private function authenticate_token() {
6665
if (APITools\authenticate_token($this->request["client-id"], $this->request["client-id"]) === true) {
6766
$this->username = pack("H*", $this->request["client-id"]);
6867
// Ensure user is not disabled
@@ -79,13 +78,13 @@ class APIAuth {
7978
public function authenticate() {
8079
# Attempt to authenticate
8180
if ($this->auth_mode === "local") {
82-
$resp = $this->authenticateLocalDatabase();
81+
$resp = $this->authenticate_local_database();
8382
}
8483
elseif ($this->auth_mode === "jwt") {
85-
$resp = $this->authenticateJWT();
84+
$resp = $this->authenticate_jwt();
8685
}
8786
elseif ($this->auth_mode === "token") {
88-
$resp = $this->authenticateToken();
87+
$resp = $this->authenticate_token();
8988
}
9089
else {
9190
$resp = false;
Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
require_once("api/APITools.inc");
3-
require_once("api/APIResponse.inc");
4-
require_once("api/APIAuth.inc");
2+
require_once("api/framework/APITools.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
require_once("api/framework/APIAuth.inc");
55

66
class APIBaseModel {
77
public $client;
@@ -13,10 +13,9 @@ class APIBaseModel {
1313
public $methods;
1414
public $requires_auth;
1515

16-
1716
public function __construct() {
18-
$this->methods = ["POST"];
19-
$this->privileges = ["pages-all"];
17+
$this->methods = ["GET", "POST"];
18+
$this->privileges = ["page-all"];
2019
$this->client = new APIAuth($this->privileges);
2120
$this->requires_auth = true;
2221
$this->validators = [];
@@ -26,34 +25,62 @@ class APIBaseModel {
2625

2726
}
2827

29-
# Validate our request
30-
public function validate() {
31-
# Validate authentication
28+
private function check_authentication() {
3229
if ($this->requires_auth === true) {
33-
# Add error if user is not authenticated
3430
if (!$this->client->is_authenticated) {
3531
$this->errors[] = APIResponse\get(3);
3632
}
37-
# Add error if user is not authorized
38-
if (!$this->client->is_authorized) {
39-
$this->errors[] = APIResponse\get(4);
40-
}
4133
}
34+
}
4235

43-
# Validate HTTP method
36+
private function check_authorization() {
37+
if (!$this->client->is_authorized) {
38+
$this->errors[] = APIResponse\get(4);
39+
}
40+
}
41+
42+
private function check_method() {
4443
if (!in_array($_SERVER["REQUEST_METHOD"], $this->methods)) {
44+
$this->errors[] = APIResponse\get(2);
45+
}
46+
}
4547

48+
public function action() {
49+
# This function is intended to be overridden by an API model extended class
50+
# Any configuration writes, system configurations, etc should be added when overriding this base class
51+
# If this class is not overridden a 500 unexpected error is returned
52+
return APIResponse\get(1);
53+
}
54+
55+
public function validate() {
56+
$this->check_method();
57+
if ($this->requires_auth) {
58+
$this->check_authentication();
59+
$this->check_authorization();
4660
}
61+
$this->errors = array_merge($this->errors, $this->validators);
4762

48-
# Run our field/conditional validators
49-
$this->validateAuthMode();
5063

51-
# Check if we have errors in our error array
5264
if (count($this->errors) === 0) {
5365
return true;
5466
} else {
5567
return false;
5668
}
5769
}
5870

71+
public function call() {
72+
if ($this->validate()) {
73+
return $this->action();
74+
} else {
75+
return $this->errors[0];
76+
}
77+
}
78+
79+
public function listen() {
80+
$resp = $this->call();
81+
http_response_code($resp["code"]);
82+
echo json_encode($resp) . PHP_EOL;
83+
exit();
84+
}
85+
5986
}

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

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)