eloquent / otis
此包已被弃用且不再维护。未建议替代包。
PHP的一次性密码/多因素认证库。
0.3.0
2014-01-23 06:22 UTC
Requires
- php: >=5.3
- christian-riesen/base32: ~1
- eloquent/enumeration: ~5
- icecave/isolator: ~2
Requires (Dev)
- eloquent/asplode: ~1
- eloquent/liberator: ~1
- eloquent/liftoff: ~0.1
- icecave/archer: ~1
This package is auto-updated.
Last update: 2020-02-06 04:38:28 UTC
README
PHP的一次性密码/多因素认证库。
安装和文档
- 作为Composer包eloquent/otis可用。
- API文档可在此找到。
什么是Otis?
Otis是一个PHP库,用于实现一次性密码 / 多因素认证系统。 Otis提供了TOTP(基于时间的密码,定义在RFC 6238)和HOTP(基于计数的密码,在RFC 4226中涵盖)的生成器和验证器。 Otis支持所有散列算法(SHA-1,SHA-256,SHA-512)。
此外, Otis提供了生成由Google Authenticator和其他兼容OTP应用程序理解的URI格式的工具,以及用于生成QR码服务的URI,以进一步简化集成。
使用方法
验证TOTP密码
use Eloquent\Otis\Totp\TotpValidator; $validator = new TotpValidator; $password = '<OTP password>'; // the password to validate $secret = '<OTP secret>'; // the shared secret $result = $validator->validate($password, $secret);
验证HOTP密码
use Eloquent\Otis\Hotp\HotpValidator; $validator = new HotpValidator; $password = '<OTP password>'; // the password to validate $secret = '<OTP secret>'; // the shared secret $counter = 0; // current counter value $result = $validator->validate($password, $secret, $counter, $newCounter); if ($result) { $counter = $newCounter; }
生成Google Authenticator URI
use Eloquent\Otis\GoogleAuthenticator\GoogleAuthenticatorUriFactory; $uriFactory = new GoogleAuthenticatorUriFactory; $uri = $uriFactory->createTotpUri('12345678901234567890', 'test.ease@example.org'); echo $uri; // outputs 'otpauth://totp/test.ease%40example.org?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'
生成Google Authenticator QR码URI
use Eloquent\Otis\GoogleAuthenticator\GoogleAuthenticatorUriFactory; use Eloquent\Otis\QrCode\GoogleChartsQrCodeUriFactory; $uriFactory = new GoogleAuthenticatorUriFactory; $qrCodeUriFactory = new GoogleChartsQrCodeUriFactory; $qrCodeUri = $qrCodeUriFactory->createUri( $uriFactory->createTotpUri('12345678901234567890', 'test.ease@example.org') ); echo $qrCodeUri; // outputs 'https://chart.googleapis.com/chart?cht=qr&chs=250x250&chld=%7C0&chl=otpauth%3A%2F%2Ftotp%2Ftest.ease%2540example.org%3Fsecret%3DGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'
验证HOTP密码序列
use Eloquent\Otis\Hotp\HotpValidator; $validator = new HotpValidator; // the password sequence to validate $passwords = array('<OTP password 1>', '<OTP password 2>', '<OTP password 3>'); $secret = '<OTP secret>'; // the shared secret $counter = 0; // current counter value $result = $validator->validateSequence($passwords, $secret, $counter, $newCounter); if ($result) { $counter = $newCounter; }
安全注意事项
在实现OTP系统时,应仔细考虑以下事项
- 每个密码只能视为有效一次。这有助于防止重放攻击。对于可能被视为整个时间段有效的基于时间的密码,这一点尤为重要。跟踪已成功验证的哪些一次性密码已使用是确保密码不被重复使用的唯一方法。
- 共享密钥应被视为敏感信息。在服务器端存储密钥时,应使用强大的双向加密。例如,使用Lockbox解决方案将是理想的。
- 为了使基于时间的OTP系统运行良好,服务器和正在使用的OTP设备之间的系统时间差异应尽可能小。Otis默认允许-1到+1的时间窗口(窗口通常为30秒),但验证器可以配置为接受更大时间窗口的密码。
尝试使用Otis
Otis有一个简单的演示系统。要运行演示,必须遵循以下说明
- 安装Google Authenticator或兼容的OTP应用程序。
- 克隆Otis仓库。
- 安装包括开发依赖在内的Composer依赖项。
- 根据首选的OTP系统类型,运行
test/bin/totp
或test/bin/hotp
。 - 将在默认浏览器中启动一个指向二维码图片的链接。
- 使用OTP应用程序扫描此二维码。
- 返回控制台并输入OTP应用程序提供的密码。
此外,还有一个用于确定OTP应用程序功能的测试套件。要运行测试套件,请按照以下步骤操作
- 按照上述方法安装Composer依赖项。
- 运行
test/bin/otp-test-suite
。 - 测试套件将在默认浏览器中启动。
OTP应用程序功能
并非所有OTP应用程序都支持相同的功能。即使是Google Authenticator也不支持其URI格式能够表达的所有功能(并且在不同平台上的支持程度不同)。
有关OTP应用程序及其功能的表格,请参阅维基中的OTP应用程序功能。