Initial Version

This commit is contained in:
root
2025-12-21 10:09:54 -05:00
commit 4ae6befc7b
422 changed files with 47225 additions and 0 deletions

View File

@@ -0,0 +1,255 @@
<?php
declare(strict_types=1);
namespace KTXC\Models\Firewall;
use KTXF\Json\JsonDeserializable;
/**
* Represents a firewall access log entry for tracking blocked/allowed requests
*/
class FirewallLogObject implements \JsonSerializable, JsonDeserializable
{
public const RESULT_ALLOWED = 'allowed';
public const RESULT_BLOCKED = 'blocked';
public const EVENT_AUTH_FAILURE = 'auth_failure';
public const EVENT_RATE_LIMIT = 'rate_limit';
public const EVENT_BRUTE_FORCE = 'brute_force';
public const EVENT_SUSPICIOUS = 'suspicious';
public const EVENT_RULE_MATCH = 'rule_match';
public const EVENT_ACCESS_CHECK = 'access_check';
private ?string $id = null;
private ?string $tenantId = null;
private ?string $ipAddress = null;
private ?string $deviceFingerprint = null;
private ?string $userAgent = null;
private ?string $requestPath = null;
private ?string $requestMethod = null;
private ?string $eventType = null;
private ?string $result = null; // allowed, blocked
private ?string $ruleId = null; // Which rule triggered (if any)
private ?string $identityId = null; // User ID if authenticated
private ?\DateTimeImmutable $timestamp = null;
private ?array $metadata = null; // Additional context
public function jsonDeserialize(array|string $data): static
{
if (is_string($data)) {
$data = json_decode($data, true);
}
if (array_key_exists('_id', $data)) {
$this->id = $data['_id'] !== null ? (string)$data['_id'] : null;
} elseif (array_key_exists('id', $data)) {
$this->id = $data['id'] !== null ? (string)$data['id'] : null;
}
if (array_key_exists('tenantId', $data)) {
$this->tenantId = $data['tenantId'] !== null ? (string)$data['tenantId'] : null;
}
if (array_key_exists('ipAddress', $data)) {
$this->ipAddress = $data['ipAddress'] !== null ? (string)$data['ipAddress'] : null;
}
if (array_key_exists('deviceFingerprint', $data)) {
$this->deviceFingerprint = $data['deviceFingerprint'] !== null ? (string)$data['deviceFingerprint'] : null;
}
if (array_key_exists('userAgent', $data)) {
$this->userAgent = $data['userAgent'] !== null ? (string)$data['userAgent'] : null;
}
if (array_key_exists('requestPath', $data)) {
$this->requestPath = $data['requestPath'] !== null ? (string)$data['requestPath'] : null;
}
if (array_key_exists('requestMethod', $data)) {
$this->requestMethod = $data['requestMethod'] !== null ? (string)$data['requestMethod'] : null;
}
if (array_key_exists('eventType', $data)) {
$this->eventType = $data['eventType'] !== null ? (string)$data['eventType'] : null;
}
if (array_key_exists('result', $data)) {
$this->result = $data['result'] !== null ? (string)$data['result'] : null;
}
if (array_key_exists('ruleId', $data)) {
$this->ruleId = $data['ruleId'] !== null ? (string)$data['ruleId'] : null;
}
if (array_key_exists('identityId', $data)) {
$this->identityId = $data['identityId'] !== null ? (string)$data['identityId'] : null;
}
if (array_key_exists('timestamp', $data)) {
$this->timestamp = $data['timestamp'] !== null
? new \DateTimeImmutable($data['timestamp'])
: null;
}
if (array_key_exists('metadata', $data)) {
$this->metadata = $data['metadata'] !== null ? (array)$data['metadata'] : null;
}
return $this;
}
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'tenantId' => $this->tenantId,
'ipAddress' => $this->ipAddress,
'deviceFingerprint' => $this->deviceFingerprint,
'userAgent' => $this->userAgent,
'requestPath' => $this->requestPath,
'requestMethod' => $this->requestMethod,
'eventType' => $this->eventType,
'result' => $this->result,
'ruleId' => $this->ruleId,
'identityId' => $this->identityId,
'timestamp' => $this->timestamp?->format(\DateTimeInterface::ATOM),
'metadata' => $this->metadata,
];
}
// Getters and setters
public function getId(): ?string
{
return $this->id;
}
public function setId(?string $id): self
{
$this->id = $id;
return $this;
}
public function getTenantId(): ?string
{
return $this->tenantId;
}
public function setTenantId(?string $tenantId): self
{
$this->tenantId = $tenantId;
return $this;
}
public function getIpAddress(): ?string
{
return $this->ipAddress;
}
public function setIpAddress(?string $ipAddress): self
{
$this->ipAddress = $ipAddress;
return $this;
}
public function getDeviceFingerprint(): ?string
{
return $this->deviceFingerprint;
}
public function setDeviceFingerprint(?string $deviceFingerprint): self
{
$this->deviceFingerprint = $deviceFingerprint;
return $this;
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function setUserAgent(?string $userAgent): self
{
$this->userAgent = $userAgent;
return $this;
}
public function getRequestPath(): ?string
{
return $this->requestPath;
}
public function setRequestPath(?string $requestPath): self
{
$this->requestPath = $requestPath;
return $this;
}
public function getRequestMethod(): ?string
{
return $this->requestMethod;
}
public function setRequestMethod(?string $requestMethod): self
{
$this->requestMethod = $requestMethod;
return $this;
}
public function getEventType(): ?string
{
return $this->eventType;
}
public function setEventType(?string $eventType): self
{
$this->eventType = $eventType;
return $this;
}
public function getResult(): ?string
{
return $this->result;
}
public function setResult(?string $result): self
{
$this->result = $result;
return $this;
}
public function getRuleId(): ?string
{
return $this->ruleId;
}
public function setRuleId(?string $ruleId): self
{
$this->ruleId = $ruleId;
return $this;
}
public function getIdentityId(): ?string
{
return $this->identityId;
}
public function setIdentityId(?string $identityId): self
{
$this->identityId = $identityId;
return $this;
}
public function getTimestamp(): ?\DateTimeImmutable
{
return $this->timestamp;
}
public function setTimestamp(?\DateTimeImmutable $timestamp): self
{
$this->timestamp = $timestamp;
return $this;
}
public function getMetadata(): ?array
{
return $this->metadata;
}
public function setMetadata(?array $metadata): self
{
$this->metadata = $metadata;
return $this;
}
}

