ziming/laravel-myinfo-sg

此包最新版本(3.12)没有可用的许可证信息。

Laravel新加坡MyInfo包

3.12 2024-07-16 11:38 UTC

README

Latest Version on Packagist Total Downloads

一个适用于MyInfo新加坡的工作PHP Laravel包。已经解决了在PHP中实现时的那些令人烦恼、浪费时间的小问题。

官方MyInfo文档

Myinfo v4会被支持吗?

最近有一个公告,将有一个新的Myinfo版本取代Myinfo v4。

因此,不再强制从Myinfo v3升级到Myinfo v4。这意味着除非我有太多空闲时间(这很可能意味着永远不会),否则我不会支持Myinfo v4。

所以,无需再给我发邮件要求支持它了。

希望每个人都能对这则好消息感到高兴 :)

但如果你想提交一个PR来支持Myinfo v4,我会很乐意看一下。

安装

您可以通过composer安装此包

composer require ziming/laravel-myinfo-sg

然后,在您的.env文件中添加以下变量。

以下值是官方MyInfo nodejs教程中提供的值。

将它们更改为您应用提供的值。

MYINFO_APP_CLIENT_ID=STG2-MYINFO-SELF-TEST
MYINFO_APP_CLIENT_SECRET=44d953c796cccebcec9bdc826852857ab412fbe2
MYINFO_APP_REDIRECT_URL=https://:3001/callback
MYINFO_APP_PURPOSE="demonstrating MyInfo APIs"
MYINFO_APP_ATTRIBUTES=uinfin,name,sex,race,nationality,dob,email,mobileno,regadd,housingtype,hdbtype,marital,noa-basic,ownerprivate,cpfcontributions,cpfbalances

MYINFO_APP_SIGNATURE_CERT_PRIVATE_KEY=file:///Users/your-username/your-laravel-app/storage/myinfo-ssl/stg-demoapp-client-privatekey-2018.pem
MYINFO_SIGNATURE_CERT_PUBLIC_CERT=file:///Users/your-username/your-laravel-app/storage/myinfo-ssl/staging_myinfo_public_cert.cer

MYINFO_DEBUG_MODE=false

# SANDBOX ENVIRONMENT (no PKI digital signature)
MYINFO_AUTH_LEVEL=L0
MYINFO_API_AUTHORISE=https://sandbox.api.myinfo.gov.sg/com/v3/authorise
MYINFO_API_TOKEN=https://sandbox.api.myinfo.gov.sg/com/v3/token
MYINFO_API_PERSON=https://sandbox.api.myinfo.gov.sg/com/v3/person

# TEST ENVIRONMENT (with PKI digital signature)
#MYINFO_AUTH_LEVEL=L2
#MYINFO_API_AUTHORISE=https://test.api.myinfo.gov.sg/com/v3/authorise
#MYINFO_API_TOKEN=https://test.api.myinfo.gov.sg/com/v3/token
#MYINFO_API_PERSON=https://test.api.myinfo.gov.sg/com/v3/person

# Controller URI Paths. IMPORTANT
MYINFO_CALL_AUTHORISE_API_URL=/redirect-to-singpass
MYINFO_GET_PERSON_DATA_URL=/myinfo-person

最后,发布配置文件

php artisan vendor:publish --provider="Ziming\LaravelMyinfoSg\LaravelMyinfoSgServiceProvider" --tag="myinfo-sg-config"

您还可能希望将MyInfo官方nodejs demo应用的ssl文件发布到storage/myinfo-ssl,您应该在生产环境中替换这些文件。

php artisan vendor:publish --provider="Ziming\LaravelMyinfoSg\LaravelMyinfoSgServiceProvider" --tag="myinfo-ssl"

使用和定制

当构建您用于重定向到SingPass的按钮时,应链接到route('myinfo.singpass')

在SingPass重定向回您的回调URI后,您应向route('myinfo.person')发送一个POST请求

如果您不想使用默认的路由,您可以在config/laravel-myinfo-sg.php中将enable_default_myinfo_routes设置为false并映射您自己的路由。此包控制器仍可通过以下示例访问

<?php
use Ziming\LaravelMyinfoSg\Http\Controllers\CallAuthoriseApiController;
use Ziming\LaravelMyinfoSg\Http\Controllers\GetMyinfoPersonDataController;
use Illuminate\Support\Facades\Route;

Route::post('/go-singpass'), CallAuthoriseApiController::class)
->name('myinfo.singpass')
->middleware('web');

Route::post('/fetch-myinfo-person-data', GetMyinfoPersonDataController::class)
->name('myinfo.person');

在整个执行过程中,可能会抛出一些异常。如果您不喜欢json响应的格式,您可以在您的laravel应用程序中拦截它们来定制它,如以下示例所示

以下是一个示例

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Ziming\LaravelMyinfoSg\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\LaravelMyinfoSg\Exceptions\AccessTokenNotFoundException;
use Ziming\LaravelMyinfoSg\Exceptions\InvalidAccessTokenException;
use Ziming\LaravelMyinfoSg\Exceptions\InvalidDataOrSignatureForPersonDataException;
use Ziming\LaravelMyinfoSg\Exceptions\InvalidStateException;
use Ziming\LaravelMyinfoSg\Exceptions\MyinfoPersonDataNotFoundException;
use Ziming\LaravelMyinfoSg\Exceptions\SubNotFoundException;

最后,如果您想自己编写控制器,可以使用LaravelMyinfoSgFacadeLaravelMyinfoSg来生成授权API URI(重定向到Singpass的链接)以及获取MyInfo个人信息。以下是一些示例

<?php

use Ziming\LaravelMyinfoSg\LaravelMyinfoSgFacade as LaravelMyinfoSg;

// Get the Singpass URI and redirect to there
return redirect(LaravelMyinfoSg::generateAuthoriseApiUrl($state));
<?php
use Ziming\LaravelMyinfoSg\LaravelMyinfoSgFacade as LaravelMyinfoSg;

// Get the Myinfo person data in an array with 'data' key
$personData = LaravelMyinfoSg::getMyinfoPersonData($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($personData['data']);

您还可以选择从GetMyinfoPersonDataController派生并重写其preResponseHook()模板方法,在返回个人信息之前进行日志记录或其他操作。

更新日志

有关最近更改的更多信息,请参阅更新日志

贡献

捐赠总是受欢迎的(目前为$0),尤其是如果您或您的雇主通过我的包赚钱。我知道有几家。