danieleambrosino/firebase-authentication-bundle

一个轻量级、自包含的Symfony包,提供由Firebase客户端SDK生成的JWT进行身份验证。

dev-main 2024-01-30 13:54 UTC

This package is auto-updated.

Last update: 2024-09-30 01:36:20 UTC


README

一个轻量级、自包含、无依赖项、符合规范的Symfony包,提供开箱即用的Firebase JWT身份验证。支持使用短期ID令牌会话cookie

安装

使用Composer安装此包

composer require danieleambrosino/firebase-authentication-bundle

配置

将Firebase项目的ID设置在名为FIREBASE_PROJECT_ID的环境变量中

# .env
FIREBASE_PROJECT_ID=projectid-1a2b3

firebase身份验证器添加到您的应用防火墙中的任何一个

# config/packages/security.yaml
security:
    firewalls:
        main:
            stateless: true
            firebase: ~

对于每个防火墙,您可以选择身份验证策略(默认是bearer,请参阅配置参考

  • 使用bearer策略,您的请求必须通过发送一个短期ID令牌(由您使用的Firebase客户端SDK的Auth包生成)到Authorization: Bearer HTTP头中进行认证(根据OAuth 2.0规范);
  • 使用cookie策略,您的请求必须通过发送一个会话cookie令牌(默认名称为sessionToken)进行认证。

就是这样!认证用户将通过user_identifier参数指定的JWT负载中的声明进行识别(默认为sub)。

您可以通过为每个防火墙设置verify_email布尔参数来要求验证电子邮件。您可以选择添加一个leeway包级参数(作为一个正整数秒数)来考虑与Google服务器的时钟偏差。

此包还提供了一个名为firebase的非常基本的用户提供程序,用于基本目的(例如,保护注册路由)。

配置参考

包级配置

# config/packages/firebase_authentication.yaml
firebase_authentication:
    project_id: '%env(string:FIREBASE_PROJECT_ID)%'

    # The leeway to account for clock skew with Google servers
    leeway: 0

    # Used only by the authenticators with "cookie" strategy
    cookie_name: sessionToken

    # The field in the payload used to identify the user
    user_identifier: sub

防火墙级配置

# config/packages/security.yaml
security:
    providers:
        # Give the provider any name you want
        # You just have to set the "firebase" field
        jwt: { firebase: ~ }
    firewalls:
        main:
            stateless: true
            firebase:
                strategy: bearer # One of "bearer"; "cookie"
                verify_email: false
            
            # If you want to enable the provider
            jwt: ~