accredifysg / singpass-login
一个用于集成SingPass登录的Laravel包
dev-master
2024-10-01 22:47 UTC
Requires
- php: ^8.2
- ext-gmp: *
- ext-json: *
- ext-openssl: *
- guzzlehttp/guzzle: ^7.0
- illuminate/contracts: ^10.0||^11.0
- spomky-labs/aes-key-wrap: ^7.0
- web-token/jwt-framework: ^4.0.1
Requires (Dev)
- fakerphp/faker: ^1.23
- laravel/pint: ^1.16
- mockery/mockery: ^1.6
- orchestra/testbench: ^9.2
- phpunit/phpunit: ^11.2
- dev-master
- dev-dependabot/github_actions/actions/checkout-4
- dev-dependabot/github_actions/actions/upload-artifact-4
- dev-dependabot/github_actions/emibcn/badge-action-2.0.3
- dev-dependabot/composer/laravel/pint-1.18.1
- dev-dependabot/composer/orchestra/testbench-9.5.0
- dev-dependabot/composer/phpunit/phpunit-11.3.6
This package is auto-updated.
Last update: 2024-10-01 22:48:53 UTC
README
PHP Laravel SingPass 登录包
安装
您可以通过composer安装此包
composer require accredifysg/singpass-login
将以下变量添加到您的.env
文件中。
# SingPass variables
SINGPASS_CLIENT_ID=
SINGPASS_REDIRECT_URI=
SINGPASS_DOMAIN=
SINGPASS_DISCOVERY_ENDPOINT=
SINGPASS_SIGNING_KID=
SINGPASS_PRIVATE_EXPONENT=
SINGPASS_ENCRYPTION_KEY=
SINGPASS_JWKS=
# Default Routes
SINGPASS_USE_DEFAULT_ROUTES=true
SINGPASS_JWKS_URL=/sp/jwks
SINGPASS_CALLBACK_URL=/sp/callback
# Default Listener
SINGPASS_USE_DEFAULT_LISTENER=true
发布配置文件
php artisan vendor:publish --provider="Accredifysg\SingPassLogin\SingPassLoginServiceProvider" --tag="config"
可选地,您可以发布一个监听器,该监听器将监听SingPassLoginEvent并登录用户
php artisan vendor:publish --provider="Accredifysg\SingPassLogin\SingPassLoginServiceProvider" --tag="listener"
使用和定制
控制器和路由
有两个默认控制器用于处理登录过程
GetJwksEndpointController
将您的应用程序的JWKS端点暴露给SingPass注册。此控制器的默认路由是/sp/jwks
PostSingPassCallbackController
处理来自SingPass的回调,并启动登录过程。此控制器的默认路由是/sp/callback
如果您想设置自己的路由,可以将SINGPASS_USE_DEFAULT_ROUTES
设置为false
,然后编辑.env
文件中的SINGPASS_JWKS_URL
和SINGPASS_CALLBACK_URL
并映射自己的路由。
如果您想编写自己的控制器,可以在配置文件SingPass-Login.php
中定义它们为get_jwks_endpoint_controller
和post_singpass_callback_controller
监听器
如果您已发布默认监听器,您应该编辑它并根据NRIC映射您的用户检索。
public function handle(SingPassSuccessfulLoginEvent $event): RedirectResponse { $singPassUser = $event->getSingPassUser(); $nric = $singPassUser->getNric(); $user = User::where('nric', '=', $nric)->first(); // Map to your own model that stores the users' NRIC or UUID if (! $user) { throw new ModelNotFoundException; } Auth::login($user); return redirect()->intended(); }
如果您想编写自己的,可以将.env
中的SINGPASS_USE_DEFAULT_LISTENER
设置为false
,并替换配置文件SingPass-Login.php
中的listener_class
异常
<?php use Accredifysg\SingPassLogin\Exceptions\JweDecryptionFailedException; use Accredifysg\SingPassLogin\Exceptions\JwksInvalidException; use Accredifysg\SingPassLogin\Exceptions\JwtDecodeFailedException; use Accredifysg\SingPassLogin\Exceptions\JwtPayloadException; use Accredifysg\SingPassLogin\Exceptions\OpenIdDiscoveryException; use Accredifysg\SingPassLogin\Exceptions\SingPassJwksException; use Accredifysg\SingPassLogin\Exceptions\SingPassTokenException;