
支持双因素身份验证 (2FA) 几乎总是一个好主意,尤其是对于后台系统。2FA 有许多不同的形式,其中一些包括 SMS、TOTP 甚至硬件令牌。
启用它们需要类似的流程:
我将重点介绍 Google Authenticator,它使用 TOTP(基于时间的一次性密码)来生成一系列验证码。这个想法是服务器和客户端应用程序共享一个密钥。根据该键和当前时间,两者都得出相同的代码。当然,时钟不是完全同步的,所以有一些代码窗口被服务器接受为有效的。请注意,如果您不信任 Google 的应用程序,您可以使用下面的相同库实现您自己的客户端应用程序。
如何使用 Java(在服务器上)实现它?使用GoogleAuth 库。流程如下:
从理论的角度来看,这里最重要的一点是密钥的共享。加密是对称的,因此双方(身份验证器应用程序和服务器)具有相同的密钥。它通过用户扫描的二维码共享。如果攻击者在那时控制了用户的机器,则机密可能会泄露,因此 2FA 也会被攻击者滥用。但这不在威胁模型中——换句话说,如果攻击者可以访问用户的机器,那么损害就已经造成了。
注意:您可能会看到此过程称为 2 步身份验证或 2 因素。“因素”是:“你知道的东西”、“你拥有的东西”和“你是的东西”。您可以将 TOTP 视为“您知道”的另一件事,但您也可以将带有安全存储的密钥的手机视为“您拥有”的东西。在这种特殊情况下,我不坚持使用任何一个术语。
登录后,流程如下:
虽然我说的是用户名和密码,但它可以适用于任何其他身份验证方法。从 OAuth/OpenID Connect/SAML 提供程序获得成功确认后,或者在获得来自SecureLogin的令牌后,您可以请求第二个因素(代码)。
在代码中,上述流程如下所示(使用 Spring MVC;为了简洁起见,我合并了控制器和服务层。您可以将 @AuthenticatedPrincipal 位替换为您将当前登录的用户详细信息提供给控制器的方式)。假设方法在映射到“/user/”的控制器中:
@RequestMapping(value ="/init2fa", method = RequestMethod.POST)
@ResponseBody
public String initTwoFactorAuth(@AuthenticationPrincipal LoginAuthenticationToken token) {
User user = getLoggedInUser(token);
GoogleAuthenticatorKey googleAuthenticatorKey = googleAuthenticator.createCredentials();
// note - preferably encrypt it with an externally stored (or even HSM) key
user.setTwoFactorAuthKey(googleAuthenticatorKey.getKey());
dao.update(user);
return GoogleAuthenticatorQRGenerator.getOtpAuthURL(GOOGLE_AUTH_ISSUER, email, googleAuthenticatorKey);
}
@RequestMapping(value ="//confirm/i2fa", method = RequestMethod.POST)
@ResponseBody
public boolean confirmTwoFactorAuth(@AuthenticationPrincipal LoginAuthenticationToken token,@RequestParam("code")int code) {
User user = getLoggedInUser(token);
boolean result = googleAuthenticator.authorize(user.getTwoFactorAuthKey(), code);
user.setTwoFactorAuthEnabled(result);
dao.update(user);
return result;
}
@RequestMapping(value ="/disable2fa", method = RequestMethod.GET)
@ResponseBody
public void disableTwoFactorAuth(@AuthenticationPrincipal LoginAuthenticationToken token) {
User user = getLoggedInUser(token);
user.setTwoFactorAuthKey(null);
user.setTwoFactorAuthEnabled(false);
dao.update(user);
}
@RequestMapping(value ="/requires2fa", method = RequestMethod.POST)
@ResponseBody
public boolean login(@RequestParam("email") String email) {
// TODO consider verifying the password here in order not to reveal that a given user uses 2FA
return userService.getUserDetailsByEmail(email).isTwoFactorAuthEnabled();
}
二维码生成使用 Google 的服务,从技术上讲,该服务也为 Google 提供了密钥。我怀疑他们除了生成二维码之外还存储它,但是如果您不信任他们,您可以实现自己的二维码生成器,自己生成二维码应该不难。
在客户端,它是对上述方法的简单 AJAX 请求(旁注:我有点觉得 AJAX 一词不再流行,但我不知道如何调用它们。异步?背景?Javascript?)。
$("#two-fa-init").click(function() {
$.post("/user/init2fa",function(qrImage) {
$("#two-fa-verification").show();
$("#two-fa-qr").prepend($('',{id:'qr',src:qrImage}));
$("#two-fa-init").hide();
});
});
$("#two-fa-/confirm/i").click(function() {
var verificationCode = $("#verificationCode").val().replace(/ /g,'')
$.post("/user//confirm/i2fa?code=" + verificationCode,function() {
$("#two-fa-verification").hide();
$("#two-fa-qr").hide();
$.notify("Successfully enabled two-factor authentication","success");
$("#two-fa-message").html("Successfully enabled");
});
});
$("#two-fa-disable").click(function() {
$.post("/user/disable2fa",function(qrImage) {
window.location.reload();
});
});
登录表单代码在很大程度上取决于您正在使用的现有登录表单,但重点是使用电子邮件(和密码)调用 /requires2fa 以检查是否启用了 2FA,然后显示验证码输入。
总的来说,如果双因素身份验证的实现很简单,我建议将它用于大多数系统,在这些系统中,安全性比用户体验的简单性更重要。