ziming / laravel-myinfo-business-sg
新加坡MyInfo商业Laravel包
Requires
- php: ^8.3
- ext-json: *
- ext-openssl: *
- guzzlehttp/guzzle: ^7.0
- illuminate/contracts: ^11.0
- web-token/jwt-framework: ^4.0
Requires (Dev)
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0||^11.0
- rector/rector: ^1.0
- symfony/thanks: ^1.3
README
PHP Laravel包用于新加坡MyInfo商业。
安装
您可以通过composer安装此包
composer require ziming/laravel-myinfo-business-sg
然后,将以下变量添加到您的.env
文件中。
以下提供的值是官方MyInfo nodejs教程中提供的值。
将其更改为您的应用程序提供的值。
MYINFOBIZ_APP_CLIENT_ID=STG2-MYINFOBIZ-SELF-TEST
MYINFOBIZ_APP_CLIENT_SECRET=44d953c796cccebcec9bdc826852857ab412fbe2
MYINFOBIZ_APP_REDIRECT_URL=https://:3001/callback
MYINFOBIZ_APP_REALM=https://:3001
MYINFOBIZ_APP_PURPOSE="demonstrating MyInfo Business APIs"
MYINFOBIZ_APP_ATTRIBUTES=name,sex,race,nationality,dob,regadd,housingtype,email,mobileno,marital,edulevel,basic-profile,addresses,appointments
MYINFOBIZ_APP_SIGNATURE_CERT_PRIVATE_KEY=file:///Users/your-username/your-laravel-app/storage/myinfo-business-ssl/demoapp-client-privatekey-2018.pem
MYINFOBIZ_SIGNATURE_CERT_PUBLIC_CERT=file:///Users/your-username/your-laravel-app/storage/myinfo-business-ssl/staging_myinfo_public_cert.cer
MYINFOBIZ_DEBUG_MODE=false
# SANDBOX ENVIRONMENT (no PKI digital signature)
MYINFOBIZ_AUTH_LEVEL=L0
MYINFOBIZ_API_AUTHORISE=https://sandbox.api.myinfo.gov.sg/biz/v2/authorise
MYINFOBIZ_API_TOKEN=https://sandbox.api.myinfo.gov.sg/biz/v2/token
MYINFOBIZ_API_ENTITYPERSON=https://sandbox.api.myinfo.gov.sg/biz/v2/entity-person-sample
# TEST ENVIRONMENT (with PKI digital signature)
MYINFOBIZ_AUTH_LEVEL=L2
MYINFOBIZ_API_AUTHORISE=https://test.api.myinfo.gov.sg/biz/v2/authorise
MYINFOBIZ_API_TOKEN=https://test.api.myinfo.gov.sg/biz/v2/token
MYINFOBIZ_API_ENTITYPERSON=https://test.api.myinfo.gov.sg/biz/v2/entity-person
# Controller URI Paths. IMPORTANT
MYINFOBIZ_CALL_AUTHORISE_API_URL=/redirect-to-singpass
MYINFOBIZ_GET_ENTITY_PERSON_DATA_URL=/myinfo-entity-person
最后,发布配置文件
php artisan vendor:publish --provider="Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgServiceProvider" --tag="config"
您还可能希望将MyInfo官方nodejs演示应用程序ssl文件发布到storage/myinfo-business-ssl。您应该在生产环境中替换这些文件。
php artisan vendor:publish --provider="Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgServiceProvider" --tag="myinfo-business-ssl"
使用和自定义
当构建您的按钮以重定向到SingPass时,它应链接到route('myinfo-business.singpass')
SingPass重定向回您的回调URI后,您应向route('myinfo.person')
发送POST请求
如果您不想使用提供的默认路由,您可以将config/laravel-myinfo-business-sg.php
中的enable_default_myinfo_business_routes
设置为false
并映射自己的路由。此包的控制器仍然可以通过以下示例访问
<?php use Ziming\LaravelMyinfoBusinessSg\Http\Controllers\CallMyinfoBusinessAuthoriseApiController; use Ziming\LaravelMyinfoBusinessSg\Http\Controllers\GetMyinfoBusinessEntityPersonDataController; use Illuminate\Support\Facades\Route; Route::post(config('/go-myinfo-business-singpass'), CallMyinfoBusinessAuthoriseApiController::class) ->name('myinfo-business.singpass') ->middleware('web'); Route::post('/fetch-myinfo-business-entity-person-data', GetMyinfoBusinessEntityPersonDataController::class) ->name('myinfo-business.entity-person');
在整个执行过程中,可能会抛出一些异常。如果您不喜欢json响应的格式,您可以通过在您的laravel应用程序中的app/Exceptions/Handler.php
中拦截它们来自定义它
以下是一个示例
<?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Ziming\LaravelMyinfoBusinessSg\Exceptions\AccessTokenNotFoundException; class Handler extends ExceptionHandler { /** * A list of the exception types that are not reported. * * @var array */ protected $dontReport = [ // You may wish to add all the Exceptions thrown by this package. See src/Exceptions folder ]; /** * A list of the inputs that are never flashed for validation exceptions. * * @var array */ protected $dontFlash = [ 'password', 'password_confirmation', ]; /** * Report or log an exception. * * @param \Throwable $exception * @return void */ public function report(\Throwable $exception) { parent::report($exception); } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Throwable $exception * @return \Illuminate\Http\Response */ public function render($request, \Throwable $exception) { // Example of an override. You may override it via Service Container binding too if ($exception instanceof AccessTokenNotFoundException && $request->wantsJson()) { return response()->json([ 'message' => 'Access Token is missing' ], 404); } return parent::render($request, $exception); } }
异常列表如下
<?php use Ziming\LaravelMyinfoBusinessSg\Exceptions\AccessTokenNotFoundException; use Ziming\LaravelMyinfoBusinessSg\Exceptions\InvalidAccessTokenException; use Ziming\LaravelMyinfoBusinessSg\Exceptions\InvalidDataOrSignatureForEntityPersonDataException; use Ziming\LaravelMyinfoBusinessSg\Exceptions\InvalidStateException; use Ziming\LaravelMyinfoBusinessSg\Exceptions\MyinfoEntityPersonDataNotFoundException; use Ziming\LaravelMyinfoBusinessSg\Exceptions\SubNotFoundException;
最后,如果您想自己编写控制器,您可以使用LaravelMyinfoBusinessSgFacade
或LaravelMyinfoBusinessSg
来生成授权api uri(重定向到Singpass的链接)和获取MyInfo Person数据。以下是一些示例
<?php use Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgFacade as LaravelMyinfoBusinessSg; // Get the Singpass URI and redirect to there return redirect(LaravelMyinfoBusinessSg::generateAuthoriseApiUrl($state));
<?php use Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgFacade as LaravelMyinfoBusinessSg; // Get the Myinfo Business data in an array with 'data' key $entityPersonData = LaravelMyinfoBusinessSg::getMyinfoEntityPersonData($code); // If you didn't want to return a json response with the person information in the 'data' key. You can do this return response()->json($entityPersonData['data']);
您还可以选择扩展GetMyinfoEntityPersonDataController
并覆盖其preResponseHook()
模板方法以在返回人员数据之前进行日志记录或其他操作。
变更日志
请参阅CHANGELOG了解最近有哪些更改。
贡献
其他贡献方式
请参阅CONTRIBUTING以获取详细信息。