hfrahmann / opauth
这是一个用于在您的 TYPO3 Flow 项目中使用 Opauth 的包。
Requires
- opauth/opauth: @dev
- typo3/flow: *
This package is not auto-updated.
Last update: 2022-10-19 22:33:28 UTC
README
这是一个用于在您的 TYPO3 Flow 项目中使用 Opauth 的包。
如何使用
- 安装
将此添加到您的 composer.json 中并执行更新。
{
"require":{
"hfrahmann/opauth": "*"
}
}
您可以从以下列表中下载任何策略:https://github.com/opauth/opauth/wiki/List-of-strategies
然后,您需要将提取的目录复制到您选择的文件夹中,并在 settings.yaml 中配置路径。
- 认证控制器
首先,您需要一个 AuthenticationController。 \Hfrahmann\Opauth\AbstractAuthenticationController 扩展了 TYPO3 Flow 的原始 AbstractAuthenticationController。
当您扩展 Opauth AbstractAuthenticationController 时,您必须向您的 AuthenticationController 添加以下方法。
/** * This method is called when the account does not exist in the TYPO3 Flow Account Repository. * You can show an addition formular for registration or add the account directly to the Account Repository. * If you add the account to the Repository you have to authenticate again manually. * * @param array $opauthResponseData Opauth Response with all sent data depends on the used strategy (facebook, twitter, ...) * @param \TYPO3\Flow\Security\Account $opauthAccount A pre-generated account with the Opauth data * @return void|string */ abstract public function onOpauthAccountDoesNotExist(array $opauthResponseData, \TYPO3\Flow\Security\Account $opauthAccount); /** * This method is called when the authentication was cancelled or another problem occurred at the provider. * * @param array $opauthResponseData * @return void|string */ abstract public function onOpauthAuthenticationFailure(array $opauthResponseData);
- 路由
以下路由是使 Opauth 认证工作所必需的。它必须指向您的 AuthenticationController。您可以根据需要修改 uriPattern 中的第一部分(在 /{strategy}... 之前)。但最后一部分包含 strategy 和 internalcallback 是重要的。
-
name: 'Opauth - Strategy-Login'
uriPattern: 'opauth/{strategy}(/{internalcallback})'
defaults:
'@package': 'My.Package'
'@controller': 'Authentication'
'@action': 'opauth' # don't change
'@format': 'html'
'internalcallback': '' # important
appendExceedingArguments: true
- 配置
这是 Settings.yaml 的配置。您需要配置 AuthenticationProvider 以指向 OPAuthProvider。
在 Opauth 部分,您必须声明指向您的 AuthenticationController 的路由。(与步骤 3 中的路由相同的数据)
defaultRoleIdentifier 用于作为新账户的 roleIdentifier。
对于策略的配置,您必须在 Strategy 区域中指定它们。它们的结构类似于原始 Opauth 配置。
TYPO3: Flow: security: authentication: authenticationStrategy: oneToken providers: OpauthProvider: provider: 'Hfrahmann\Opauth\Authentication\OpauthProvider' Hfrahmann: Opauth: # The route the AuthenticationController. # Must extends the \Hfrahmann\Opauth\AbstractAuthenticationController. authenticationControllerRoute: '@package': 'My.Package' '@subpackage': '' '@controller': 'Authentication' # No @action required defaultRoleIdentifier: 'My.Package:User' authenticationProviderName: 'OpauthProvider' #the provider name from top # The security_salt must be changed before first use security_salt: 'LDFmiilYf8Fyw5W10rx4W1KsVrieQCnpBzzpTBWA5vJidQKDx8pMJbmw28R1C4m' strategies: Facebook: app_id: '571xxxxxxxxxxx' app_secret: '3daxxxxxxxxxxxxxxxxxxxxxxxxxxxx' scope: 'email,read_friendlists' # optional
视图助手
还有一个视图助手,可以轻松地创建一个 Opauth 策略的 URI。
{namespace opauth=Hfrahmann\Opauth\ViewHelpers}
{opauth:opauthStrategyUri(strategy:'facebook')}
输出可以是这样的:/opauth/facebook
示例
以下是一个 AuthenticationController 的示例。
//... class AuthenticationController extends \Hfrahmann\Opauth\Controller\AbstractAuthenticationController { /** * @var \TYPO3\Flow\Security\AccountRepository * @Flow\Inject */ protected $accountRepository; /** * @param \TYPO3\Flow\Mvc\ActionRequest $originalRequest The request that was intercepted by the security framework, NULL if there was none * @return string */ protected function onAuthenticationSuccess(\TYPO3\Flow\Mvc\ActionRequest $originalRequest = NULL) { $opauthResponseData = $this->opauthResponse; // opauthResponseData contains the raw data of the Opauth response if ($originalRequest !== NULL) { $this->redirectToRequest($originalRequest); } $this->redirect('index', 'Standard', 'My.Package'); } /** * @param array $opauthResponseData Opauth Response with all sent data * @param \TYPO3\Flow\Security\Account $opauthAccount A pre-generated account with the Opauth data * @return void */ public function onOpauthAccountDoesNotExist(array $opauthResponseData, \TYPO3\Flow\Security\Account $opauthAccount) { $this->accountRepository->add($opauthAccount); $this->persistenceManager->persistAll(); // Add the account to TYPO3 Flow Account Repository. $this->authenticateAction(); // authenticate again } /** * This method is called when the authentication was cancelled or another problem occurred at the provider. * * @param array $opauthResponseData * @return void|string */ public function onOpauthAuthenticationFailure(array $opauthResponseData) { return 'Opauth Authentication Canceled'; } }
许可证
此项目根据 MIT 许可证授权。