Search by

adachsoft / auth-json-file

Arkadiusz Adach

JSON-file storage adapters for authentication credentials and sessions, implementing adachsoft/auth-contracts.

Package info

gitlab.com/a.adach/auth-json-file

Issues

pkg:composer/adachsoft/auth-json-file

Statistics

Installs: 24

Dependents: 1

Suggesters: 0

Stars: 0

v0.2.0 2026-11-17 12:03 UTC

This package is auto-updated.

Last update: 2026-11-17 11:05:26 UTC


README

adachsoft/auth-json-file provides JSON-file implementations of the credential, session, and API key repositories defined by adachsoft/auth-contracts. It is intended for single-host applications that need simple persistent authentication storage.

Features

  • Stores credentials, sessions, and API key metadata in separate JSON files.
  • Persists password and session-token hashes; API key files contain only token hashes and their last four characters.
  • Uses file locks and atomic replacement to protect concurrent writes.
  • Rejects malformed stored records instead of silently accepting invalid data.

Requirements

  • PHP 8.3 or later.
  • adachsoft/auth-contracts ^0.2.

Installation

composer require adachsoft/auth-json-file

Usage

<?php

declare(strict_types=1);

use AdachSoft\AccessControlContracts\ValueObject\AccountId;
use AdachSoft\AuthContracts\Factory\AuthFactory;
use AdachSoft\AuthJsonFile\Repository\JsonApiKeyRepository;
use AdachSoft\AuthJsonFile\Repository\JsonCredentialRepository;
use AdachSoft\AuthJsonFile\Repository\JsonSessionRepository;

$credentials = new JsonCredentialRepository(__DIR__ . '/var/auth/credentials.json');
$sessions = new JsonSessionRepository(__DIR__ . '/var/auth/sessions.json');
$apiKeys = new JsonApiKeyRepository(__DIR__ . '/var/auth/api-keys.json');

$credentialManager = AuthFactory::createCredentialManager($credentials, $sessions);
$credentialManager->create(new AccountId('anna'), 'anna', 'a long password');

$authentication = AuthFactory::createAuthenticationService($credentials, $sessions);
$token = $authentication->login('anna', 'a long password')->token;
$accountId = $authentication->authenticate($token)->accountId;

Create the storage directory before use and make it writable by the application. Keep the JSON files outside the web root and out of version control: although passwords, session tokens, and API key tokens are stored as hashes (API key files also retain only the last four characters), the files contain sensitive authentication data. JSON file storage is intended for a single host; use another repository implementation when multiple hosts must share data.

See the usage examples for setup and storage details.

Documentation