onelabs/xguard-client
Composer 安装命令:
composer require onelabs/xguard-client
包简介
Laravel client SDK for x-guard OAuth 2.0 / OpenID Connect Identity Provider. Drop-in Socialite provider with default routes, controller, and Blade button component.
README 文档
README
Laravel client SDK for x-guard — an OAuth 2.0 / OpenID Connect Identity Provider.
Drop in a Socialite provider, default routes, and a Blade login button. Get SSO into your Laravel app in 5 minutes.
composer require onelabs/xguard-client
What it does
- Registers
Socialite::driver('xguard')— same API you already know. - Provides default
/auth/xguard/redirect&/auth/xguard/callbackroutes (opt-out). - Resolves SSO users to your local
Usermodel (override viaUserResolverinterface). - Ships a Blade component
<x-xguard-login-button />for instant UI.
Requirements
- PHP 8.2+
- Laravel 10, 11, or 12
- A running x-guard server with an App registered for your project (see Developer Portal Setup)
Installation
1. Install the package
composer require onelabs/xguard-client
2. Add credentials to .env
XGUARD_ISSUER=https://x-guard.example.com XGUARD_CLIENT_ID=xg_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx XGUARD_CLIENT_SECRET=PASTE_PLAIN_SECRET_HERE XGUARD_REDIRECT_URI="${APP_URL}/auth/xguard/callback"
The
XGUARD_REDIRECT_URIvalue must exactly match a Redirect URI registered for your App in the x-guard Developer Portal — including scheme, host, port, and path.
3. Add to config/services.php
'xguard' => [ 'issuer' => env('XGUARD_ISSUER'), 'client_id' => env('XGUARD_CLIENT_ID'), 'client_secret' => env('XGUARD_CLIENT_SECRET'), 'redirect' => env('XGUARD_REDIRECT_URI'), ],
4. Publish the migration & run it
The package adds an sso_user_id column to your users table (used to link local users to x-guard).
php artisan vendor:publish --tag=xguard-migrations php artisan migrate
The migration is idempotent: it skips if the column already exists.
5. Add the login button to your login view
<x-xguard-login-button />
That's it. The button points to the default /auth/xguard/redirect route, which handles the entire OAuth flow.
Customization
Publish the config (optional)
php artisan vendor:publish --tag=xguard-config
This creates config/xguard.php where you can change defaults: scopes, route prefix, redirect-after-login, lookup column, button text, etc.
Customize the button label/style
<x-xguard-login-button label="Masuk via SSO" class="btn btn-lg btn-primary w-100" /> <x-xguard-login-button :icon="false" />
Publish the button view (full markup control)
php artisan vendor:publish --tag=xguard-views
# edits resources/views/vendor/xguard/components/login-button.blade.php
Custom user resolver
Need different linking logic (multi-table user storage, role assignment, domain whitelist)?
Implement the UserResolver contract:
// app/Auth/MyXGuardResolver.php namespace App\Auth; use Onelabs\XGuardClient\Contracts\UserResolver; use Illuminate\Contracts\Auth\Authenticatable; use Laravel\Socialite\Two\User as SocialiteUser; class MyXGuardResolver implements UserResolver { public function resolve(SocialiteUser $ssoUser): Authenticatable { // Reject users from non-allowed domains, for example: if (!str_ends_with((string) $ssoUser->getEmail(), '@mycompany.com')) { throw new \DomainException('Hanya email @mycompany.com yang diizinkan.'); } // ... your custom lookup / create logic ... return \App\Models\User::firstOrCreate( ['sso_user_id' => $ssoUser->getId()], ['name' => $ssoUser->getName(), 'email' => $ssoUser->getEmail()], ); } }
Bind it in AppServiceProvider::register():
$this->app->bind( \Onelabs\XGuardClient\Contracts\UserResolver::class, \App\Auth\MyXGuardResolver::class, );
Disable default routes (write your own)
Set XGUARD_USE_DEFAULT_ROUTES=false (or in published config: 'use_default_routes' => false), then write your own controller using the Socialite driver:
public function redirect() { return Socialite::driver('xguard')->redirect(); } public function callback(Request $request) { $ssoUser = Socialite::driver('xguard')->user(); // ... your logic }
Custom scopes per request
Socialite::driver('xguard') ->scopes(['openid', 'profile', 'email', 'phone', 'offline_access']) ->redirect();
Available scopes: openid (required), profile, email, phone, offline_access (triggers refresh token).
Getting credentials from x-guard
You need 4 values from your x-guard developer:
| Value | Where it comes from |
|---|---|
XGUARD_ISSUER |
The x-guard server URL (e.g. https://x-guard.example.com) |
XGUARD_CLIENT_ID |
Developer Portal → your App → Overview tab |
XGUARD_CLIENT_SECRET |
Developer Portal → your App → Credentials → Generate New Secret (shown ONCE) |
XGUARD_REDIRECT_URI |
Developer Portal → your App → Redirect URIs → add the exact callback URL of this Laravel app |
If you're the x-guard developer, log in to x-guard and follow the Developer Portal flow.
How it works (under the hood)
- User clicks
<x-xguard-login-button />→ goes to/auth/xguard/redirect XGuardLoginController::redirect()callsSocialite::driver('xguard')->redirect()- User is sent to
{XGUARD_ISSUER}/oauth/authorize→ logs in at x-guard → grants consent - x-guard redirects back to
{XGUARD_REDIRECT_URI}?code=...&state=... XGuardLoginController::callback():- Exchanges code for tokens at
{XGUARD_ISSUER}/oauth/token - Fetches user profile from
{XGUARD_ISSUER}/oauth/userinfo - Calls
UserResolver::resolve($ssoUser)to find/create the local user Auth::login()and redirects to the intended URL (orredirect_after_login)
- Exchanges code for tokens at
PKCE, JWT signature verification (via the OIDC userinfo server-to-server fetch), state CSRF protection — all handled by Socialite + the underlying x-guard server.
Troubleshooting
| Symptom | Likely cause / fix |
|---|---|
error=invalid_request, redirect_uri not registered |
XGUARD_REDIRECT_URI doesn't exactly match what's whitelisted in Developer Portal. Check scheme, host, port, path, trailing slash. |
error=invalid_client |
Wrong XGUARD_CLIENT_SECRET or App is suspended in Developer Portal. |
| User logs in but new account is created instead of linking | Check that email returned by x-guard matches your local user's email exactly. Or override UserResolver. |
| Button has no link (href="#") | Default routes disabled but no xguard.redirect route defined. Either re-enable default routes or pass href="...". |
Class "Laravel\Socialite\..." not found |
Socialite is a peer dep — composer require laravel/socialite once. |
License
MIT — see LICENSE.
Credits
- Built on top of Laravel Socialite.
- The icon is a generic shield-with-lock SVG; replace by publishing the view if you have x-guard branding.
onelabs/xguard-client 适用场景与选型建议
onelabs/xguard-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「oauth」 「oauth2」 「SSO」 「laravel」 「oidc」 「socialite」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 onelabs/xguard-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 onelabs/xguard-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 onelabs/xguard-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Email Toolkit Plugin for CakePHP
The missing social authentication for thephpleague/oauth2-server, adapted from schedula/league-oauth2-social
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 50
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-08