service_test.go131 lines, 4.0 KB
| 1 | package auth_test |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/fiston-user/payments/internal/auth" |
| 11 | "github.com/fiston-user/payments/internal/platform/authn" |
| 12 | "github.com/fiston-user/payments/internal/platform/postgres" |
| 13 | "github.com/google/uuid" |
| 14 | ) |
| 15 | |
| 16 | // newService returns a Service on the package's database, with a new key |
| 17 | // pair, and a verifier for its access tokens. |
| 18 | func newService(t *testing.T) (*auth.Service, *authn.Verifier) { |
| 19 | t.Helper() |
| 20 | if testing.Short() { |
| 21 | t.Skip("integration test") |
| 22 | } |
| 23 | pool, err := postgres.NewPool(t.Context(), postgresURL, 4) |
| 24 | if err != nil { |
| 25 | t.Fatalf("connect: %v", err) |
| 26 | } |
| 27 | t.Cleanup(pool.Close) |
| 28 | pub, key, _ := ed25519.GenerateKey(nil) |
| 29 | svc := auth.NewService(pool, auth.NewSigner(key, 15*time.Minute), 24*time.Hour) |
| 30 | return svc, authn.NewVerifier(pub) |
| 31 | } |
| 32 | |
| 33 | // newUser registers a user with a new email address, and returns it. |
| 34 | func newUser(t *testing.T, svc *auth.Service) string { |
| 35 | t.Helper() |
| 36 | email := uuid.NewString() + "@example.com" |
| 37 | if _, err := svc.Register(t.Context(), email, password); err != nil { |
| 38 | t.Fatalf("register: %v", err) |
| 39 | } |
| 40 | return email |
| 41 | } |
| 42 | |
| 43 | // login logs the user in, and returns the tokens. |
| 44 | func login(t *testing.T, svc *auth.Service, email string) auth.Tokens { |
| 45 | t.Helper() |
| 46 | tokens, err := svc.Login(t.Context(), email, password) |
| 47 | if err != nil { |
| 48 | t.Fatalf("login: %v", err) |
| 49 | } |
| 50 | return tokens |
| 51 | } |
| 52 | |
| 53 | func TestLogin(t *testing.T) { |
| 54 | t.Parallel() |
| 55 | svc, verifier := newService(t) |
| 56 | email := newUser(t, svc) |
| 57 | |
| 58 | tests := []struct { |
| 59 | name, email, password string |
| 60 | err error |
| 61 | }{ |
| 62 | {"right password", email, password, nil}, |
| 63 | {"email in capitals", strings.ToUpper(email), password, nil}, |
| 64 | {"wrong password", email, password + "s", auth.ErrInvalidCredentials}, |
| 65 | {"unknown email", "nobody." + email, password, auth.ErrInvalidCredentials}, |
| 66 | } |
| 67 | for _, tt := range tests { |
| 68 | t.Run(tt.name, func(t *testing.T) { |
| 69 | tokens, err := svc.Login(t.Context(), tt.email, tt.password) |
| 70 | if !errors.Is(err, tt.err) { |
| 71 | t.Fatalf("err = %v, want %v", err, tt.err) |
| 72 | } |
| 73 | if err == nil { |
| 74 | if _, err := verifier.Verify(tokens.Access); err != nil { |
| 75 | t.Errorf("access token: %v", err) |
| 76 | } |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestRefreshRotation(t *testing.T) { |
| 83 | t.Parallel() |
| 84 | svc, _ := newService(t) |
| 85 | first := login(t, svc, newUser(t, svc)) |
| 86 | |
| 87 | // Each refresh returns a new refresh token, and the one before it stops |
| 88 | // working, without harm to the rest of the chain. |
| 89 | second, err := svc.Refresh(t.Context(), first.Refresh) |
| 90 | if err != nil || second.Refresh == first.Refresh { |
| 91 | t.Fatalf("refresh: %v, or the refresh token wasn't rotated", err) |
| 92 | } |
| 93 | third, err := svc.Refresh(t.Context(), second.Refresh) |
| 94 | if err != nil { |
| 95 | t.Fatalf("second refresh: %v", err) |
| 96 | } |
| 97 | |
| 98 | // Logging out ends the chain. |
| 99 | if err := svc.Logout(t.Context(), third.Refresh); err != nil { |
| 100 | t.Fatalf("logout: %v", err) |
| 101 | } |
| 102 | if _, err := svc.Refresh(t.Context(), third.Refresh); !errors.Is(err, auth.ErrInvalidRefreshToken) { |
| 103 | t.Errorf("refresh after logout: err = %v, want ErrInvalidRefreshToken", err) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestRefreshReuse(t *testing.T) { |
| 108 | t.Parallel() |
| 109 | svc, _ := newService(t) |
| 110 | email := newUser(t, svc) |
| 111 | stolen := login(t, svc, email) |
| 112 | laptop := login(t, svc, email) // another session of the same user |
| 113 | |
| 114 | // The real client refreshes. Then the thief tries the old token. |
| 115 | current, err := svc.Refresh(t.Context(), stolen.Refresh) |
| 116 | if err != nil { |
| 117 | t.Fatalf("refresh: %v", err) |
| 118 | } |
| 119 | if _, err := svc.Refresh(t.Context(), stolen.Refresh); !errors.Is(err, auth.ErrInvalidRefreshToken) { |
| 120 | t.Fatalf("reused token: err = %v, want ErrInvalidRefreshToken", err) |
| 121 | } |
| 122 | |
| 123 | // The reuse revoked the whole family, so the current token is dead too. |
| 124 | if _, err := svc.Refresh(t.Context(), current.Refresh); !errors.Is(err, auth.ErrInvalidRefreshToken) { |
| 125 | t.Errorf("current token after reuse: err = %v, want ErrInvalidRefreshToken", err) |
| 126 | } |
| 127 | // But only that family: the laptop's session is fine. |
| 128 | if _, err := svc.Refresh(t.Context(), laptop.Refresh); err != nil { |
| 129 | t.Errorf("other session: %v", err) |
| 130 | } |
| 131 | } |