View File

@@ -0,0 +1,241 @@
<?php
declare(strict_types=1);
namespace KTXC\Models\Firewall;
use KTXF\Json\JsonDeserializable;
/**
* Represents a firewall rule for IP/device access control
*/
class FirewallRuleObject implements \JsonSerializable, JsonDeserializable
{
public const TYPE_IP = 'ip';
public const TYPE_IP_RANGE = 'ip_range';
public const TYPE_DEVICE = 'device';
public const ACTION_ALLOW = 'allow';
public const ACTION_BLOCK = 'block';
private ?string $id = null;
private ?string $tenantId = null;
private ?string $type = null; // ip, ip_range, device
private ?string $action = null; // allow, block
private ?string $value = null; // IP address, CIDR range, or device fingerprint
private ?string $reason = null; // Why this rule was created
private ?string $createdBy = null; // User ID who created the rule
private ?\DateTimeImmutable $createdAt = null;
private ?\DateTimeImmutable $expiresAt = null; // null = permanent
private bool $enabled = true;
private ?array $metadata = null; // Additional context (user agent, country, etc.)
public function jsonDeserialize(array|string $data): static
{
if (is_string($data)) {
$data = json_decode($data, true);
}
if (array_key_exists('_id', $data)) {
$this->id = $data['_id'] !== null ? (string)$data['_id'] : null;
} elseif (array_key_exists('id', $data)) {
$this->id = $data['id'] !== null ? (string)$data['id'] : null;
}
if (array_key_exists('tenantId', $data)) {
$this->tenantId = $data['tenantId'] !== null ? (string)$data['tenantId'] : null;
}
if (array_key_exists('type', $data)) {
$this->type = $data['type'] !== null ? (string)$data['type'] : null;
}
if (array_key_exists('action', $data)) {
$this->action = $data['action'] !== null ? (string)$data['action'] : null;
}
if (array_key_exists('value', $data)) {
$this->value = $data['value'] !== null ? (string)$data['value'] : null;
}
if (array_key_exists('reason', $data)) {
$this->reason = $data['reason'] !== null ? (string)$data['reason'] : null;
}
if (array_key_exists('createdBy', $data)) {
$this->createdBy = $data['createdBy'] !== null ? (string)$data['createdBy'] : null;
}
if (array_key_exists('createdAt', $data)) {
$this->createdAt = $data['createdAt'] !== null
? new \DateTimeImmutable($data['createdAt'])
: null;
}
if (array_key_exists('expiresAt', $data)) {
$this->expiresAt = $data['expiresAt'] !== null
? new \DateTimeImmutable($data['expiresAt'])
: null;
}
if (array_key_exists('enabled', $data)) {
$this->enabled = (bool)$data['enabled'];
}
if (array_key_exists('metadata', $data)) {
$this->metadata = $data['metadata'] !== null ? (array)$data['metadata'] : null;
}
return $this;
}
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'tenantId' => $this->tenantId,
'type' => $this->type,
'action' => $this->action,
'value' => $this->value,
'reason' => $this->reason,
'createdBy' => $this->createdBy,
'createdAt' => $this->createdAt?->format(\DateTimeInterface::ATOM),
'expiresAt' => $this->expiresAt?->format(\DateTimeInterface::ATOM),
'enabled' => $this->enabled,
'metadata' => $this->metadata,
];
}
/**
* Check if this rule has expired
*/
public function isExpired(): bool
{
if ($this->expiresAt === null) {
return false;
}
return $this->expiresAt < new \DateTimeImmutable();
}
/**
* Check if this rule is currently active (enabled and not expired)
*/
public function isActive(): bool
{
return $this->enabled && !$this->isExpired();
}
// Getters and setters
public function getId(): ?string
{
return $this->id;
}
public function setId(?string $id): self
{
$this->id = $id;
return $this;
}
public function getTenantId(): ?string
{
return $this->tenantId;
}
public function setTenantId(?string $tenantId): self
{
$this->tenantId = $tenantId;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getAction(): ?string
{
return $this->action;
}
public function setAction(?string $action): self
{
$this->action = $action;
return $this;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(?string $value): self
{
$this->value = $value;
return $this;
}
public function getReason(): ?string
{
return $this->reason;
}
public function setReason(?string $reason): self
{
$this->reason = $reason;
return $this;
}
public function getCreatedBy(): ?string
{
return $this->createdBy;
}
public function setCreatedBy(?string $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getExpiresAt(): ?\DateTimeImmutable
{
return $this->expiresAt;
}
public function setExpiresAt(?\DateTimeImmutable $expiresAt): self
{
$this->expiresAt = $expiresAt;
return $this;
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getMetadata(): ?array
{
return $this->metadata;
}
public function setMetadata(?array $metadata): self
{
$this->metadata = $metadata;
return $this;
}
}

View File

@@ -0,0 +1,156 @@
<?php
namespace KTXC\Models\Identity;
class User
{
private ?string $id = null;
private ?string $identity = null;
private ?string $label = null;
private ?array $roles = [];
private array $permissions = [];
private ?bool $enabled = null;
private ?string $provider = null;
private ?string $externalSubject = null;
private ?int $initialLogin = null;
private ?int $recentLogin = null;
public function populate(array $data, string $source): void
{
if ($source === 'users') {
$this->id = $data['uid'] ?? null; // 'uid' maps to 'id'
$this->identity = $data['identity'] ?? null;
$this->label = $data['label'] ?? null;
$this->roles = (array)($data['roles'] ?? []);
$this->enabled = $data['enabled'] ?? null;
$this->provider = $data['provider'] ?? null;
$this->externalSubject = $data['external_subject'] ?? null;
$this->initialLogin = $data['initial_login'] ?? null;
$this->recentLogin = $data['recent_login'] ?? null;
$this->permissions = (array)($data['permissions'] ?? []);
}
if ($source === 'jwt') {
$this->id = $data['identifier'] ?? null;
$this->identity = $data['identity'] ?? null;
$this->label = $data['label'] ?? null;
$this->roles = (array)($data['role'] ?? []);
$this->permissions = (array)($data['permissions'] ?? []);
$this->enabled = true;
}
if ($source === 'external') {
$this->identity = $data['identity'] ?? null;
$this->label = $data['label'] ?? null;
$this->externalSubject = $data['external_subject'] ?? null;
$this->provider = $data['provider'] ?? null;
}
}
public function getId(): ?string
{
return $this->id;
}
public function setId(string $value): void
{
$this->id = $value;
}
public function getIdentity(): ?string
{
return $this->identity;
}
public function setIdentity(string $value): void
{
$this->identity = $value;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $value): void
{
$this->label = $value;
}
public function getRoles(): array
{
return $this->roles;
}
public function setRoles(array $values): void
{
$this->roles = $values;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(?bool $value): void
{
$this->enabled = $value;
}
public function getProvider(): ?string
{
return $this->provider;
}
public function setProvider(?string $value): void
{
$this->provider = $value;
}
public function getExternalSubject(): ?string
{
return $this->externalSubject;
}
public function setExternalSubject(?string $value): void
{
$this->externalSubject = $value;
}
public function getInitialLogin(): ?int
{
return $this->initialLogin;
}
public function setInitialLogin(?int $value): void
{
$this->initialLogin = $value;
}
public function getRecentLogin(): ?int
{
return $this->recentLogin;
}
public function setRecentLogin(?int $value): void
{
$this->recentLogin = $value;
}
public function getPermissions(): array
{
return $this->permissions;
}
public function setPermissions(array $permissions): void
{
$this->permissions = $permissions;
}
public function hasPermission(string $permission): bool
{
return in_array($permission, $this->permissions, true);
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace KTXC\Models\Tenant;
use KTXF\Utile\Collection\CollectionAbstract;
class DomainCollection extends CollectionAbstract
{
public function __construct(array $items = [])
{
parent::__construct($items, CollectionAbstract::TYPE_STRING);
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace KTXC\Models\Tenant;
use KTXF\Json\JsonSerializableObject;
/**
* Tenant Configuration
*/
class TenantAuthentication extends JsonSerializableObject
{
protected array $providers = [];
protected int $methodsMinimal = 1;
public function providers(): array {
return $this->providers;
}
public function methodsMinimal(): int {
return $this->methodsMinimal;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace KTXC\Models\Tenant;
use KTXF\Utile\Collection\CollectionAbstract;
class TenantCollection extends CollectionAbstract
{
public function __construct(array $items = [])
{
parent::__construct($items, TenantObject::class, CollectionAbstract::TYPE_STRING);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace KTXC\Models\Tenant;
use KTXF\Json\JsonSerializableObject;
/**
* Tenant Configuration
*/
class TenantConfiguration extends JsonSerializableObject
{
protected TenantAuthentication $authentication;
protected TenantSecurity $security;
public function __construct()
{
$this->authentication = new TenantAuthentication();
$this->security = new TenantSecurity();
}
public function authentication(): TenantAuthentication {
return $this->authentication;
}
public function security(): TenantSecurity {
return $this->security;
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace KTXC\Models\Tenant;
use KTXF\Json\JsonSerializableObject;
/**
* Tenant entity representing a tenant
*/
class TenantObject extends JsonSerializableObject
{
private ?string $id = null;
private ?string $identifier = null;
private bool $enabled = false;
private ?string $label = null;
private ?string $description = null;
private ?DomainCollection $domains = null;
private ?TenantConfiguration $configuration = null;
/**
* Deserialize from associative array.
*/
public function jsonDeserialize(array|string $data): static
{
if (is_string($data)) {
$data = json_decode($data, true);
}
// Map only if key exists to avoid notices and allow partial input
if (array_key_exists('_id', $data)) $this->id = $data['_id'] !== null ? (string)$data['_id'] : null;
elseif (array_key_exists('id', $data)) $this->id = $data['id'] !== null ? (string)$data['id'] : null;
if (array_key_exists('identifier', $data)) $this->identifier = $data['identifier'] !== null ? (string)$data['identifier'] : null;
if (array_key_exists('enabled', $data)) $this->enabled = $data['enabled'] !== null ? (bool)$data['enabled'] : null;
if (array_key_exists('label', $data)) $this->label = $data['label'] !== null ? (string)$data['label'] : null;
if (array_key_exists('description', $data)) $this->description = $data['description'] !== null ? (string)$data['description'] : null;
if (array_key_exists('domains', $data)) {
$this->domains = (new DomainCollection((array)$data['domains']));
}
if (array_key_exists('configuration', $data)) {
$this->configuration = (new TenantConfiguration)->jsonDeserialize($data['configuration']);
}
return $this;
}
/**
* Serialize to JSON-friendly structure.
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'identifier' => $this->identifier,
'enabled' => $this->enabled,
'label' => $this->label,
'description' => $this->description,
'domains' => $this->domains,
'configuration' => $this->configuration,
];
}
public function getId(): ?string
{
return $this->id;
}
public function setId(string $value): self
{
$this->id = $value;
return $this;
}
public function getIdentifier(): ?string
{
return $this->identifier;
}
public function setIdentifier(string $value): self
{
$this->identifier = $value;
return $this;
}
public function getEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $value): self
{
$this->enabled = $value;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $value): self
{
$this->label = $value;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $value): self
{
$this->description = $value;
return $this;
}
public function getDomains(): ?DomainCollection
{
return $this->domains;
}
public function setDomains(DomainCollection $value): self
{
$this->domains = $value;
return $this;
}
public function getConfiguration(): TenantConfiguration
{
return $this->configuration;
}
public function setConfiguration(TenantConfiguration $value): self
{
$this->configuration = $value;
return $this;
}
public function getSettings(): array
{
return $this->configuration['settings'] ?? [];
}
public function setSettings(array $value): self
{
$this->configuration['settings'] = $value;
return $this;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace KTXC\Models\Tenant;
use KTXF\Json\JsonSerializableObject;
/**
* Tenant Configuration
*/
class TenantSecurity extends JsonSerializableObject
{
protected string $code = '';
public function __construct()
{
$this->code = uniqid();
}
public function code(): string {
return $this->code;
}
}