TOTP(Time-Based One-Time Password)の URI 形式


URI 形式

otpauth://{TYPE}/[LABEL]?[PARAMETERS]
  • {TYPE} : TOTP の場合は totp、 HOTP の場合は hotp
  • [LABEL] : 通常 発行者名(サービス名やサイト名):アカウント名 の形式
  • [PARAMETERS] : param1=value&param2=value のようにキーバリューを & 連結

パラメータは以下

パラメータ 必須/任意 デフォルト 説明
secret 必須 なし Base32でエンコードされた秘密鍵
issuer 任意 なし 発行者名(サービス名やサイト名)
algorithm 任意 SHA1 ハッシュアルゴリズム(SHA1, SHA256, SHA512)
digits 任意 6 認証コードの桁数
period 任意 30 TOTPの有効期間(秒)
counter HOTPの場合必須 なし HOTPの初期カウンター値


設定値

パラメータの issuer と [LABEL] の発行者名には、基本的に同じものを指定する(Google Authenticator では issuer の値が優先されるようであるが、両方同じ値を指定しておくことが推奨されている)。

issuer にはドメイン名を指定するのが良い。

それぞれの値はURLエンコードが必要。つまり以下のようになる。

public String getUri() {
    return "otpauth://" +
            uriEncode(type) + "/" +
            uriEncode(label) + "?" +
            "secret=" + uriEncode(secret) +
            "&issuer=" + uriEncode(issuer) +
            "&algorithm=" + uriEncode(algorithm) +
            "&digits=" + digits +
            "&period=" + period;
}
private String uriEncode(String text) {
    return URLEncoder.encode(text, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
}

issuer が github.com でアカウント名が name の場合は以下のようになる(: は URLエンコードで %3A となる)。

otpauth://totp/github.com%3Aname?secret=JBSWY3DPEHPK3PXP&issuer=github.com

参考リンク