Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .config/phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0"?>
<ruleset
xsi:noNamespaceSchemaLocation="https://schema.phpcodesniffer.com/phpcs.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="PDS Interop PHP Solid Server"
>
<description>PHP coding standards for the PDS Interop PHP Solid Server project</description>

<!-- Show sniff codes in all reports, and progress when running -->
<arg value="sp"/>

<!-- Strip the file paths down to the relevant bit. -->
<arg name="basepath" value="../"/>
<arg name="extensions" value="php"/>
<arg name="colors"/>

<file>.</file>
<exclude-pattern>*(.config|vendor|tests)/*</exclude-pattern>

<rule ref="PHPCompatibility"/>
<config name="testVersion" value="8.0-"/>

<!-- Set indent for `break` to 0 so it aligns with `case` and `default` -->
<rule ref="PSR2">
<exclude name="PSR2.ControlStructures.SwitchDeclaration"/>
<exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed"/>
<exclude name="PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace"/>
</rule>

<!-- Enforce the use of tabs for indentation -->
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>

<!-- Include the whole PSR-12 standard -->
<rule ref="PSR12">
<!-- Enforce the use of tabs for indentation -->
<exclude name="Generic.WhiteSpace.DisallowTabIndent.NonIndentTabsUsed"/>
<exclude name="Generic.WhiteSpace.DisallowTabIndent.TabsUsed"/>

<!-- Do not align multiple statements, to reduce noise in diffs -->
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSame"/>
</rule>

<!-- Have 12 chars padding maximum and always show as errors -->
<rule ref="Generic.Formatting.MultipleStatementAlignment">
<properties>
<property name="maxPadding" value="12"/>
<property name="error" value="true"/>
</properties>
</rule>
</ruleset>
24 changes: 24 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
root = true

[*.php]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
max_line_length = 120
spelling_language = en-US
tab_width = 4
trim_trailing_whitespace = true

# Unset settings for vendor directories
[{node_modules,vendor}/**]
charset = unset
end_of_line = unset
indent_size = unset
indent_style = unset
insert_final_newline = unset
max_line_length = unset
spelling_language = unset
tab_width = unset
trim_trailing_whitespace = unset
23 changes: 22 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ jobs:
--exclude vendor
--no-progress
.
# # 01.quality.php.validate.dependencies-file.yml

# 01.quality.php.validate.dependencies-file.yml
validate-dependencies-file:
name: Validate dependencies file
runs-on: ubuntu-24.04
Expand All @@ -63,6 +64,7 @@ jobs:
--no-plugins
--no-scripts
--strict

# 02.test.php.test-unit.yml
php-unittest:
name: PHP Unit Tests
Expand All @@ -89,6 +91,7 @@ jobs:
env:
COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.GITHUB_TOKEN }}"}}'
- run: vendor/bin/phpunit --configuration tests/phpunit/phpunit.xml

# 03.quality.php.scan.dependencies-vulnerabilities.yml
scan-dependencies-vulnerabilities:
name: Scan Dependencies Vulnerabilities
Expand All @@ -107,6 +110,24 @@ jobs:
--no-dev
--no-plugins
--no-scripts


# 03.quality.php.lint-quality.yml
php-lint-quality:
needs:
- lint-php-syntax
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: docker://pipelinecomponents/php-codesniffer
with:
args: >-
phpcs
--standard=.config/phpcs.xml.dist
--report-full
--report-summary
.

# 03.quality.php.lint-version-compatibility.yml
php-check-version-compatibility:
name: PHP Version Compatibility
Expand Down
79 changes: 43 additions & 36 deletions init.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,90 @@
<?php

// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols

require_once(__DIR__ . "/config.php");
require_once(__DIR__ . "/vendor/autoload.php");

use Pdsinterop\PhpSolid\Server;

function initKeys() {

function initKeys()
{
$keys = Server::generateKeySet();
file_put_contents(KEYDIR . "public.key", $keys['publicKey']);
file_put_contents(KEYDIR . "private.key", $keys['privateKey']);
file_put_contents(KEYDIR . "encryption.key", $keys['encryptionKey']);
}

function initDatabase() {
function initDatabase()
{
$statements = [
'CREATE TABLE IF NOT EXISTS clients (
'CREATE TABLE IF NOT EXISTS clients (
clientId VARCHAR(255) NOT NULL PRIMARY KEY,
origin TEXT NOT NULL,
clientData TEXT NOT NULL
)',
'CREATE TABLE IF NOT EXISTS allowedClients (
)',
'CREATE TABLE IF NOT EXISTS allowedClients (
userId VARCHAR(255) NOT NULL PRIMARY KEY,
clientId VARCHAR(255) NOT NULL
)',
'CREATE TABLE IF NOT EXISTS userStorage (
)',
'CREATE TABLE IF NOT EXISTS userStorage (
userId VARCHAR(255) NOT NULL PRIMARY KEY,
storageUrl VARCHAR(255) NOT NULL
)',
'CREATE TABLE IF NOT EXISTS verify (
)',
'CREATE TABLE IF NOT EXISTS verify (
code VARCHAR(255) NOT NULL PRIMARY KEY,
data TEXT NOT NULL
)',
'CREATE TABLE IF NOT EXISTS jti (
)',
'CREATE TABLE IF NOT EXISTS jti (
jti VARCHAR(255) NOT NULL PRIMARY KEY,
expires TEXT NOT NULL
)',
'CREATE TABLE IF NOT EXISTS users (
)',
'CREATE TABLE IF NOT EXISTS users (
user_id VARCHAR(255) NOT NULL PRIMARY KEY,
email TEXT NOT NULL,
password TEXT NOT NULL,
data TEXT
)',
'CREATE TABLE IF NOT EXISTS ipAttempts (
)',
'CREATE TABLE IF NOT EXISTS ipAttempts (
ip VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL,
expires TEXT NOT NULL
)',
)',
];

try {
$pdo = new \PDO("sqlite:" . DBPATH);
$pdo = new \PDO("sqlite:" . DBPATH);

// create tables
foreach($statements as $statement){
// create tables
foreach ($statements as $statement) {
$pdo->exec($statement);
}
} catch(\PDOException $e) {
echo $e->getMessage();
}
} catch (\PDOException $e) {
echo $e->getMessage();
}
}

function initStorageDatabase() {
function initStorageDatabase()
{
$statements = [
'CREATE TABLE IF NOT EXISTS storage (
'CREATE TABLE IF NOT EXISTS storage (
storage_id VARCHAR(255) NOT NULL PRIMARY KEY,
owner VARCHAR(255) NOT NULL
)'
)'
];

try {
$pdo = new \PDO("sqlite:" . DBPATH);
$pdo = new \PDO("sqlite:" . DBPATH);

// create tables
foreach($statements as $statement){
$pdo->exec($statement);
}
} catch(\PDOException $e) {
echo $e->getMessage();
// create tables
foreach ($statements as $statement) {
$pdo->exec($statement);
}
} catch (\PDOException $e) {
echo $e->getMessage();
}
}
initKeys();
initDatabase();
initStorageDatabase();
initStorageDatabase();
26 changes: 17 additions & 9 deletions lib/ClientRegistration.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact

namespace Pdsinterop\PhpSolid;

use Pdsinterop\PhpSolid\Db;

class ClientRegistration {
public static function getRegistration($clientId) {
class ClientRegistration
{
public static function getRegistration($clientId)
{
Db::connect();
$query = Db::$pdo->prepare(
'SELECT clientData FROM clients WHERE clientId=:clientId'
Expand All @@ -27,7 +32,8 @@ public static function getRegistration($clientId) {
return false;
}

public static function getRemoteRegistration($url) {
public static function getRemoteRegistration($url)
{
$clientDocument = file_get_contents($url);
$clientRegistration = json_decode($clientDocument, true);
if (!isset($clientRegistration['client_id'])) {
Expand All @@ -39,7 +45,8 @@ public static function getRemoteRegistration($url) {
return $clientRegistration;
}

public static function saveClientRegistration($clientData) {
public static function saveClientRegistration($clientData)
{
Db::connect();
if (!isset($clientData['client_name'])) {
$clientData['client_name'] = $clientData['origin'];
Expand All @@ -53,8 +60,9 @@ public static function saveClientRegistration($clientData) {
':clientData' => json_encode($clientData)
]);
}

public static function getClientByOrigin($origin) {

public static function getClientByOrigin($origin)
{
Db::connect();
$query = Db::$pdo->prepare(
'SELECT clientData FROM clients WHERE origin=:origin'
Expand All @@ -63,10 +71,10 @@ public static function getClientByOrigin($origin) {
':origin' => $origin
]);
$result = $query->fetchAll();
if (sizeof($result)=== 1) {

if (sizeof($result) === 1) {
return json_decode($result[0]['clientData'], true);
}
return false;
}
}
}
11 changes: 8 additions & 3 deletions lib/Db.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php

// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact

namespace Pdsinterop\PhpSolid;

class Db {

class Db
{
public static $pdo;
public static function connect() {
public static function connect()
{
if (!isset(self::$pdo)) {
self::$pdo = new \PDO("sqlite:" . DBPATH);
}
Expand Down
19 changes: 13 additions & 6 deletions lib/IpAttempts.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<?php

// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact

namespace Pdsinterop\PhpSolid;

use Pdsinterop\PhpSolid\Db;

class IpAttempts {
public static function logFailedAttempt($ip, $type, $expires) {
class IpAttempts
{
public static function logFailedAttempt($ip, $type, $expires)
{
if (in_array($ip, TRUSTED_IPS)) {
return;
}

Db::connect();

$query = Db::$pdo->prepare(
'INSERT INTO ipAttempts VALUES(:ip, :type, :expires)'
);
Expand All @@ -21,7 +26,8 @@ public static function logFailedAttempt($ip, $type, $expires) {
]);
}

public static function getAttemptsCount($ip, $type) {
public static function getAttemptsCount($ip, $type)
{
if (in_array($ip, TRUSTED_IPS)) {
return 0;
}
Expand All @@ -43,9 +49,10 @@ public static function getAttemptsCount($ip, $type) {
}
return 0;
}
public static function cleanupAttempts() {
public static function cleanupAttempts()
{
Db::connect();

$now = new \DateTime();
$query = Db::$pdo->prepare(
'DELETE FROM ipAttempts WHERE expires < :now'
Expand Down
Loading
Loading