formatd/hmac-authentication

Neos Flow 包,通过配置超时时间的令牌来验证 flow 账户的认证提供者。

v1.1.4 2022-08-15 13:32 UTC

This package is auto-updated.

Last update: 2024-09-15 18:00:05 UTC


README

此包通过使用配置超时时间的令牌来为验证 flow 账户添加认证提供者。认证通过传递用户名、时间戳和 hmac 完成。为了生成此令牌,包中包含一些视图助手和一个服务。

请注意:令牌可以多次使用,并在使用后不会失效。只有超时后才会失效。

兼容性

版本方案

 1.0.0 
 | | |
 | | Bugfix Releases (non breaking)
 | Neos Compatibility Releases (non breaking except framework dependencies)
 Feature Releases (breaking)

版本和兼容性

配置认证提供者

除了您常用的 PersistedUsernamePasswordProvider,您还需要将第二个 UsernameHmacTimestampProvider 提供者添加到配置中。将 providerOption "mainAuthenticationProviderName" 设置为您的 PersistedUsernamePasswordProvider 的名称,这样只有此提供者的账户可以使用魔法链接。

Neos:
  Flow:
    security:
      authentication:
        providers:
          'HmacProvider':
            provider: 'FormatD\HmacAuthentication\Authentication\UsernameHmacTimestampProvider'
            providerOptions:
              # For an account with this authentication provider name will be searched:
              mainAuthenticationProviderName: 'My.Website:FrontendLoginProvider'
              # optionally only track failed authentication attempts (possible values: 'failed', 'successful', true)
              #trackAuthenticationAttempts: 'failed'
            entryPoint: 'WebRedirect'
            entryPointOptions:
              routeValues:
                '@package': 'My.Website'
                '@controller': 'Authentication'
                '@action': 'index'

多个认证提供者

要使用不同的 AuthenticationProvider,您必须将它们添加到 allowedAuthenticationProviders 配置中,并传递 AuthenticationProviderName 作为第二个参数。

FormatD:
  HmacAuthentication:
    allowedAuthenticationProviders:
      user: 'FormatD.UserManagementPlugin:Login'
      singleSignOn: 'Project.Site:SingleSignOnLogin'
$this->hmacService->encodeAuthToken($accountIdentifier, $authenticationProviderName);

按钮点击时的认证

{namespace hmacauth=FormatD\HmacAuthentication\ViewHelpers}

<f:form package="myPackage" action="login" controller="authentication">
	<hmacauth:form.authToken accountIdentifier="anyAccountIdentifierYouWantToAuthenticate" />
	<f:form.submit name="login" value="login" />
</f:form>

通过链接进行认证(不推荐)

请注意!

这是非常危险的一种认证方式,因为登录链接会被跟踪,例如在服务器日志文件中或被代理缓存中缓存。将超时设置为尽可能低是必须的。始终首选上述提到的提交按钮解决方案。

好吧,我还是想这样做

有两种方法可以生成认证链接:使用其中一个视图助手...

{namespace hmacauth=FormatD\HmacAuthentication\ViewHelpers}
<hmacauth:link.authenticatedAction action="myAction" controller="MyController">Login to MyWebsite</f:link.authenticatedAction>
{namespace hmacauth=FormatD\HmacAuthentication\ViewHelpers}
<hmacauth:uri.authenticatedAction action="myAction" controller="MyController" />

...或者直接在您的代码中使用 hmacService

	/**
	 * @Flow\Inject
	 * @var \FormatD\HmacAuthentication\Service\HmacService
	 */
	protected $hmacService;

	public function myFunction() {
	    // ...
		$theUserName = 'username';
		$loginLinkQueryPart = $this->hmacService->generateHmacAuthenticationQueryStringPart($theUserName);
		// ...
	}

仅使用 AuthToken

如果您想在您的代码中使用 authToken(例如,用于认证其他内容),只需使用服务类即可。

	$authToken = $this->hmacService->encodeAuthToken($theUserName);
	
	$timestampIdentifierAndHmac = $this->hmacService->decodeAndValidateAuthToken($authToken);