Sign-in:
- Google, GitHub, Microsoft and any OpenID Connect provider, set up in the
admin panel (secrets are write-only). PocketBase talks to the provider,
and docwell-server keeps the state and PKCE verifier. Accounts a
provider creates follow the sign-up switch and the domain list; refused
ones are removed again.
- Two-step sign-in with an authenticator app (TOTP), set up under "Your
account", with ten single-use recovery codes. After any first factor
(password, email code, provider) the session is held behind a
five-minute challenge: five tries, each code works once. Turning it off
needs a code. Managers can reset it for someone who lost their phone.
Both the web app and the admin panel ask for the code.
Integrations:
- Personal API tokens ("dw_…") that act as their owner, read-only or
read-write, with optional expiry. Only a hash is stored, and the token is
shown once. Tokens cannot manage tokens or two-step sign-in. Admins see
and can revoke their organization's tokens.
- Workspace webhooks for page, comment and space events. Deliveries are
HMAC-SHA256 signed and retried (10 s, 1 min, 5 min), with a 30-day log
and a test button. Loopback and link-local addresses are refused
(DOCWELL_WEBHOOKS_ALLOW_LOCAL is for development).
Audit log: sign-ins (including failures and second steps, filed under the
account's organization) and every successful change request, by route and
path ids, never bodies. Superadmins see everything and org admins their
organization. It is searchable and exportable as CSV in the admin panel's
new Security page, and kept for DOCWELL_AUDIT_RETENTION_DAYS (365).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
)
|
|
|
|
func TestTOTPSignIn(t *testing.T) {
|
|
ctx := context.Background()
|
|
_, pb := newFakePB(t)
|
|
s := NewService(pb, Options{SignupEnabled: true})
|
|
now := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)
|
|
s.now = func() time.Time { return now }
|
|
|
|
sess, err := s.Signup(ctx, SignupInput{Email: "ann@docwell.test", Password: "password123", Name: "Ann"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
setup, err := s.StartTOTP(ctx, sess.User, "Docwell")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.EnableTOTP(ctx, sess.User, "000000"); !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("wrong first code: %v", err)
|
|
}
|
|
code := func() string {
|
|
c, err := totp.GenerateCode(setup.Secret, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return c
|
|
}
|
|
recovery, err := s.EnableTOTP(ctx, sess.User, code())
|
|
if err != nil || len(recovery) != recoveryCount {
|
|
t.Fatalf("enable: %v %v", recovery, err)
|
|
}
|
|
|
|
// Signing in now stops at a challenge.
|
|
login := func() string {
|
|
_, err := s.Login(ctx, "ann@docwell.test", "password123", "")
|
|
var need *TOTPRequiredError
|
|
if !errors.As(err, &need) {
|
|
t.Fatalf("login should ask for a code, got %v", err)
|
|
}
|
|
return need.Challenge
|
|
}
|
|
ch := login()
|
|
now = now.Add(30 * time.Second) // the enabling code is spent
|
|
if _, err := s.VerifyTOTP(ctx, ch, "123456"); !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("wrong code: %v", err)
|
|
}
|
|
got, err := s.VerifyTOTP(ctx, ch, code())
|
|
if err != nil || got.Token == "" {
|
|
t.Fatalf("right code: %v", err)
|
|
}
|
|
if _, err := s.VerifyTOTP(ctx, ch, code()); !errors.Is(err, ErrInvalidToken) {
|
|
t.Fatalf("challenge reused: %v", err)
|
|
}
|
|
if _, err := s.VerifyTOTP(ctx, login(), code()); !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("code reused: %v", err)
|
|
}
|
|
|
|
// A recovery code works once.
|
|
if _, err := s.VerifyTOTP(ctx, login(), recovery[0]); err != nil {
|
|
t.Fatalf("recovery code: %v", err)
|
|
}
|
|
if _, err := s.VerifyTOTP(ctx, login(), recovery[0]); !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("recovery code reused: %v", err)
|
|
}
|
|
if st, _ := s.TOTPStatus(ctx, sess.User.ID); !st.Enabled || st.RecoveryCodesLeft != recoveryCount-1 {
|
|
t.Fatalf("status = %+v", st)
|
|
}
|
|
|
|
// Five wrong tries end a challenge.
|
|
ch = login()
|
|
for range challengeAttempts {
|
|
_, _ = s.VerifyTOTP(ctx, ch, "000000")
|
|
}
|
|
if _, err := s.VerifyTOTP(ctx, ch, recovery[1]); !errors.Is(err, ErrInvalidToken) {
|
|
t.Fatalf("challenge should be gone: %v", err)
|
|
}
|
|
|
|
// An expired challenge is gone too.
|
|
ch = login()
|
|
now = now.Add(challengeTTL + time.Second)
|
|
if _, err := s.VerifyTOTP(ctx, ch, recovery[1]); !errors.Is(err, ErrInvalidToken) {
|
|
t.Fatalf("expired challenge: %v", err)
|
|
}
|
|
|
|
// Turning it off needs a code; then the password alone works again.
|
|
if err := s.DisableTOTP(ctx, sess.User, "000000"); !errors.Is(err, ErrInvalidCode) {
|
|
t.Fatalf("disable without a code: %v", err)
|
|
}
|
|
if err := s.DisableTOTP(ctx, sess.User, recovery[2]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Login(ctx, "ann@docwell.test", "password123", ""); err != nil {
|
|
t.Fatalf("login after disable: %v", err)
|
|
}
|
|
}
|