定制 acato/ms-entra-guard 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

acato/ms-entra-guard

最新稳定版本:0.3.0

Composer 安装命令:

composer require acato/ms-entra-guard

包简介

Laravel authentication guard for Microsoft Entra ID (Azure AD) JWT bearer tokens

README 文档

README

A Laravel authentication guard for validating Microsoft Entra ID (Azure AD) JWT bearer tokens.

Requirements

  • PHP 8.2+
  • Laravel 11.0+

Installation

composer require acato/ms-entra-guard

The service provider is auto-discovered. Publish the config file:

php artisan vendor:publish --tag=entra-guard-config

Configuration

1. Add a guard to config/auth.php

'guards' => [
    // ...
    'api' => [
        'driver' => 'entra',
        'provider' => null,
    ],
],

2. Configure trusted providers in config/entra-guard.php

'providers' => [
    [
        'iss' => 'https://login.microsoftonline.com/{tenant-id}/v2.0',
        'alg' => \Lcobucci\JWT\Signer\Rsa\Sha256::class,
        'claims' => [
            'aud' => 'your-client-id',
        ],
    ],
],

3. Implement the interface on your User model

use Acato\EntraGuard\Contracts\ResolvesFromEntraToken;
use Acato\EntraGuard\Concerns\ResolvesEntraUser;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements ResolvesFromEntraToken
{
    use ResolvesEntraUser;
}

The ResolvesEntraUser trait provides a default updateOrCreate implementation. Customize the field mapping with static properties:

class User extends Authenticatable implements ResolvesFromEntraToken
{
    use ResolvesEntraUser;

    // Map DB columns to JWT claims for the unique key
    protected static array $entraUniqueBy = [
        'uuid' => 'oid',
    ];

    // Map DB columns to JWT claims for update values
    // Use '|' for fallback (tries left-to-right), null inserts now()
    protected static array $entraUpdateAttributes = [
        'name' => 'name',
        'email' => 'email|upn',
        'last_login_at' => null,
    ];
}

Alternative: custom user resolver

Instead of implementing the interface, you can provide a closure in the config:

'user_resolver' => function (array $payload) {
    return \App\Models\User::where('azure_id', $payload['oid'])->first();
},

Usage

Protect routes with the guard:

Route::middleware('auth:api')->group(function () {
    Route::get('/me', fn () => auth()->user());
});

Testing

Use actingAs in your tests:

use Acato\EntraGuard\EntraGuard;

EntraGuard::actingAs($user, 'api');

Config Options

KeyTypeDefaultDescription
modelclass-stringApp\Models\UserEloquent model implementing ResolvesFromEntraToken
user_resolver?ClosurenullCustom resolver (takes precedence over model)
token_cache_ttlint10Minutes to cache decoded tokens
key_cache_ttlint4Hours to cache public signing keys
timezonestringapp timezoneTimezone for JWT time validation
providersarray[]Trusted issuer configurations

License

Apache-2.0. See LICENSE for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2026-02-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固