30 lines
932 B
Go
30 lines
932 B
Go
package rtc
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLocalTokenIsDeterministicBoundAndExpiring(t *testing.T) {
|
||
|
|
issuer, err := NewLocalTokenIssuer([]byte("01234567890123456789012345678901"))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
expiresAt := time.Unix(2_000_000_000, 0)
|
||
|
|
first, _ := issuer.Issue("room-1", "device-user-1", expiresAt)
|
||
|
|
second, _ := issuer.Issue("room-1", "device-user-1", expiresAt)
|
||
|
|
if first != second {
|
||
|
|
t.Fatal("same claims must regenerate the same token for idempotent replay")
|
||
|
|
}
|
||
|
|
claims, err := issuer.Verify(first, expiresAt.Add(-time.Second))
|
||
|
|
if err != nil || claims.RoomID != "room-1" || claims.UserID != "device-user-1" {
|
||
|
|
t.Fatalf("claims = %#v, err = %v", claims, err)
|
||
|
|
}
|
||
|
|
if _, err := issuer.Verify(first, expiresAt); err == nil {
|
||
|
|
t.Fatal("expired token was accepted")
|
||
|
|
}
|
||
|
|
if _, err := issuer.Verify(first+"x", expiresAt.Add(-time.Second)); err == nil {
|
||
|
|
t.Fatal("tampered token was accepted")
|
||
|
|
}
|
||
|
|
}
|