-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_test.go
More file actions
151 lines (125 loc) · 3.4 KB
/
auth_test.go
File metadata and controls
151 lines (125 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"crypto/ed25519"
"crypto/rand"
"testing"
"time"
)
func TestValidateJWT_Valid(t *testing.T) {
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
claims := &Claims{
RoomID: "test-room",
PeerID: "test-peer",
Role: "host",
Name: "Alice",
CreatedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
}
token := SignJWT(claims, priv)
auth := NewAuth()
got, err := auth.ValidateJWT(token, pub)
if err != nil {
t.Fatalf("ValidateJWT failed: %v", err)
}
if got.RoomID != claims.RoomID {
t.Errorf("room_id = %q, want %q", got.RoomID, claims.RoomID)
}
if got.PeerID != claims.PeerID {
t.Errorf("peer_id = %q, want %q", got.PeerID, claims.PeerID)
}
if got.Role != claims.Role {
t.Errorf("role = %q, want %q", got.Role, claims.Role)
}
}
func TestValidateJWT_Expired(t *testing.T) {
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
claims := &Claims{
RoomID: "test-room",
PeerID: "test-peer",
Role: "host",
Name: "Alice",
CreatedAt: time.Now().Add(-48 * time.Hour).Unix(),
ExpiresAt: time.Now().Add(-24 * time.Hour).Unix(), // Expired
}
token := SignJWT(claims, priv)
auth := NewAuth()
_, err := auth.ValidateJWT(token, pub)
if err == nil {
t.Fatal("expected error for expired token")
}
}
func TestValidateJWT_WrongKey(t *testing.T) {
_, priv, _ := ed25519.GenerateKey(rand.Reader)
otherPub, _, _ := ed25519.GenerateKey(rand.Reader)
claims := &Claims{
RoomID: "test-room",
PeerID: "test-peer",
Role: "guest",
Name: "Bob",
CreatedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
}
token := SignJWT(claims, priv)
auth := NewAuth()
_, err := auth.ValidateJWT(token, otherPub)
if err == nil {
t.Fatal("expected error for wrong key")
}
}
func TestValidateJWT_InvalidRole(t *testing.T) {
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
claims := &Claims{
RoomID: "test-room",
PeerID: "test-peer",
Role: "admin", // Invalid role
Name: "Eve",
CreatedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
}
token := SignJWT(claims, priv)
auth := NewAuth()
_, err := auth.ValidateJWT(token, pub)
if err == nil {
t.Fatal("expected error for invalid role")
}
}
func TestValidateJWT_MalformedToken(t *testing.T) {
auth := NewAuth()
_, err := auth.ValidateJWT("not.a.valid.token", make([]byte, 32))
if err == nil {
t.Fatal("expected error for malformed token")
}
_, err = auth.ValidateJWT("", make([]byte, 32))
if err == nil {
t.Fatal("expected error for empty token")
}
}
func TestValidateJWT_HostSignsForGuest(t *testing.T) {
// Host generates keypair and signs a JWT for a guest
hostPub, hostPriv, _ := ed25519.GenerateKey(rand.Reader)
guestClaims := &Claims{
RoomID: "shared-room",
PeerID: "guest-1",
Role: "guest",
Name: "Bob",
CreatedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
}
// Host signs the guest JWT
guestToken := SignJWT(guestClaims, hostPriv)
// Relay verifies against host's public key
auth := NewAuth()
got, err := auth.ValidateJWT(guestToken, hostPub)
if err != nil {
t.Fatalf("relay should accept host-signed guest JWT: %v", err)
}
if got.Role != "guest" {
t.Errorf("role = %q, want guest", got.Role)
}
if got.PeerID != "guest-1" {
t.Errorf("peer_id = %q, want guest-1", got.PeerID)
}
}