VBA拡張版:OTPを複数ユーザーに発行し、各ユーザーごとに独立管理
ここまでで「OTPを一度使ったら即失効」する仕組みを紹介しました。今回はさらに拡張して、複数ユーザーにOTPを発行し、各ユーザーごとに独立管理する仕組みです。これにより、チームや複数管理者が同時に安全に認証できます。
実現方法の流れ
- ユーザーごとにOTPを生成
- ユーザーIDやメールアドレスをキーにして管理
- 発行時刻と使用状態を記録
- 有効期限(例:5分)+ワンタイム利用
- ユーザーが入力したコードをチェック
- ユーザーごとに独立して認証
- 一度使ったら即失効
サンプルコード(複数ユーザー管理)
Option Explicit
'=== ユーザーごとのOTP管理用コレクション ===
Dim otpDict As Object
'=== 初期化 ===
Sub InitOTPSystem()
Set otpDict = CreateObject("Scripting.Dictionary")
End Sub
'=== OTP生成関数 ===
Function GenerateOTP(Optional length As Integer = 6) As String
Dim chars As String, i As Integer, result As String
chars = "0123456789"
Randomize
For i = 1 To length
result = result & Mid(chars, Int(Rnd() * Len(chars)) + 1, 1)
Next i
GenerateOTP = result
End Function
'=== OTP発行(ユーザーごと) ===
Sub IssueOTP(userID As String, email As String)
Dim otpCode As String, otpInfo As Object
If otpDict Is Nothing Then InitOTPSystem
otpCode = GenerateOTP(6)
' ユーザーごとの情報を保持
Set otpInfo = CreateObject("Scripting.Dictionary")
otpInfo("Code") = otpCode
otpInfo("IssuedTime") = Now
otpInfo("Used") = False
otpDict(userID) = otpInfo
' メール送信(例)
Call SendOTPEmail(email, otpCode)
MsgBox "ユーザー " & userID & " にOTPを発行しました", vbInformation
End Sub
'=== OTP送信(メール) ===
Sub SendOTPEmail(toAddress As String, otpCode As String)
' 簡易例:CDOで送信
Dim objMsg As Object, objConf As Object
Set objMsg = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
With objConf.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_account@domain.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_password"
.Update
End With
With objMsg
Set .Configuration = objConf
.From = "your_account@domain.com"
.To = toAddress
.Subject = "【認証コード通知】OTP"
.HTMLBody = "<html><body><h2>🔑 認証コード</h2><p><b>" & otpCode & "</b></p><p>※有効期限は5分、かつ一度のみ利用可能です。</p></body></html>"
.Send
End With
End Sub
'=== OTP認証(ユーザーごと) ===
Sub VerifyOTP(userID As String)
Dim userInput As String, otpInfo As Object
If Not otpDict.Exists(userID) Then
MsgBox "ユーザー " & userID & " のOTPは存在しません", vbCritical
Exit Sub
End If
Set otpInfo = otpDict(userID)
' 入力を受け付け
userInput = InputBox("ユーザー " & userID & " の認証コードを入力してください")
' 有効期限チェック
If DateDiff("n", otpInfo("IssuedTime"), Now) > 5 Then
MsgBox "認証コードの有効期限が切れました。新しいコードを発行してください。", vbExclamation
Exit Sub
End If
' ワンタイム利用チェック
If otpInfo("Used") = True Then
MsgBox "この認証コードはすでに使用済みです。新しいコードを発行してください。", vbCritical
Exit Sub
End If
' 一致チェック
If userInput = otpInfo("Code") Then
MsgBox "認証成功!ユーザー " & userID & " はパスワードを取得できます", vbInformation
otpInfo("Used") = True ' 即失効
otpDict(userID) = otpInfo
Else
MsgBox "認証失敗!コードが一致しません", vbCritical
End If
End Sub
VBこの拡張版のポイント
- ユーザーごとに独立管理 → DictionaryでユーザーIDをキーにしてOTPを保持
- 有効期限+ワンタイム利用 → 5分以内かつ未使用なら認証可能
- 複数ユーザー同時利用可能 → チームや複数管理者が並行して安全に認証
実務でのメリット
- 複数管理者対応 → 各ユーザーが独立したOTPを利用可能
- セキュリティ強化 → 有効期限+ワンタイム利用で不正利用を防止
- 柔軟性 → チーム単位で安全に認証フローを運用可能
✅ まとめ
- OTPを複数ユーザーに発行
- ユーザーごとに独立管理(有効期限+ワンタイム利用)
- チームや複数管理者が同時に安全に認証可能


