feat(auth): add single-use totp recovery codes
Codes are stored as SHA-256 hashes and consumed with one conditional update, so concurrent use of the same code succeeds at most once. Refs #40 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
3f21361995
commit
dbea42e7a5
9 files changed
+248
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RecoveryCodeCount is how many codes a user receives per set.
|
||||
const RecoveryCodeCount = 10
|
||||
|
||||
// recoveryAlphabet omits characters that are easily confused on paper
|
||||
// (0/o, 1/l/i).
|
||||
const recoveryAlphabet = "abcdefghjkmnpqrstuvwxyz23456789"
|
||||
|
||||
const recoveryLen = 10
|
||||
|
||||
// NewRecoveryCodes returns RecoveryCodeCount codes formatted as
|
||||
// "xxxxx-xxxxx", about 49 bits each.
|
||||
func NewRecoveryCodes() []string {
|
||||
codes := make([]string, RecoveryCodeCount)
|
||||
for i := range codes {
|
||||
b := make([]byte, recoveryLen)
|
||||
for j := range b {
|
||||
b[j] = recoveryAlphabet[randIndex(len(recoveryAlphabet))]
|
||||
}
|
||||
codes[i] = string(b[:recoveryLen/2]) + "-" + string(b[recoveryLen/2:])
|
||||
}
|
||||
return codes
|
||||
}
|
||||
|
||||
// randIndex returns a uniform index below n using rejection sampling.
|
||||
func randIndex(n int) int {
|
||||
limit := 256 - 256%n
|
||||
var b [1]byte
|
||||
for {
|
||||
_, _ = rand.Read(b[:])
|
||||
if int(b[0]) < limit {
|
||||
return int(b[0]) % n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NormalizeRecoveryCode strips separators and case so users can type codes
|
||||
// the way they wrote them down. It returns "" if the input cannot be a code.
|
||||
func NormalizeRecoveryCode(s string) string {
|
||||
s = strings.Map(func(r rune) rune {
|
||||
switch r {
|
||||
case '-', ' ':
|
||||
return -1
|
||||
}
|
||||
return r
|
||||
}, strings.ToLower(strings.TrimSpace(s)))
|
||||
if len(s) != recoveryLen {
|
||||
return ""
|
||||
}
|
||||
for _, r := range s {
|
||||
if !strings.ContainsRune(recoveryAlphabet, r) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// HashRecoveryCode hashes a normalized code. The codes carry enough entropy
|
||||
// that a fast hash suffices; attempts are rate limited.
|
||||
func HashRecoveryCode(normalized string) []byte {
|
||||
sum := sha256.Sum256([]byte(normalized))
|
||||
return sum[:]
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewRecoveryCodes(t *testing.T) {
|
||||
codes := NewRecoveryCodes()
|
||||
if len(codes) != RecoveryCodeCount {
|
||||
t.Fatalf("len(NewRecoveryCodes()) = %d, want %d", len(codes), RecoveryCodeCount)
|
||||
}
|
||||
format := regexp.MustCompile(`^[a-z2-9]{5}-[a-z2-9]{5}$`)
|
||||
seen := map[string]bool{}
|
||||
for _, c := range codes {
|
||||
if !format.MatchString(c) {
|
||||
t.Errorf("code %q does not match %s", c, format)
|
||||
}
|
||||
if NormalizeRecoveryCode(c) == "" {
|
||||
t.Errorf("NormalizeRecoveryCode(%q) = \"\", want the code", c)
|
||||
}
|
||||
if seen[c] {
|
||||
t.Errorf("duplicate code %q", c)
|
||||
}
|
||||
seen[c] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRecoveryCode(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"abcde-fghjk", "abcdefghjk"},
|
||||
{" ABCDE FGHJK ", "abcdefghjk"},
|
||||
{"abcdefghjk", "abcdefghjk"},
|
||||
{"123456", ""},
|
||||
{"abcde-fghj0", ""},
|
||||
{"abcde-fghjkm", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := NormalizeRecoveryCode(tt.in); got != tt.want {
|
||||
t.Errorf("NormalizeRecoveryCode(%q) = %q, want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user