marick/laravel-google-cloud-iap
Composer 安装命令:
composer require marick/laravel-google-cloud-iap
包简介
README 文档
README
Companion packages: Cloud Logging, Cloud Scheduler, Cloud Tasks
Introduction
This package integrates Google Cloud Identity-Aware Proxy (IAP) with Laravel's authentication system.
When IAP is enabled on a Cloud Run service or App Engine application, Google intercepts every request and requires the user to authenticate with their Google account. Authenticated requests are forwarded to your app with a signed JWT header (X-Goog-IAP-JWT-Assertion) containing the user's identity.
This package verifies that JWT and exposes the user via Auth::user(), so you can access the logged-in user anywhere in your Laravel application without building your own login system.
This package requires Laravel 12 or 13.
Note: If your app uses database sessions, the
user_idcolumn in thesessionstable must be avarcharinstead of the defaultbigint, as IAP user identifiers are strings. Run a migration to change the column type before using this package.
Installation
Install the package with Composer:
composer require marick/laravel-google-cloud-iap
Register the iap guard in config/auth.php:
'guards' => [ 'iap' => [ 'driver' => 'iap', ], ],
Set it as your default guard, or use it explicitly per route:
// Set as default 'defaults' => [ 'guard' => 'iap', ],
How to
Access the authenticated user
use Illuminate\Support\Facades\Auth; Auth::user()->email; // john@company.com Auth::user()->sub; // accounts.google.com:123456789 Auth::user()->domain; // company.com Auth::check(); // true when the IAP header is present and valid Auth::guest(); // true when unauthenticated Auth::id(); // returns the sub claim
The user is an IapUser value object — it has no database backing. Google manages your users; this package just reads what IAP tells you.
Protect routes
Use the iap middleware alias instead of auth. This is necessary because IAP has no login page to redirect to — unauthenticated requests return a 401 response instead.
Route::middleware('iap:iap')->group(function () { Route::get('/dashboard', DashboardController::class); });
The iap alias is registered automatically by the package's service provider.
Restrict by domain or email
Pass allowed domains or email addresses after the guard name. Users that match none of them receive a 403 response.
// Single domain Route::middleware('iap:iap,company.com')->group(function () { Route::get('/dashboard', DashboardController::class); }); // Multiple domains Route::middleware('iap:iap,company.com,partner.com')->group(function () { Route::get('/dashboard', DashboardController::class); }); // Specific email Route::middleware('iap:iap,john@company.com')->group(function () { Route::get('/dashboard', DashboardController::class); }); // Mixed domains and emails Route::middleware('iap:iap,company.com,john@partner.com')->group(function () { Route::get('/dashboard', DashboardController::class); });
Blade directives
@iapauth Hello, {{ Auth::user()->email }}! @endiapauth @iapauth('company.com') You are from company.com. @endiapauth @iapauth('john@company.com') Hello, John! @endiapauth @iapauth('company.com', 'john@partner.com') You are from an allowed domain or a specific user. @endiapauth @iapguest You are not logged in. @endiapguest
Validate the audience claim
IAP signs JWTs with an audience claim (aud) specific to your backend service. Validating it prevents tokens issued for one service from being accepted by another. Set it via the environment:
IAP_AUDIENCE=/projects/123456789/global/backendServices/456789123
For App Engine the format is /projects/PROJECT_NUMBER/apps/PROJECT_ID.
Leave it unset to skip audience validation (fine for single-service setups).
Testing
Act as a user
use Marick\LaravelGoogleCloudIap\CloudIAP; CloudIAP::actingAs('john@company.com'); $this->assertTrue(Auth::check()); $this->assertSame('john@company.com', Auth::user()->email); $this->assertSame('company.com', Auth::user()->domain);
Provide a custom sub if your tests need a specific identifier:
CloudIAP::actingAs('john@company.com', 'accounts.google.com:12345');
Test unauthenticated behaviour
CloudIAP::fake(); $this->assertNull(Auth::user()); $this->assertTrue(Auth::guest());
fake() also ensures that any X-Goog-IAP-JWT-Assertion header present during a test is ignored — no HTTP calls are made to Google's certificate endpoint.
marick/laravel-google-cloud-iap 适用场景与选型建议
marick/laravel-google-cloud-iap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 marick/laravel-google-cloud-iap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marick/laravel-google-cloud-iap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-14