基于时间的 One-time Password (TOTP)¶
配置 TOTP¶
TOTP 在 Keystone 中默认情况下未启用。要启用它,请将 totp 身份验证方法添加到 [auth] 部分的 keystone.conf 中
[auth]
methods = external,password,token,oauth1,totp
为了让用户能够访问 TOTP,他必须在 Keystone 中配置 TOTP 凭据,并且拥有一个 TOTP 设备(例如 Google Authenticator)。
TOTP 使用 base32 编码的字符串作为密钥。密钥必须至少为 128 位(16 字节)。以下 Python 代码可用于生成 TOTP 密钥
import base64
message = b'1234567890123456'
print(base64.b32encode(message).rstrip(b'='))
示例输出
GEZDGNBVGY3TQOJQGEZDGNBVGY
然后,可以使用生成的密钥为特定用户添加新的 ‘totp’ 凭据。
创建 TOTP 凭据¶
为用户创建 totp 凭据
USER_ID=b7793000f8d84c79af4e215e9da78654
SECRET=GEZDGNBVGY3TQOJQGEZDGNBVGY
curl -i \
-H "Content-Type: application/json" \
-d '
{
"credential": {
"blob": "'$SECRET'",
"type": "totp",
"user_id": "'$USER_ID'"
}
}' \
https://:5000/v3/credentials ; echo
Google Authenticator¶
在设备上安装 Google Authenticator,然后在应用程序中点击 ‘设置帐户’,然后点击 ‘输入提供的密钥’。在输入字段中输入帐户名称和密钥。可以选择性地以编程方式生成 QR 代码,以避免手动输入信息。
QR 代码¶
为设备创建 TOTP QR 代码
import qrcode
secret='GEZDGNBVGY3TQOJQGEZDGNBVGY'
uri = 'otpauth://totp/{name}?secret={secret}&issuer={issuer}'.format(
name='name',
secret=secret,
issuer='Keystone')
img = qrcode.make(uri)
img.save('totp.png')
在 Google Authenticator 应用程序中点击 ‘设置帐户’,然后点击 ‘扫描条形码’,然后扫描 ‘totp.png’ 图像。这应该在应用程序中创建一个新的 TOTP 条目。
使用 TOTP 进行身份验证¶
Google Authenticator 将每隔几秒生成一个 6 位数的 PIN 码(密码)。使用密码和您的用户 ID 使用 totp 方法进行身份验证。
令牌¶
使用 totp 获取具有默认范围(可能未设置范围)的令牌
USER_ID=b7793000f8d84c79af4e215e9da78654
PASSCODE=012345
curl -i \
-H "Content-Type: application/json" \
-d '
{ "auth": {
"identity": {
"methods": [
"totp"
],
"totp": {
"user": {
"id": "'$USER_ID'",
"passcode": "'$PASSCODE'"
}
}
}
}
}' \
https://:5000/v3/auth/tokens ; echo