承接 oremis/sentinel 相关项目开发

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

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

oremis/sentinel

Composer 安装命令:

composer require oremis/sentinel

包简介

OREMIS centralized token validation and ability system (Sentinel)

README 文档

README

Sentinel is a Laravel package designed for the OREMIS ecosystem. It provides a centralized mechanism for validating API tokens against the OREMIS Identity Provider (Data) and managing access control via abilities.

This package allows client applications (like App, Pio, etc.) to offload authentication and authorization logic to a central authority while maintaining high performance through local caching.

Features

  • Remote Token Validation: Validates Bearer tokens against the OREMIS Identity Provider.
  • Ability-Based Access Control: Checks if a token has the required permissions (abilities).
  • Context Awareness: Distinguishes between User tokens (user_id) and Service Account tokens (service_id).
  • Performance: Caches validation results locally to minimize network requests.
  • Laravel Integration: Provides Middleware, Facades, and a Service Provider for seamless integration.

Compatibility

  • Laravel: 10.x, 11.x, 12.x, 13.x
  • PHP: 8.3, 8.4, 8.5

Installation

Install the package via Composer:

composer require oremis/sentinel

The package will automatically register its Service Provider and Facade.

Configuration

Publish the configuration file to your application:

php artisan vendor:publish --tag=sentinel-config

This will create config/sentinel.php. You can configure the behavior using environment variables in your .env file:

# The base URL of the OREMIS Identity Provider
SENTINEL_BASE_URL=https://data.oremis.dev

# The endpoint used to validate tokens
SENTINEL_VALIDATE_ENDPOINT=/api/validate-token

# How long (in seconds) to cache the validation result
SENTINEL_CACHE_TTL=15

Middleware Registration (Laravel 11+)

In Laravel 11, you may need to manually register the middleware aliases in your bootstrap/app.php file to use them as string aliases in your routes.

// bootstrap/app.php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'remote.token'     => \Oremis\Sentinel\Middleware\CheckRemoteToken::class,
            'sentinel.ability' => \Oremis\Sentinel\Middleware\CheckAbility::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })
    ->create();

How It Works

  1. Incoming Request: An API request arrives at your application with a Authorization: Bearer <token> header.
  2. Middleware Interception: The remote.token middleware intercepts the request.
  3. Cache Check: It checks if the token's validity is already cached locally.
  4. Remote Validation: If not cached, it sends a request to the configured Identity Provider (SENTINEL_BASE_URL).
    • The IDP returns the token's validity, associated abilities, and the owner (User or Service).
  5. Context Injection: The middleware injects the abilities, token_user_id, and token_service_id into the request attributes.
  6. Authorization: The sentinel.ability middleware (if used) checks if the injected abilities match the route requirements.

Usage

1. Protecting Routes

Apply the middleware to your API routes. You typically need remote.token to validate the user, and optionally sentinel.ability to enforce permissions.

use Illuminate\Support\Facades\Route;

Route::middleware(['remote.token'])->group(function () {

    // Route accessible to any valid token
    Route::get('/profile', function () {
        // ...
    });

    // Route requiring specific abilities
    Route::middleware('sentinel.ability:ca.gdpr:create')->post('/gdpr-records', function () {
        // ...
    });

    // Route requiring ANY of the listed abilities
    Route::middleware('sentinel.ability:admin,editor')->group(function () {
        // ...
    });
});

2. Using the Facade

You can use the TokenAbility facade within your controllers or services to check permissions or retrieve context.

use Oremis\Sentinel\Facades\TokenAbility;

public function store()
{
    // Check for a specific ability
    if (TokenAbility::can('ca.gdpr:delete')) {
        // ...
    }

    // Enforce an ability (throws 403 if missing)
    TokenAbility::require('ca.gdpr:create');

    // Get the ID of the authenticated user (if it's a user token)
    $userId = TokenAbility::userId();

    // Get the ID of the service account (if it's a service token)
    $serviceId = TokenAbility::serviceId();

    // Get all abilities
    $abilities = TokenAbility::abilities();
}

3. Response Structure

The package expects the Identity Provider to return data in the following format:

{
  "data": {
    "valid": true,
    "abilities": ["ca.gdpr:create", "user.read"],
    "service_id": 2,
    "user_id": null
  }
}
  • valid: Boolean indicating if the token is active.
  • abilities: Array of permission strings.
  • service_id: Integer ID if the token belongs to a service account (otherwise null).
  • user_id: Integer ID if the token belongs to a user (otherwise null).

License

AGPL-3.0-or-later

oremis/sentinel 适用场景与选型建议

oremis/sentinel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.07k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 oremis/sentinel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 oremis/sentinel 我们能提供哪些服务?
定制开发 / 二次开发

基于 oremis/sentinel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2025-12-03