square1 / laravel-passport-firebase-auth
使用 Firebase Auth 提供者创建和验证用户,让 Laravel Passport 处理其余部分!
Requires
- php: ^7.3
- illuminate/support: ^7.0|^8.0
- kreait/laravel-firebase: ^3.0
- laravel/passport: ^9.3|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- laravel/legacy-factories: ^1.0.4
- orchestra/testbench: ^5.0 || ^6.0
- phpunit/phpunit: ^9.3
- psalm/plugin-laravel: ^1.4
- vimeo/psalm: ^4.0
README
使用 Firebase Auth 提供者(如 Google、Facebook、Apple、电子邮件等)创建和验证用户,并让 Laravel Passport 了解并处理您的后端安全端点!
这是一种创建 Laravel Passport 令牌的方法,该方法基于 Firebase 有效令牌。
安装
您可以通过 composer 安装此软件包
composer require square1/laravel-passport-firebase-auth
您需要在您的用户表上有一个 firebase_uid
列。您可以通过发布和运行迁移来自定义此列
php artisan vendor:publish --provider="Square1\LaravelPassportFirebaseAuth\LaravelPassportFirebaseAuthServiceProvider" --tag="migrations" # after column customization run: php artisan migrate
使用以下命令发布配置文件
php artisan vendor:publish --provider="Square1\LaravelPassportFirebaseAuth\LaravelPassportFirebaseAuthServiceProvider" --tag="config"
这是已发布配置文件的内容
return [ /** * We will load all the package routes under your api prefix for consistency */ 'api_prefix' => env('API_PREFIX', 'api/v1'), /** * Declare the amount of minutes in the future. Laravel Passport will create a token that endures * that long. See: https://laravel.net.cn/docs/7.x/passport#token-lifetimes */ 'token_expiration_in_minutes' => 60 * 24 * 7, // Default 1 week expiration /** * Please update the column names in the array (the values) to match your users database columns. * Remove all the key pairs you don't need to store, but leave 'uid' and 'email', we need them! * For the 'uid' create a nullable string (we'll do this if use this package migrations) * Of course, make all columns fillable in your User model * or we can't save them if those columns are protected. */ 'map_user_columns' => [ 'uid' => 'firebase_uid', // REQUIRED 'email' => 'email', // REQUIRED 'displayName' => 'name', 'emailVerified' => 'email_verified_at', 'phoneNumber' => 'phone', 'photoURL' => 'avatar', 'provider' => 'provider' // e.g facebook, google, password ], /** * Define a set of columns to add to `user` key in returned payload */ 'expose_user_columns' => [ 'id', // 'username', // 'role', ], /** * If you need some mandatory columns in order to store your laravel user model, * you can indicate them in here along with the validation rules you need. * Of course, you will need to post this keys as indicated in the readme. * And of course, make all columns fillable in your User model, * or we can't save them if those columns are protected. */ 'extra_user_columns' => [ // 'username' => 'required|unique:users|max:255', // 'birthday' => 'require|date|before:today|date_format:Y-m-d' ], /** * Please indicate if anonymous firebase users capability is alowed in your application. * If so, we will need to olso create an "anonymous" user in your database in order * to let Laravel Passport issue a token for that particular user. */ 'allow_anonymous_users' => false, /** * Indicate the mandatory fields for anonymous user creation in your laravel database. * For the email, we'll concatenate (prefix) with the UID from firebase. */ 'anonymous_columns' => [ 'email' => '@anonymous.com', // This will end up being firebasetoken@anonymous.com 'name' => 'Anonymous', 'anonymous' => true, // 'avatar' => 'sample_anonymous_avatar.png' ] ];
配置 Laravel Passport
此软件包将 Laravel Passport 作为依赖项,如果您尚未配置,请配置 Laravel Passport。
配置 Firebase
在控制台中创建一个 Firebase 项目 https://console.firebase.google.com/。
如果您尚未生成服务帐户认证文件,请从以下网址生成: https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk。您将被要求选择 Firebase 项目。之后,Firebase Admin SDK 屏幕将要求您选择语言,只需选择 Node.js
并点击 生成新的私钥
。
一旦您已将服务帐户 JSON 文件下载到您的项目中(**注意**!请将此文件添加到 git 忽略中,因为它包含敏感凭据),请像这样在 .env
中指定文件的路径
FIREBASE_CREDENTIALS=storage/firebase_credentials.json
配置认证提供者
在您的 Firebase 项目中创建和配置您想要使用的所有提供者:https://firebase.google.com/docs/auth
用法
此软件包将在您的 api 前缀下暴露 2 个端点(可配置)
- POST:
api/v1/firebase/user/create
在您的移动应用程序或前端中,您将允许您的用户使用您选择的 Firebase 客户端 SDK 创建帐户。
然后,您将使用包含在帖子中的有效 Firebase 令牌的 firebase_token
键调用此端点。
此端点将到达 Firebase 数据库,找到并验证您在前端/移动应用程序中创建的用户,并将用户记录创建到您的 Laravel 数据库中,在安装步骤中填充的 firebase_uid
列表中。
可选,您还可以执行两个额外的用户配置步骤
1 - a) 从 Firebase 用户有效负载连接额外的用户数据
在您的 config/laravel-passport-firebase-auth.php 中,使用数组中的 "map_user_columns" 键指示您想要与 Laravel 用户表匹配的键。
1 - b) 将任何其他您需要在 Laravel 数据库的用户创建过程中使用的自定义数据传递
例如,如果用户创建需要一些必填列(例如 user_plan、username、role 等)。为此,您将使用配置数组中的 "extra_user_columns" 键上的说明。
出于安全原因,我们将验证此数据,并忽略在此 "extra_user_columns" 数组中未声明的任何其他值。
发送到 api/v1/firebase/user/create
的示例有效负载
{ "firebase_token": "super_long_firebase_token_here", "username": "tonystark", "plan": "platinum", "role": "superadmin" }
如果在您的 config/laravel-passport-firebase-auth.php
文件中有以下配置
'map_user_columns' => [ 'uid' => 'firebase_uid', 'email' => 'email', 'displayName' => 'full_name', 'photoURL' => 'avatar', ], 'extra_user_columns' => [ 'username' => 'required|unique:users|max:255', 'plan' => 'required|in:silver,gold,platinum' ]
结果将是,新创建的 Firebase 用户将以 uid、email、displayName 作为 full_name 列,photoURL 作为 avatar 列存储在您的数据库中,其余的 Firebase 元数据将被丢弃。
同时也会存储用户名和计划,但将忽略对 role
的操作尝试。
您将从端点收到一个 success
状态,以及后端用户 ID 和有效的 Laravel Passport 访问令牌。
{ "status": "success", "data": { "access_token": "valid_laravel_passport_token", "token_type": "Bearer", "expires_at": "2020-09-14T23:16:35.000000Z", "user": { "id": 1 } } }
- POST:
api/v1/firebase/user/login
您需要使用有效的 Firebase 令牌调用此端点,并在请求数据中用 firebase_token
作为键。
如果我们发现该用户已在 Laravel 数据库中,结果将包含 success
状态以及用于后续请求的 passport 令牌。
{ "status": "success", "data": { "access_token": "valid_laravel_passport_token", "token_type": "Bearer", "expires_at": "2020-09-14T23:17:02.000000Z", "user": { "id": 1 } } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 变更日志。
安全
如果您发现任何安全相关的问题,请通过电子邮件 emiliano@square1.io 反馈,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。