andersbjorkland / facebook-oauth-extension
👍 Facebook Oauth 扩展允许您使用 Facebook 登录到您的管理界面。
Requires
- ext-json: *
- symfony/http-client: ^5.2
- twig/twig: ^3.3
Requires (Dev)
- bolt/core: ^4.1
- symplify/easy-coding-standard: ^8.3
This package is auto-updated.
Last update: 2024-09-04 14:16:56 UTC
README
作者:Anders Björkland 联系方式:contact@andersbjorkland.online
Bolt CMS 扩展,Facebook Oauth 扩展允许您的管理员用户使用 Facebook 登录到后端。
此扩展不是即插即用解决方案。您需要在 Facebook 开发者界面进行一些配置,以及在安全配置 config/packages/security.yaml
中添加环境变量。它还需要您在您的用户资料中使用与 Facebook 注册相同的电子邮件地址,以便能够使用此服务登录。
在 https://developers.facebook.com/ 上,您需要注册一个账户并创建一个带有 Facebook 登录 产品的全新应用。添加此产品后,进入 Facebook 登录/设置。在表单中添加 有效的 OAuth 重定向 URI
- https://your-domain.com/extensions/facebook-oauth
- https://your-domain.com/extensions/facebook-oauth/check
在开发过程中,您可以有以下条目: https://127.0.0.1:8000/extensions/facebook-oauth,https://127.0.0.1:8000/extensions/facebook-oauth/check
同样,您可以在 Facebook 登录 中添加 注销回调 URL 到 https://your-domain.com/extensions/facebook-oauth/revoke
截图
安装
composer require andersbjorkland/facebook-oauth-extension
配置安全
通过在 config/packages/security.yaml 中添加此验证器来配置认证参数。不要替换 Bolt 配置。
security: firewalls: main: guard: authenticators: - AndersBjorkland\FacebookOauthExtension\Security\FacebookAuthenticator entry_point: AndersBjorkland\FacebookOauthExtension\Security\FacebookAuthenticator
可选,如果您不希望 Facebook oauth 在尝试访问管理界面时自动触发,则将 Bolt\Security\LoginFormAuthenticator 作为入口点: entry_point: Bolt\Security\LoginFormAuthenticator
完整的安全配置
security: role_hierarchy: ROLE_ADMIN: [ROLE_EDITOR, ROLE_USER] ROLE_EDITOR: [ROLE_USER] ROLE_USER: [] encoders: Bolt\Entity\User: auto providers: database_users: entity: class: Bolt\Entity\User property: username firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: pattern: ^/ anonymous: true user_checker: Bolt\Security\UserChecker guard: authenticators: - Bolt\Security\LoginFormAuthenticator - AndersBjorkland\FacebookOauthExtension\Security\FacebookAuthenticator entry_point: AndersBjorkland\FacebookOauthExtension\Security\FacebookAuthenticator logout: handler: Bolt\Security\LogoutListener path: bolt_logout target: bolt_login remember_me: secret: '%kernel.secret%' lifetime: 2592000 access_control: # this is a catch-all for the admin area # additional security lives in the controllers - { path: '^%bolt.backend_url%/(login|resetpassword)$', roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: '^%bolt.backend_url%', roles: ROLE_ADMIN } - { path: '^/(%app_locales%)%bolt.backend_url%', roles: ROLE_ADMIN }
添加环境变量
默认情况下,此扩展将查找环境变量 FACEBOOK_APP_ID 和 FACEBOOK_APP_SECRET。将这些添加到您存储其他敏感变量的地方。您可以在您的应用中查看这些变量的值,在 https://developers.facebook.com/ 的 settings/basic 下,在 App ID 和 App Secret 下列出。
认证流程
用户访问 URL /extensions/facebook-oauth
。这将触发位于 AndersBjorkland\FacebookOauthExtension\Controller 的控制器类中的 index 方法。该方法将用户重定向到 Facebook 的 oauth-endpoint,即 https://#/v10.0/dialog/oauth。如果用户当前未通过 Facebook 进行身份验证,它将打开一个对话框,让用户使用 Facebook 登录并批准扩展访问其用户资料。当用户批准访问或如果用户已经通过 Facebook 进行身份验证时,Facebook 将将用户重定向回控制器。
当控制器收到来自Facebook的重定向时,接收到的请求对象将包含一个code参数。为了确保code有效,并且不是简单添加到url的查询参数,会向Facebook发送第二个请求以将其替换为访问令牌。收到访问令牌后,控制器将向/extensions/facebook-oauth/check路由发送响应。如果您已根据上述说明配置了config/packages/security.yaml,这将触发FacebookAuthenticator守卫。
FacebookAuthenticator守卫将查找之前步骤中收到的访问令牌对应的Facebook用户的邮箱。然后,使用此邮箱从您的数据库中检索用户。如果您的用户已使用与他们的Facebook账户相同的邮箱注册,则守卫将验证用户并将其登录到Bolt后端。
贡献
我正在考虑扩展此扩展的功能。如果您有任何改进建议,请随时通过contact@andersbjorkland.online与我联系。