acato/ms-entra-guard

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

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

pkg:composer/acato/ms-entra-guard

0.3.0 2026-02-24 14:51 UTC

This package is auto-updated.

Last update: 2026-02-24 14:52:00 UTC


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.