Files
openteam/server/internal/passkey/passkey_test.go
T
Sakurasan 3e7efb3c88 fix passkey
server/internal/passkey/passkey.go
 的 takeSession,过期校验只在显式设置了 Expires 时才生效(与 go-webauthn 库内部 !IsZero() 的检查一致)
2026-08-19 00:00:51 +08:00

57 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package passkey
import (
"testing"
"github.com/glebarez/sqlite"
"github.com/openteam/server/internal/store"
"gorm.io/gorm"
)
func newTestService(t *testing.T) *Service {
t.Helper()
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&store.User{}, &store.Passkey{}); err != nil {
t.Fatal(err)
}
s, err := New(db, Config{RPID: "localhost", Origin: "http://localhost:8080", Name: "openteam test"})
if err != nil {
t.Fatal(err)
}
return s
}
// TestChallengeSessionRoundTrip 验证 begin 产生的 challenge 能被 complete 取回。
// go-webauthn 默认 Enforce=false,SessionData.Expires 为零值;takeSession 的
// 过期判断必须跳过零值时间,否则每个 challenge 都被当成已过期(线上必现
// "challenge 已过期或不存在")。
func TestChallengeSessionRoundTrip(t *testing.T) {
s := newTestService(t)
u := &store.User{Username: "alice", Email: "alice@example.com", Role: "user", Status: "active"}
if err := s.db.Create(u).Error; err != nil {
t.Fatal(err)
}
creation, err := s.BeginRegistration(u)
if err != nil {
t.Fatal(err)
}
challenge := creation.Response.Challenge.String() // 与前端回传一致的 base64url
sess, ok := s.takeSession(challenge)
if !ok {
t.Fatal("takeSession 返回 false:challenge 被误判为已过期(Expires 零值 bug)")
}
if sess.Challenge != challenge {
t.Fatalf("session challenge 不匹配: %q != %q", sess.Challenge, challenge)
}
// challenge 一次性消费
if _, ok := s.takeSession(challenge); ok {
t.Fatal("takeSession 应一次性消费 challenge,二次取应失败")
}
}