klimesf/nette-jwt-user-storage

使用 JWT 而不是会话的 Nette IUserStorage 实现。

v1.0.0 2015-08-21 15:51 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:15 UTC


README

Latest Stable Version License Build Status

已弃用

此仓库已弃用。您可以使用 slepic 的分支 https://github.com/slepic/nette-jwt-user-storage

使用 JWT 访问令牌而不是 PHP 会话的 Nette IUserStorage 实现。

免责声明:如果您不知道 JWT 是什么,请参阅 JWT 草案JWT 主页

在用户登录时,应用程序存储 jwt_access_token 曲奇而不是古老的 PHPSESSID。该曲奇包含由应用程序签名的编码 JWT。然后用户身份验证基于验证 JWT 而不是会话。

警告:CSRF 保护规则仍然适用!

这意味着您不再需要解决 PHP 会话实现、扩展和测试问题。您通常会存储在 SessionStorage 中的所有事物都可以存储在键值存储中,其中 JWT 是键。

这也意味着您的应用程序已准备好在未来成为 SPA。 :)

配置

在您的 config.neon 中注册此扩展。

extensions:
	jwtUserStorage: Klimesf\Security\DI\JWTUserStorageExtension

然后配置其所需的属性。

JWTUserStorage:
	privateKey: 'secret-cat'    # this secret is used to sign the JWT
	algorithm: 'HS256'          # this is the signing algorithm

JWT 及其存储的曲奇默认设置为 20 天过期。如果您想调整过期时间,请使用 expiration 选项

JWTUserStorage:
	expiration: 20 days     # sets JWT and cookie expiration time to 20 days (this is the default option)
	expiration: 20 minutes  # sets JWT and cookie expiration time to 20 minutes
	expiration: false       # sets JWT and cookie to never expire

默认情况下,jtiiat(请参阅 JWT 草案)将添加到您的 JWT 中。如果您不想使用它们,请将 generateJtigenerateIat 选项设置为 false。

JWTUserStorage:
	generateJti: false          # disables jti generation for your JWT access tokens
	generateIat: false          # disables iat generation for your JWT access tokens

如果您想定义自己的 Nette\Security\IIdentity 序列化程序,该序列化程序将您的身份实现序列化为 JWT 主体,您可以实现 Klimesf\Security\IIdentitySerializer

namespace Your\Own;

class IdentitySerializer implements \Klimesf\Security\IIdentitySerializer
{
	// ...
}

并在配置中注册它。

JWTUserStorage:
	identitySerializer: Your\Own\IdentitySerializer

到此为止,您已准备好出发了!

已知问题

  • 如果您正在开发使用 JWT 用户存储的应用程序,并且您仍然在曲奇中看到 PHPSESSID,这可能是由于 Tracy\Tracy 使用了它。

讨论线程

文献