承接 jeffersongoncalves/laravel-zero-credentials 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jeffersongoncalves/laravel-zero-credentials

Composer 安装命令:

composer require jeffersongoncalves/laravel-zero-credentials

包简介

Reusable base for storing CLI authentication credentials locally in JSON (~/.<app>/config.json or XDG), with 0600 permissions and a typed credentials contract.

README 文档

README

laravel-zero-credentials

laravel-zero-credentials

Reusable base for storing CLI authentication credentials locally in JSON, with secure file permissions and a typed credentials contract.

Extracted from the bb-cli and jira-cli Laravel Zero tools, whose AuthService classes were ~90% identical. This package provides the shared persistence pipeline so each CLI only declares its own credential fields.

Why

Every CLI that authenticates against an API needs to:

  • resolve the user's home directory across platforms,
  • write credentials to ~/.<app>/config.json (or a custom/XDG path),
  • create the directory 0700 and the file 0600,
  • cache the loaded credentials in memory,
  • validate stored data before trusting it.

This package does all of that. The CLI supplies only a typed DTO and two small methods.

Installation

composer require jeffersongoncalves/laravel-zero-credentials

Requires PHP ^8.2. No other dependencies.

Usage

1. Implement the credentials contract

use JeffersonGoncalves\LaravelZero\Credentials\CredentialsContract;

final class Credentials implements CredentialsContract
{
    public function __construct(
        public readonly string $username,
        public readonly string $apiToken,
    ) {}

    public static function fromArray(array $data): static
    {
        return new static(
            username: $data['username'] ?? '',
            apiToken: $data['api_token'] ?? '',
        );
    }

    public function toArray(): array
    {
        return ['username' => $this->username, 'api_token' => $this->apiToken];
    }

    public function isValid(): bool
    {
        return $this->username !== '' && $this->apiToken !== '';
    }
}

2. Extend the abstract auth service

use JeffersonGoncalves\LaravelZero\Credentials\AbstractAuthService;
use JeffersonGoncalves\LaravelZero\Credentials\CredentialsContract;

final class AuthService extends AbstractAuthService
{
    protected function appName(): string
    {
        return 'bb-cli'; // => ~/.bb-cli/config.json
    }

    protected function fromArray(array $data): CredentialsContract
    {
        return Credentials::fromArray($data);
    }
}

3. Use it

$auth = new AuthService;

$auth->save(new Credentials('alice', 'secret-token'));

$auth->isAuthenticated();          // true
$auth->load()->username;           // 'alice'
$auth->getConfigPath();            // /home/alice/.bb-cli/config.json

$auth->forget();                   // deletes the file

Guarding commands

use JeffersonGoncalves\LaravelZero\Credentials\AuthenticationException;

if (! $auth->isAuthenticated()) {
    throw new AuthenticationException("Run 'bb auth:save' first.");
}

Custom / XDG config directory

Override configDir() to change where credentials live (the default is ~/.<appName>):

protected function configDir(): string
{
    $base = getenv('XDG_CONFIG_HOME') ?: $this->getHomeDir().'/.config';

    return $base.'/bb-cli';
}

Public API

Class Description
CredentialsContract Interface for a typed credentials DTO: fromArray(), toArray(), isValid().
AbstractAuthService Template method base: save(), load(), isAuthenticated(), forget(), getConfigPath(), getConfigDir(), getHomeDir(). Subclass defines appName() and fromArray(); override configDir() to customize the path.
AuthenticationException RuntimeException with a parametrizable message (default "Not authenticated.").

License

MIT

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固