+
+
LIVEKIT_{KEY|SECRET} | |
+| `LIVEKIT_JWT_BIND` | Address to bind the server to | β No, β οΈ mutually exclusive with `LIVEKIT_JWT_PORT` | `:8080` |
+| `LIVEKIT_JWT_PORT` | β οΈ Deprecated Port to bind the server to | β No, β οΈ mutually exclusive with `LIVEKIT_JWT_BIND` | |
+| `LIVEKIT_FULL_ACCESS_HOMESERVERS` | Comma-separated list of full-access homeservers (`*` for all) | β No | `*` |
+
+> [!IMPORTANT]
+> By default, the LiveKit SFU auto-creates rooms for all users. To ensure proper
+> access control, update your LiveKit
+> [config.yaml](https://github.com/livekit/livekit/blob/7350e9933107ecdea4ada8f8bcb0d6ca78b3f8f7/config-sample.yaml#L170)
+> to **disable automatic room creation**.
+
+**LiveKit SFU config should include:**
+
+```yaml
+room:
+ auto_create: false
+```
+
+## π Transport Layer Security (TLS) Setup Using a Reverse Proxy
+
+To properly secure the MatrixRTC Authorization Service, a reverse proxy is
+recommended.
+
+### Example Caddy Config
+
+```caddy
+matrix-rtc.domain.tld {
+ bind xx.xx.xx.xx
+
+ handle /livekit/jwt* {
+ reverse_proxy localhost:8080
+ }
+}
+```
+
+### Example Nginx Config
+
+```nginx
+server {
+ listen 80;
+ server_name matrix-rtc.domain.tld;
+
+ # Redirect HTTP β HTTPS
+ return 301 https://$host$request_uri;
+}
+
+server {
+ listen 443 ssl;
+ server_name matrix-rtc.domain.tld;
+
+ # TLS certificate paths (replace with your own)
+ ssl_certificate /etc/ssl/certs/matrix-rtc.crt;
+ ssl_certificate_key /etc/ssl/private/matrix-rtc.key;
+
+ # TLS settings (minimal)
+ ssl_protocols TLSv1.2 TLSv1.3;
+ ssl_ciphers HIGH:!aNULL:!MD5;
+
+ location /livekit/jwt/ {
+ proxy_pass http://localhost:8080/;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ }
+}
+```
+
+## π Do Not Forget to Update Your Matrix Site's `.well-known/matrix/client`
+
+For proper MatrixRTC functionality, you need to configure your site's
+`.well-known/matrix/client`. See the
+[Element Call self-hosting guide](https://github.com/element-hq/element-call/blob/livekit/docs/self-hosting.md#matrixrtc-backend-announcement)
+for reference.
+
+The following key must be included in
+`https://domain.tld/.well-known/matrix/client`:
+
+```json
+"org.matrix.msc4143.rtc_foci": [
+ {
+ "type": "livekit",
+ "livekit_service_url": "https://matrix-rtc.domain.tld/livekit/jwt"
+ }
+]
+```
+
+## π§ͺ Development & Testing
+
+### Disable TLS verification
+
+For testing and debugging (e.g. in the absence of trusted certificates while
+testing in a lab), you can disable TLS verification for the outgoing connection
+to the Matrix homeserver by setting the environment variable
+`LIVEKIT_INSECURE_SKIP_VERIFY_TLS` to `YES_I_KNOW_WHAT_I_AM_DOING`.
+
+### π οΈ Development Environment (Docker Compose)
+
+Based on the
+[Element Call GitHub repo](https://github.com/element-hq/element-call)
+
+The easiest way to spin up the full Matrix stack is by using the development
+environment provided by Element Call. For detailed instructions, see
+[Element Call Backend Setup](https://github.com/element-hq/element-call?tab=readme-ov-file#backend).
+
+> [!NOTE]
+> To ensure your local frontend works properly, you need to add certificate
+> exceptions in your browser for:
+>
+> - `https://localhost:3000`
+> - `https://matrix-rtc.m.localhost/livekit/jwt/healthz`
+> - `https://synapse.m.localhost/.well-known/matrix/client`
+>
+> You can do this either by adding the minimal m.localhost CA
+> ([dev_tls_m.localhost.crt](https://raw.githubusercontent.com/element-hq/element-call/refs/heads/livekit/backend/dev_tls_m.localhost.crt))
+> to your browserβs trusted certificates, or by visiting each URL in your
+> browser and following the prompts to accept the exception.
+
+#### π³ Start MatrixRTC stack without the MatrixRTC Authorization Service
+
+```sh
+git clone https://github.com/element-hq/element-call.git
+cd element-call
+docker-compose -f ./dev-backend-docker-compose.yml -f ./playwright-backend-docker-compose.override.yml up nginx livekit synapse redis
+```
+
+#### π Start the MatrixRTC Authorization Service locally
+
+```sh
+git clone https://github.com/element-hq/lk-jwt-service
+cd lk-jwt-service
+LIVEKIT_INSECURE_SKIP_VERIFY_TLS="YES_I_KNOW_WHAT_I_AM_DOING" \
+LIVEKIT_URL="wss://matrix-rtc.m.localhost/livekit/sfu" \
+LIVEKIT_KEY=devkey \
+LIVEKIT_SECRET=secret \
+LIVEKIT_JWT_PORT=6080 \
+LIVEKIT_FULL_ACCESS_HOMESERVERS=synapse.m.localhost \
+./lk-jwt-service
+```
diff --git a/lk-jwt-service/go.mod b/lk-jwt-service/go.mod
new file mode 100644
index 0000000..fa0989f
--- /dev/null
+++ b/lk-jwt-service/go.mod
@@ -0,0 +1,92 @@
+module lk-jwt-service
+
+go 1.23.0
+
+toolchain go1.25.3
+
+require (
+ github.com/golang-jwt/jwt/v5 v5.3.0
+ github.com/livekit/protocol v1.34.0
+ github.com/livekit/server-sdk-go/v2 v2.5.0
+ github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530
+ github.com/matrix-org/gomatrixserverlib v0.0.0-20250815065806-6697d93cbcba
+)
+
+require (
+ buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.0-20241127180247-a33202765966.1 // indirect
+ buf.build/go/protoyaml v0.3.1 // indirect
+ cel.dev/expr v0.19.0 // indirect
+ github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
+ github.com/benbjohnson/clock v1.3.5 // indirect
+ github.com/bep/debounce v1.2.1 // indirect
+ github.com/bufbuild/protovalidate-go v0.8.0 // indirect
+ github.com/cespare/xxhash/v2 v2.3.0 // indirect
+ github.com/dennwc/iters v1.0.1 // indirect
+ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+ github.com/frostbyte73/core v0.1.1 // indirect
+ github.com/fsnotify/fsnotify v1.8.0 // indirect
+ github.com/gammazero/deque v1.0.0 // indirect
+ github.com/go-jose/go-jose/v3 v3.0.4 // indirect
+ github.com/go-logr/logr v1.4.2 // indirect
+ github.com/go-logr/stdr v1.2.2 // indirect
+ github.com/google/cel-go v0.22.1 // indirect
+ github.com/google/uuid v1.6.0 // indirect
+ github.com/gorilla/websocket v1.5.3 // indirect
+ github.com/hashicorp/go-set/v3 v3.0.0 // indirect
+ github.com/jxskiss/base62 v1.1.0 // indirect
+ github.com/klauspost/compress v1.17.11 // indirect
+ github.com/klauspost/cpuid/v2 v2.2.7 // indirect
+ github.com/kr/pretty v0.3.1 // indirect
+ github.com/lithammer/shortuuid/v4 v4.2.0 // indirect
+ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 // indirect
+ github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564 // indirect
+ github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126 // indirect
+ github.com/magefile/mage v1.15.0 // indirect
+ github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 // indirect
+ github.com/nats-io/nats.go v1.38.0 // indirect
+ github.com/nats-io/nkeys v0.4.9 // indirect
+ github.com/nats-io/nuid v1.0.1 // indirect
+ github.com/oleiade/lane/v2 v2.0.0 // indirect
+ github.com/pion/datachannel v1.5.10 // indirect
+ github.com/pion/dtls/v3 v3.0.4 // indirect
+ github.com/pion/ice/v4 v4.0.6 // indirect
+ github.com/pion/interceptor v0.1.39 // indirect
+ github.com/pion/logging v0.2.3 // indirect
+ github.com/pion/mdns/v2 v2.0.7 // indirect
+ github.com/pion/randutil v0.1.0 // indirect
+ github.com/pion/rtcp v1.2.15 // indirect
+ github.com/pion/rtp v1.8.18 // indirect
+ github.com/pion/sctp v1.8.35 // indirect
+ github.com/pion/sdp/v3 v3.0.10 // indirect
+ github.com/pion/srtp/v3 v3.0.4 // indirect
+ github.com/pion/stun/v3 v3.0.0 // indirect
+ github.com/pion/transport/v3 v3.0.7 // indirect
+ github.com/pion/turn/v4 v4.0.0 // indirect
+ github.com/pion/webrtc/v4 v4.0.9 // indirect
+ github.com/puzpuzpuz/xsync/v3 v3.5.0 // indirect
+ github.com/redis/go-redis/v9 v9.7.3 // indirect
+ github.com/sirupsen/logrus v1.9.3 // indirect
+ github.com/stoewer/go-strcase v1.3.0 // indirect
+ github.com/tidwall/gjson v1.18.0 // indirect
+ github.com/tidwall/match v1.1.1 // indirect
+ github.com/tidwall/pretty v1.2.1 // indirect
+ github.com/tidwall/sjson v1.2.5 // indirect
+ github.com/twitchtv/twirp v8.1.3+incompatible // indirect
+ github.com/wlynxg/anet v0.0.5 // indirect
+ github.com/zeebo/xxh3 v1.0.2 // indirect
+ go.uber.org/atomic v1.11.0 // indirect
+ go.uber.org/multierr v1.11.0 // indirect
+ go.uber.org/zap v1.27.0 // indirect
+ go.uber.org/zap/exp v0.3.0 // indirect
+ golang.org/x/crypto v0.38.0 // indirect
+ golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 // indirect
+ golang.org/x/net v0.40.0 // indirect
+ golang.org/x/sync v0.14.0 // indirect
+ golang.org/x/sys v0.33.0 // indirect
+ golang.org/x/text v0.25.0 // indirect
+ google.golang.org/genproto/googleapis/api v0.0.0-20241202173237-19429a94021a // indirect
+ google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489 // indirect
+ google.golang.org/grpc v1.70.0 // indirect
+ google.golang.org/protobuf v1.36.5 // indirect
+ gopkg.in/yaml.v3 v3.0.1 // indirect
+)
diff --git a/lk-jwt-service/go.sum b/lk-jwt-service/go.sum
new file mode 100644
index 0000000..71120af
--- /dev/null
+++ b/lk-jwt-service/go.sum
@@ -0,0 +1,310 @@
+buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.0-20241127180247-a33202765966.1 h1:ntAj16eF7AtUyzOOAFk5gvbAO52QmUKPKk7GmsIEORo=
+buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.0-20241127180247-a33202765966.1/go.mod h1:AxRT+qTj5PJCz2nyQzsR/qxAcveW5USRhJTt/edTO5w=
+buf.build/go/protoyaml v0.3.1 h1:ucyzE7DRnjX+mQ6AH4JzN0Kg50ByHHu+yrSKbgQn2D4=
+buf.build/go/protoyaml v0.3.1/go.mod h1:0TzNpFQDXhwbkXb/ajLvxIijqbve+vMQvWY/b3/Dzxg=
+cel.dev/expr v0.19.0 h1:lXuo+nDhpyJSpWxpPVi5cPUwzKb+dsdOiw6IreM5yt0=
+cel.dev/expr v0.19.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
+dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
+dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
+github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
+github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
+github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
+github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
+github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
+github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
+github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
+github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
+github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
+github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
+github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
+github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
+github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
+github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
+github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
+github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
+github.com/bufbuild/protovalidate-go v0.8.0 h1:Xs3kCLCJ4tQiogJ0iOXm+ClKw/KviW3nLAryCGW2I3Y=
+github.com/bufbuild/protovalidate-go v0.8.0/go.mod h1:JPWZInGm2y2NBg3vKDKdDIkvDjyLv31J3hLH5GIFc/Q=
+github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
+github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
+github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
+github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/containerd/continuity v0.4.3 h1:6HVkalIp+2u1ZLH1J/pYX2oBVXlJZvh1X1A7bEZ9Su8=
+github.com/containerd/continuity v0.4.3/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dennwc/iters v1.0.1 h1:XwMudE6xtS0ugEdum4HQ+iRi+5HSvaeKxJPM/VI3pJs=
+github.com/dennwc/iters v1.0.1/go.mod h1:M9KuuMBeyEXYTmB7EnI9SCyALFCmPWOIxn5W1L0CjGg=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
+github.com/docker/cli v26.1.4+incompatible h1:I8PHdc0MtxEADqYJZvhBrW9bo8gawKwwenxRM7/rLu8=
+github.com/docker/cli v26.1.4+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
+github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY=
+github.com/docker/docker v27.1.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
+github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
+github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
+github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
+github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
+github.com/envoyproxy/protoc-gen-validate v1.1.0 h1:tntQDh69XqOCOZsDz0lVJQez/2L6Uu2PdjCQwWCJ3bM=
+github.com/envoyproxy/protoc-gen-validate v1.1.0/go.mod h1:sXRDRVmzEbkM7CVcM06s9shE/m23dg3wzjl0UWqJ2q4=
+github.com/frostbyte73/core v0.1.1 h1:ChhJOR7bAKOCPbA+lqDLE2cGKlCG5JXsDvvQr4YaJIA=
+github.com/frostbyte73/core v0.1.1/go.mod h1:mhfOtR+xWAvwXiwor7jnqPMnu4fxbv1F2MwZ0BEpzZo=
+github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
+github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
+github.com/gammazero/deque v1.0.0 h1:LTmimT8H7bXkkCy6gZX7zNLtkbz4NdS2z8LZuor3j34=
+github.com/gammazero/deque v1.0.0/go.mod h1:iflpYvtGfM3U8S8j+sZEKIak3SAKYpA5/SQewgfXDKo=
+github.com/go-jose/go-jose/v3 v3.0.4 h1:Wp5HA7bLQcKnf6YYao/4kpRpVMp/yf6+pJKV8WFSaNY=
+github.com/go-jose/go-jose/v3 v3.0.4/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ=
+github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
+github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
+github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
+github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
+github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
+github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
+github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
+github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
+github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
+github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
+github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
+github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
+github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
+github.com/google/cel-go v0.22.1 h1:AfVXx3chM2qwoSbM7Da8g8hX8OVSkBFwX+rz2+PcK40=
+github.com/google/cel-go v0.22.1/go.mod h1:BuznPXXfQDpXKWQ9sPW3TzlAJN5zzFe+i9tIs0yC4s8=
+github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
+github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
+github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
+github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
+github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
+github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
+github.com/hashicorp/go-set/v3 v3.0.0 h1:CaJBQvQCOWoftrBcDt7Nwgo0kdpmrKxar/x2o6pV9JA=
+github.com/hashicorp/go-set/v3 v3.0.0/go.mod h1:IEghM2MpE5IaNvL+D7X480dfNtxjRXZ6VMpK3C8s2ok=
+github.com/jxskiss/base62 v1.1.0 h1:A5zbF8v8WXx2xixnAKD2w+abC+sIzYJX+nxmhA6HWFw=
+github.com/jxskiss/base62 v1.1.0/go.mod h1:HhWAlUXvxKThfOlZbcuFzsqwtF5TcqS9ru3y5GfjWAc=
+github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
+github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
+github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
+github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
+github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
+github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
+github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
+github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
+github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkIcHO0h8c=
+github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
+github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkDaKb5iXdynYrzB84ErPPO4LbRASk58=
+github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
+github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564 h1:GX7KF/V9ExmcfT/2Bdia8aROjkxrgx7WpyH7w9MB4J4=
+github.com/livekit/mediatransportutil v0.0.0-20241220010243-a2bdee945564/go.mod h1:36s+wwmU3O40IAhE+MjBWP3W71QRiEE9SfooSBvtBqY=
+github.com/livekit/protocol v1.34.0 h1:hbIXgNW+JPiTcGjzNg1XgQg3Wqa2R5dBhzuy+LLEIS4=
+github.com/livekit/protocol v1.34.0/go.mod h1:yXuQ7ucrLj91nbxL6/AHgtxdha1DGzLj1LkgvnT90So=
+github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126 h1:fzuYpAQbCid7ySPpQWWePfQOWUrs8x6dJ0T3Wl07n+Y=
+github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126/go.mod h1:X5WtEZ7OnEs72Fi5/J+i0on3964F1aynQpCalcgMqRo=
+github.com/livekit/server-sdk-go/v2 v2.5.0 h1:HCKm3f6PvefGp8emNC2mi9+9IXzBYrynuGbtUdp5u+w=
+github.com/livekit/server-sdk-go/v2 v2.5.0/go.mod h1:98/Sa+Wgb27ABwu0WYxLaMZaRfGljrrtoZDQ2xA4oVg=
+github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
+github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
+github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U=
+github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
+github.com/matrix-org/gomatrixserverlib v0.0.0-20250619052822-904c8f04597e h1:SWediqisy1Eoumr06sjGaA6gt6gS4FtXe00VB6fSNZw=
+github.com/matrix-org/gomatrixserverlib v0.0.0-20250619052822-904c8f04597e/go.mod h1:61LpEsWAroRfdVh2dnr6fQ+K3MmRgD5I35GVvF4FpXQ=
+github.com/matrix-org/gomatrixserverlib v0.0.0-20250704071233-a234d6df21c7 h1:WAcUwx+ZCK8znn1etraC2JWTns3ppcH6/gVQLfrCAnI=
+github.com/matrix-org/gomatrixserverlib v0.0.0-20250704071233-a234d6df21c7/go.mod h1:61LpEsWAroRfdVh2dnr6fQ+K3MmRgD5I35GVvF4FpXQ=
+github.com/matrix-org/gomatrixserverlib v0.0.0-20250815065806-6697d93cbcba h1:vUUjTOXZ/bYdF/SmJPH8HZ/UTmvw+ldngFKVLElmn+I=
+github.com/matrix-org/gomatrixserverlib v0.0.0-20250815065806-6697d93cbcba/go.mod h1:b6KVfDjXjA5Q7vhpOaMqIhFYvu5BuFVZixlNeTV/CLc=
+github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 h1:6z4KxomXSIGWqhHcfzExgkH3Z3UkIXry4ibJS4Aqz2Y=
+github.com/matrix-org/util v0.0.0-20221111132719-399730281e66/go.mod h1:iBI1foelCqA09JJgPV0FYz4qA5dUXYOxMi57FxKBdd4=
+github.com/miekg/dns v1.1.66 h1:FeZXOS3VCVsKnEAd+wBkjMC3D2K+ww66Cq3VnCINuJE=
+github.com/miekg/dns v1.1.66/go.mod h1:jGFzBsSNbJw6z1HYut1RKBKHA9PBdxeHrZG8J+gC2WE=
+github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
+github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
+github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
+github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
+github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
+github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
+github.com/nats-io/nats.go v1.38.0 h1:A7P+g7Wjp4/NWqDOOP/K6hfhr54DvdDQUznt5JFg9XA=
+github.com/nats-io/nats.go v1.38.0/go.mod h1:IGUM++TwokGnXPs82/wCuiHS02/aKrdYUQkU8If6yjw=
+github.com/nats-io/nkeys v0.4.9 h1:qe9Faq2Gxwi6RZnZMXfmGMZkg3afLLOtrU+gDZJ35b0=
+github.com/nats-io/nkeys v0.4.9/go.mod h1:jcMqs+FLG+W5YO36OX6wFIFcmpdAns+w1Wm6D3I/evE=
+github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
+github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
+github.com/oleiade/lane/v2 v2.0.0 h1:XW/ex/Inr+bPkLd3O240xrFOhUkTd4Wy176+Gv0E3Qw=
+github.com/oleiade/lane/v2 v2.0.0/go.mod h1:i5FBPFAYSWCgLh58UkUGCChjcCzef/MI7PlQm2TKCeg=
+github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
+github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
+github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
+github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
+github.com/opencontainers/runc v1.1.13 h1:98S2srgG9vw0zWcDpFMn5TRrh8kLxa/5OFUstuUhmRs=
+github.com/opencontainers/runc v1.1.13/go.mod h1:R016aXacfp/gwQBYw2FDGa9m+n6atbLWrYY8hNMT/sA=
+github.com/ory/dockertest/v3 v3.11.0 h1:OiHcxKAvSDUwsEVh2BjxQQc/5EHz9n0va9awCtNGuyA=
+github.com/ory/dockertest/v3 v3.11.0/go.mod h1:VIPxS1gwT9NpPOrfD3rACs8Y9Z7yhzO4SB194iUDnUI=
+github.com/pion/datachannel v1.5.10 h1:ly0Q26K1i6ZkGf42W7D4hQYR90pZwzFOjTq5AuCKk4o=
+github.com/pion/datachannel v1.5.10/go.mod h1:p/jJfC9arb29W7WrxyKbepTU20CFgyx5oLo8Rs4Py/M=
+github.com/pion/dtls/v3 v3.0.4 h1:44CZekewMzfrn9pmGrj5BNnTMDCFwr+6sLH+cCuLM7U=
+github.com/pion/dtls/v3 v3.0.4/go.mod h1:R373CsjxWqNPf6MEkfdy3aSe9niZvL/JaKlGeFphtMg=
+github.com/pion/ice/v4 v4.0.6 h1:jmM9HwI9lfetQV/39uD0nY4y++XZNPhvzIPCb8EwxUM=
+github.com/pion/ice/v4 v4.0.6/go.mod h1:y3M18aPhIxLlcO/4dn9X8LzLLSma84cx6emMSu14FGw=
+github.com/pion/interceptor v0.1.39 h1:Y6k0bN9Y3Lg/Wb21JBWp480tohtns8ybJ037AGr9UuA=
+github.com/pion/interceptor v0.1.39/go.mod h1:Z6kqH7M/FYirg3frjGJ21VLSRJGBXB/KqaTIrdqnOic=
+github.com/pion/logging v0.2.3 h1:gHuf0zpoh1GW67Nr6Gj4cv5Z9ZscU7g/EaoC/Ke/igI=
+github.com/pion/logging v0.2.3/go.mod h1:z8YfknkquMe1csOrxK5kc+5/ZPAzMxbKLX5aXpbpC90=
+github.com/pion/mdns/v2 v2.0.7 h1:c9kM8ewCgjslaAmicYMFQIde2H9/lrZpjBkN8VwoVtM=
+github.com/pion/mdns/v2 v2.0.7/go.mod h1:vAdSYNAT0Jy3Ru0zl2YiW3Rm/fJCwIeM0nToenfOJKA=
+github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=
+github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=
+github.com/pion/rtcp v1.2.15 h1:LZQi2JbdipLOj4eBjK4wlVoQWfrZbh3Q6eHtWtJBZBo=
+github.com/pion/rtcp v1.2.15/go.mod h1:jlGuAjHMEXwMUHK78RgX0UmEJFV4zUKOFHR7OP+D3D0=
+github.com/pion/rtp v1.8.18 h1:yEAb4+4a8nkPCecWzQB6V/uEU18X1lQCGAQCjP+pyvU=
+github.com/pion/rtp v1.8.18/go.mod h1:bAu2UFKScgzyFqvUKmbvzSdPr+NGbZtv6UB2hesqXBk=
+github.com/pion/sctp v1.8.35 h1:qwtKvNK1Wc5tHMIYgTDJhfZk7vATGVHhXbUDfHbYwzA=
+github.com/pion/sctp v1.8.35/go.mod h1:EcXP8zCYVTRy3W9xtOF7wJm1L1aXfKRQzaM33SjQlzg=
+github.com/pion/sdp/v3 v3.0.10 h1:6MChLE/1xYB+CjumMw+gZ9ufp2DPApuVSnDT8t5MIgA=
+github.com/pion/sdp/v3 v3.0.10/go.mod h1:88GMahN5xnScv1hIMTqLdu/cOcUkj6a9ytbncwMCq2E=
+github.com/pion/srtp/v3 v3.0.4 h1:2Z6vDVxzrX3UHEgrUyIGM4rRouoC7v+NiF1IHtp9B5M=
+github.com/pion/srtp/v3 v3.0.4/go.mod h1:1Jx3FwDoxpRaTh1oRV8A/6G1BnFL+QI82eK4ms8EEJQ=
+github.com/pion/stun/v3 v3.0.0 h1:4h1gwhWLWuZWOJIJR9s2ferRO+W3zA/b6ijOI6mKzUw=
+github.com/pion/stun/v3 v3.0.0/go.mod h1:HvCN8txt8mwi4FBvS3EmDghW6aQJ24T+y+1TKjB5jyU=
+github.com/pion/transport/v3 v3.0.7 h1:iRbMH05BzSNwhILHoBoAPxoB9xQgOaJk+591KC9P1o0=
+github.com/pion/transport/v3 v3.0.7/go.mod h1:YleKiTZ4vqNxVwh77Z0zytYi7rXHl7j6uPLGhhz9rwo=
+github.com/pion/turn/v4 v4.0.0 h1:qxplo3Rxa9Yg1xXDxxH8xaqcyGUtbHYw4QSCvmFWvhM=
+github.com/pion/turn/v4 v4.0.0/go.mod h1:MuPDkm15nYSklKpN8vWJ9W2M0PlyQZqYt1McGuxG7mA=
+github.com/pion/webrtc/v4 v4.0.9 h1:PyOYMRKJgfy0dzPcYtFD/4oW9zaw3Ze3oZzzbj2LV9E=
+github.com/pion/webrtc/v4 v4.0.9/go.mod h1:ViHLVaNpiuvaH8pdiuQxuA9awuE6KVzAXx3vVWilOck=
+github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/puzpuzpuz/xsync/v3 v3.5.0 h1:i+cMcpEDY1BkNm7lPDkCtE4oElsYLn+EKF8kAu2vXT4=
+github.com/puzpuzpuz/xsync/v3 v3.5.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
+github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
+github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA=
+github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
+github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
+github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
+github.com/shoenig/test v1.7.0 h1:eWcHtTXa6QLnBvm0jgEabMRN/uJ4DMV3M8xUGgRkZmk=
+github.com/shoenig/test v1.7.0/go.mod h1:UxJ6u/x2v/TNs/LoLxBNJRV9DiwBBKYxXSyczsBHFoI=
+github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
+github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
+github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
+github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
+github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
+github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
+github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
+github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
+github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
+github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
+github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
+github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
+github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
+github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
+github.com/twitchtv/twirp v8.1.3+incompatible h1:+F4TdErPgSUbMZMwp13Q/KgDVuI7HJXP61mNV3/7iuU=
+github.com/twitchtv/twirp v8.1.3+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=
+github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=
+github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
+github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
+github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
+github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
+github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
+github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
+github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
+github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
+github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
+github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
+github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
+go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
+go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
+go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
+go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
+go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
+go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
+go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
+go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
+go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
+go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
+golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
+golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
+golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 h1:qNgPs5exUA+G0C96DrPwNrvLSj7GT/9D+3WMWUcUg34=
+golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
+golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
+golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
+golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
+golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
+golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
+golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
+golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
+golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
+golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
+golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
+golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
+golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
+golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
+golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
+golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
+golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
+golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
+golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/genproto/googleapis/api v0.0.0-20241202173237-19429a94021a h1:OAiGFfOiA0v9MRYsSidp3ubZaBnteRUyn3xB2ZQ5G/E=
+google.golang.org/genproto/googleapis/api v0.0.0-20241202173237-19429a94021a/go.mod h1:jehYqy3+AhJU9ve55aNOaSml7wUXjF9x6z2LcCfpAhY=
+google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489 h1:5bKytslY8ViY0Cj/ewmRtrWHW64bNF03cAatUUFCdFI=
+google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
+google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ=
+google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw=
+google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
+google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
+gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
+gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
+gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/lk-jwt-service/main.go b/lk-jwt-service/main.go
new file mode 100644
index 0000000..8f3e030
--- /dev/null
+++ b/lk-jwt-service/main.go
@@ -0,0 +1,638 @@
+// Copyright 2025 Element Creations Ltd.
+// Copyright 2023 - 2025 New Vector Ltd.
+//
+// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
+// Please see LICENSE files in the repository root for full details.
+
+package main
+
+import (
+ "context"
+ "crypto/sha256"
+ "crypto/tls"
+ "encoding/base64"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "os"
+ "slices"
+ "strings"
+
+ "time"
+
+ "github.com/livekit/protocol/auth"
+ "github.com/livekit/protocol/livekit"
+ lksdk "github.com/livekit/server-sdk-go/v2"
+
+ "github.com/matrix-org/gomatrix"
+ "github.com/matrix-org/gomatrixserverlib/fclient"
+ "github.com/matrix-org/gomatrixserverlib/spec"
+)
+
+type Handler struct {
+ key, secret, lkUrl string
+ fullAccessHomeservers []string
+ skipVerifyTLS bool
+}
+type Config struct {
+ Key string
+ Secret string
+ LkUrl string
+ SkipVerifyTLS bool
+ FullAccessHomeservers []string
+ LkJwtBind string
+}
+type MatrixRTCMemberType struct {
+ ID string `json:"id"`
+ ClaimedUserID string `json:"claimed_user_id"`
+ ClaimedDeviceID string `json:"claimed_device_id"`
+}
+
+type OpenIDTokenType struct {
+ AccessToken string `json:"access_token"`
+ TokenType string `json:"token_type"`
+ MatrixServerName string `json:"matrix_server_name"`
+ ExpiresIn int `json:"expires_in"`
+}
+
+type LegacySFURequest struct {
+ Room string `json:"room"`
+ OpenIDToken OpenIDTokenType `json:"openid_token"`
+ DeviceID string `json:"device_id"`
+}
+
+type SFURequest struct {
+ RoomID string `json:"room_id"`
+ SlotID string `json:"slot_id"`
+ OpenIDToken OpenIDTokenType `json:"openid_token"`
+ Member MatrixRTCMemberType `json:"member"`
+ DelayedEventID string `json:"delayed_event_id"`
+}
+type SFUResponse struct {
+ URL string `json:"url"`
+ JWT string `json:"jwt"`
+}
+
+type MatrixErrorResponse struct {
+ Status int
+ ErrCode string
+ Err string
+}
+
+type ValidatableSFURequest interface {
+ Validate() error
+}
+
+var unpaddedBase64 = base64.StdEncoding.WithPadding(base64.NoPadding)
+
+func (e *MatrixErrorResponse) Error() string {
+ return e.Err
+}
+
+func (r *SFURequest) Validate() error {
+ if r.RoomID == "" || r.SlotID == "" {
+ log.Printf("Missing room_id or slot_id: room_id='%s', slot_id='%s'", r.RoomID, r.SlotID)
+ return &MatrixErrorResponse{
+ Status: http.StatusBadRequest,
+ ErrCode: "M_BAD_JSON",
+ Err: "The request body is missing `room_id` or `slot_id`",
+ }
+ }
+ if r.Member.ID == "" || r.Member.ClaimedUserID == "" || r.Member.ClaimedDeviceID == "" {
+ log.Printf("Missing member parameters: %+v", r.Member)
+ return &MatrixErrorResponse{
+ Status: http.StatusBadRequest,
+ ErrCode: "M_BAD_JSON",
+ Err: "The request body `member` is missing a `id`, `claimed_user_id` or `claimed_device_id`",
+ }
+ }
+ if r.OpenIDToken.AccessToken == "" || r.OpenIDToken.MatrixServerName == "" {
+ log.Printf("Missing OpenID token parameters: %+v", r.OpenIDToken)
+ return &MatrixErrorResponse{
+ Status: http.StatusBadRequest,
+ ErrCode: "M_BAD_JSON",
+ Err: "The request body `openid_token` is missing a `access_token` or `matrix_server_name`",
+ }
+ }
+ return nil
+}
+
+func (r *LegacySFURequest) Validate() error {
+ if r.Room == "" {
+ return &MatrixErrorResponse{
+ Status: http.StatusBadRequest,
+ ErrCode: "M_BAD_JSON",
+ Err: "Missing room parameter",
+ }
+ }
+ if r.OpenIDToken.AccessToken == "" || r.OpenIDToken.MatrixServerName == "" {
+ return &MatrixErrorResponse{
+ Status: http.StatusBadRequest,
+ ErrCode: "M_BAD_JSON",
+ Err: "Missing OpenID token parameters",
+ }
+ }
+ return nil
+}
+
+// writeMatrixError writes a Matrix-style error response to the HTTP response writer.
+func writeMatrixError(w http.ResponseWriter, status int, errCode string, errMsg string) {
+ w.WriteHeader(status)
+ if err := json.NewEncoder(w).Encode(gomatrix.RespError{
+ ErrCode: errCode,
+ Err: errMsg,
+ }); err != nil {
+ log.Printf("failed to encode json error message! %v", err)
+ }
+}
+
+func getJoinToken(apiKey, apiSecret, room, identity string) (string, error) {
+ at := auth.NewAccessToken(apiKey, apiSecret)
+
+ canPublish := true
+ canSubscribe := true
+ grant := &auth.VideoGrant{
+ RoomJoin: true,
+ RoomCreate: false,
+ CanPublish: &canPublish,
+ CanSubscribe: &canSubscribe,
+ Room: room,
+ }
+
+ at.SetVideoGrant(grant).
+ SetIdentity(identity).
+ SetValidFor(time.Hour)
+
+ return at.ToJWT()
+}
+
+var exchangeOpenIdUserInfo = func(
+ ctx context.Context, token OpenIDTokenType, skipVerifyTLS bool,
+) (*fclient.UserInfo, error) {
+ if token.AccessToken == "" || token.MatrixServerName == "" {
+ return nil, errors.New("missing parameters in openid token")
+ }
+
+ if skipVerifyTLS {
+ log.Printf("!!! WARNING !!! Skipping TLS verification for matrix client connection to %s", token.MatrixServerName)
+ // Disable TLS verification on the default HTTP Transport for the well-known lookup
+ http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
+ }
+ client := fclient.NewClient(fclient.WithWellKnownSRVLookups(true), fclient.WithSkipVerify(skipVerifyTLS))
+
+ // validate the openid token by getting the user's ID
+ userinfo, err := client.LookupUserInfo(
+ ctx, spec.ServerName(token.MatrixServerName), token.AccessToken,
+ )
+ if err != nil {
+ log.Printf("Failed to look up user info: %v", err)
+ return nil, errors.New("failed to look up user info")
+ }
+ return &userinfo, nil
+}
+
+func (h *Handler) isFullAccessUser(matrixServerName string) bool {
+ // Grant full access if wildcard '*' is present as the only entry
+ if len(h.fullAccessHomeservers) == 1 && h.fullAccessHomeservers[0] == "*" {
+ return true
+ }
+
+ // Check if the matrixServerName is in the list of full-access homeservers
+ return slices.Contains(h.fullAccessHomeservers, matrixServerName)
+}
+
+func (h *Handler) processLegacySFURequest(r *http.Request, req *LegacySFURequest) (*SFUResponse, error) {
+ // Note LegacySFURequest has already been validated at this point
+
+ userInfo, err := exchangeOpenIdUserInfo(r.Context(), req.OpenIDToken, h.skipVerifyTLS)
+ if err != nil {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusInternalServerError,
+ ErrCode: "M_LOOKUP_FAILED",
+ Err: "Failed to look up user info from homeserver",
+ }
+ }
+
+ isFullAccessUser := h.isFullAccessUser(req.OpenIDToken.MatrixServerName)
+
+ log.Printf(
+ "Got Matrix user info for %s (%s)",
+ userInfo.Sub,
+ map[bool]string{true: "full access", false: "restricted access"}[isFullAccessUser],
+ )
+
+ // TODO: is DeviceID required? If so then we should have validated at the start
+ lkIdentity := userInfo.Sub + ":" + req.DeviceID
+
+ // We can hard-code the slotId since for the m.call application only the m.call#ROOM slot is defined.
+ // This ensures that the same LiveKit room alias being derived for the same Matrix room for both the
+ // LegacySFURequest (/sfu/get endpoint) and the SFURequest (/get_token endpoint).
+ //
+ // Note a mismatch between the legacy livekit_alias (which is the Matrix roomId) field in the MatrixRTC
+ // membership state event and the actual lkRoomAlias (as derived below and used on the SFU) which is
+ // part of the LiveKit JWT Token does in general NOT confuse clients as the JWT token is passed as is
+ // to the livekit-client SDK.
+ //
+ // This change ensures compatibility with clients using pseudonymous livekit_aliases.
+ slotId := "m.call#ROOM"
+ lkRoomAliasHash := sha256.Sum256([]byte(req.Room + "|" + slotId))
+ lkRoomAlias := unpaddedBase64.EncodeToString(lkRoomAliasHash[:])
+ token, err := getJoinToken(h.key, h.secret, lkRoomAlias, lkIdentity)
+ if err != nil {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusInternalServerError,
+ ErrCode: "M_UNKNOWN",
+ Err: "Internal Server Error",
+ }
+ }
+
+ if isFullAccessUser {
+ if err := createLiveKitRoom(r.Context(), h, lkRoomAlias, userInfo.Sub, lkIdentity); err != nil {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusInternalServerError,
+ ErrCode: "M_UNKNOWN",
+ Err: "Unable to create room on SFU",
+ }
+ }
+ }
+
+ return &SFUResponse{URL: h.lkUrl, JWT: token}, nil
+}
+
+func (h *Handler) processSFURequest(r *http.Request, req *SFURequest) (*SFUResponse, error) {
+ // Note SFURequest has already been validated at this point
+
+ userInfo, err := exchangeOpenIdUserInfo(r.Context(), req.OpenIDToken, h.skipVerifyTLS)
+ if err != nil {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusUnauthorized,
+ ErrCode: "M_UNAUTHORIZED",
+ Err: "The request could not be authorised.",
+ }
+ }
+
+ // Check if validated userInfo.Sub matches req.Member.ClaimedUserID
+ if req.Member.ClaimedUserID != userInfo.Sub {
+ log.Printf("Claimed user ID %s does not match token subject %s", req.Member.ClaimedUserID, userInfo.Sub)
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusUnauthorized,
+ ErrCode: "M_UNAUTHORIZED",
+ Err: "The request could not be authorised.",
+ }
+ }
+
+ // Does the user belong to homeservers granted full access
+ isFullAccessUser := h.isFullAccessUser(req.OpenIDToken.MatrixServerName)
+
+ log.Printf(
+ "Got Matrix user info for %s (%s)",
+ userInfo.Sub,
+ map[bool]string{true: "full access", false: "restricted access"}[isFullAccessUser],
+ )
+
+ lkIdentityRaw := userInfo.Sub + "|" + req.Member.ClaimedDeviceID + "|" + req.Member.ID
+ lkIdentityHash := sha256.Sum256([]byte(lkIdentityRaw))
+ lkIdentity := unpaddedBase64.EncodeToString(lkIdentityHash[:])
+
+ lkRoomAliasHash := sha256.Sum256([]byte(req.RoomID + "|" + req.SlotID))
+ lkRoomAlias := unpaddedBase64.EncodeToString(lkRoomAliasHash[:])
+
+ token, err := getJoinToken(h.key, h.secret, lkRoomAlias, lkIdentity)
+ if err != nil {
+ log.Printf("Error getting LiveKit token: %v", err)
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusInternalServerError,
+ ErrCode: "M_UNKNOWN",
+ Err: "Internal Server Error",
+ }
+ }
+
+ if isFullAccessUser {
+ if err := createLiveKitRoom(r.Context(), h, lkRoomAlias, userInfo.Sub, lkIdentity); err != nil {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusInternalServerError,
+ ErrCode: "M_UNKNOWN",
+ Err: "Unable to create room on SFU",
+ }
+ }
+ }
+
+ return &SFUResponse{URL: h.lkUrl, JWT: token}, nil
+}
+
+var createLiveKitRoom = func(ctx context.Context, h *Handler, room, matrixUser, lkIdentity string) error {
+ roomClient := lksdk.NewRoomServiceClient(h.lkUrl, h.key, h.secret)
+ creationStart := time.Now().Unix()
+ lkRoom, err := roomClient.CreateRoom(
+ ctx,
+ &livekit.CreateRoomRequest{
+ Name: room,
+ EmptyTimeout: 5 * 60, // 5 Minutes to keep the room open if no one joins
+ DepartureTimeout: 20, // number of seconds to keep the room after everyone leaves
+ MaxParticipants: 0, // 0 == no limitation
+ },
+ )
+
+ if err != nil {
+ return fmt.Errorf("unable to create room %s: %w", room, err)
+ }
+
+ // Log the room creation time and the user info
+ isNewRoom := lkRoom.GetCreationTime() >= creationStart && lkRoom.GetCreationTime() <= time.Now().Unix()
+ log.Printf(
+ "%s LiveKit room sid: %s (alias: %s) for full-access Matrix user %s (LiveKit identity: %s)",
+ map[bool]string{true: "Created", false: "Using"}[isNewRoom],
+ lkRoom.Sid, room, matrixUser, lkIdentity,
+ )
+
+ return nil
+}
+
+func (h *Handler) prepareMux() *http.ServeMux {
+
+ mux := http.NewServeMux()
+ mux.HandleFunc("/sfu/get", h.handle_legacy) // TODO: This is deprecated and will be removed in future versions
+ mux.HandleFunc("/get_token", h.handle)
+ mux.HandleFunc("/healthz", h.healthcheck)
+
+ return mux
+}
+
+func (h *Handler) healthcheck(w http.ResponseWriter, r *http.Request) {
+ log.Printf("Health check from %s", r.RemoteAddr)
+
+ if r.Method == "GET" {
+ w.WriteHeader(http.StatusOK)
+ return
+ } else {
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ }
+}
+
+// TODO: This is deprecated and will be removed in future versions
+func mapSFURequest(data *[]byte) (any, error) {
+ requestTypes := []ValidatableSFURequest{&LegacySFURequest{}, &SFURequest{}}
+ for _, req := range requestTypes {
+ decoder := json.NewDecoder(strings.NewReader(string(*data)))
+ decoder.DisallowUnknownFields()
+ if err := decoder.Decode(req); err == nil {
+ if err := req.Validate(); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }
+ }
+
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusBadRequest,
+ ErrCode: "M_BAD_JSON",
+ Err: "The request body was malformed, missing required fields, or contained invalid values (e.g. missing `room_id`, `slot_id`, or `openid_token`).",
+ }
+}
+
+// TODO: This is deprecated and will be removed in future versions
+func (h *Handler) handle_legacy(w http.ResponseWriter, r *http.Request) {
+ log.Printf("Request from %s at \"%s\"", r.RemoteAddr, r.Header.Get("Origin"))
+
+ w.Header().Set("Content-Type", "application/json")
+
+ // Set the CORS headers
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+ w.Header().Set("Access-Control-Allow-Methods", "POST")
+ w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
+
+ switch r.Method {
+ case "OPTIONS":
+ // Handle preflight request (CORS)
+ w.WriteHeader(http.StatusOK)
+ return
+ case "POST":
+ // Read request body once for later JSON parsing
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ log.Printf("Error reading request body: %v", err)
+ writeMatrixError(w, http.StatusBadRequest, "M_NOT_JSON", "Error reading request")
+ return
+ }
+
+ var sfuAccessResponse *SFUResponse
+
+ sfuAccessRequest, err := mapSFURequest(&body)
+ if err != nil {
+ matrixErr := &MatrixErrorResponse{}
+ if errors.As(err, &matrixErr) {
+ log.Printf("Error processing request: %v", matrixErr.Err)
+ writeMatrixError(w, matrixErr.Status, matrixErr.ErrCode, matrixErr.Err)
+ return
+ }
+ }
+
+ switch sfuReq := sfuAccessRequest.(type) {
+ case *SFURequest:
+ log.Printf("Processing SFU request")
+ sfuAccessResponse, err = h.processSFURequest(r, sfuReq)
+ case *LegacySFURequest:
+ log.Printf("Processing legacy SFU request")
+ sfuAccessResponse, err = h.processLegacySFURequest(r, sfuReq)
+ }
+
+ if err != nil {
+ matrixErr := &MatrixErrorResponse{}
+ if errors.As(err, &matrixErr) {
+ log.Printf("Error processing request: %v", matrixErr.Err)
+ writeMatrixError(w, matrixErr.Status, matrixErr.ErrCode, matrixErr.Err)
+ return
+ }
+ }
+
+ if err := json.NewEncoder(w).Encode(&sfuAccessResponse); err != nil {
+ log.Printf("failed to encode json response! %v", err)
+ }
+ default:
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ }
+}
+
+func (h *Handler) handle(w http.ResponseWriter, r *http.Request) {
+ log.Printf("Request from %s at \"%s\"", r.RemoteAddr, r.Header.Get("Origin"))
+
+ w.Header().Set("Content-Type", "application/json")
+
+ // Set the CORS headers
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+ w.Header().Set("Access-Control-Allow-Methods", "POST")
+ w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
+
+ // Handle preflight request (CORS)
+ switch r.Method {
+ case "OPTIONS":
+ w.WriteHeader(http.StatusOK)
+ return
+ case "POST":
+ var sfuAccessRequest SFURequest
+
+ decoder := json.NewDecoder(r.Body)
+ decoder.DisallowUnknownFields()
+ if err := decoder.Decode(&sfuAccessRequest); err == nil {
+ if err := sfuAccessRequest.Validate(); err != nil {
+ matrixErr := &MatrixErrorResponse{}
+ if errors.As(err, &matrixErr) {
+ log.Printf("Error processing request: %v", matrixErr.Err)
+ writeMatrixError(w, matrixErr.Status, matrixErr.ErrCode, matrixErr.Err)
+ return
+ }
+ }
+ } else {
+ log.Printf("Error reading request body: %v", err)
+ writeMatrixError(w, http.StatusBadRequest, "M_NOT_JSON", "Error reading request")
+ return
+ }
+
+ log.Printf("Processing SFU request")
+ sfuAccessResponse, err := h.processSFURequest(r, &sfuAccessRequest)
+
+ if err != nil {
+ matrixErr := &MatrixErrorResponse{}
+ if errors.As(err, &matrixErr) {
+ log.Printf("Error processing request: %v", matrixErr.Err)
+ writeMatrixError(w, matrixErr.Status, matrixErr.ErrCode, matrixErr.Err)
+ return
+ }
+ }
+
+ if err := json.NewEncoder(w).Encode(&sfuAccessResponse); err != nil {
+ log.Printf("failed to encode json response! %v", err)
+ }
+
+ default:
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ }
+}
+
+func readKeySecret() (string, string) {
+ // We initialize keys & secrets from environment variables
+ key := os.Getenv("LIVEKIT_KEY")
+ secret := os.Getenv("LIVEKIT_SECRET")
+ // We initialize potential key & secret path from environment variables
+ keyPath := os.Getenv("LIVEKIT_KEY_FROM_FILE")
+ secretPath := os.Getenv("LIVEKIT_SECRET_FROM_FILE")
+ keySecretPath := os.Getenv("LIVEKIT_KEY_FILE")
+
+ // If keySecretPath is set we read the file and split it into two parts
+ // It takes over any other initialization
+ if keySecretPath != "" {
+ if keySecretBytes, err := os.ReadFile(keySecretPath); err != nil {
+ log.Fatal(err)
+ } else {
+ keySecrets := strings.Split(string(keySecretBytes), ":")
+ if len(keySecrets) != 2 {
+ log.Fatalf("invalid key secret file format!")
+ }
+ log.Printf("Using LiveKit API key and API secret from LIVEKIT_KEY_FILE")
+ key = keySecrets[0]
+ secret = keySecrets[1]
+ }
+ } else {
+ // If keySecretPath is not set, we try to read the key and secret from files
+ // If those files are not set, we return the key & secret from the environment variables
+ if keyPath != "" {
+ if keyBytes, err := os.ReadFile(keyPath); err != nil {
+ log.Fatal(err)
+ } else {
+ log.Printf("Using LiveKit API key from LIVEKIT_KEY_FROM_FILE")
+ key = string(keyBytes)
+ }
+ }
+
+ if secretPath != "" {
+ if secretBytes, err := os.ReadFile(secretPath); err != nil {
+ log.Fatal(err)
+ } else {
+ log.Printf("Using LiveKit API secret from LIVEKIT_SECRET_FROM_FILE")
+ secret = string(secretBytes)
+ }
+ }
+
+ }
+
+ // remove white spaces, new lines and carriage returns
+ // from key and secret
+ return strings.Trim(key, " \r\n"), strings.Trim(secret, " \r\n")
+}
+
+func parseConfig() (*Config, error) {
+ skipVerifyTLS := os.Getenv("LIVEKIT_INSECURE_SKIP_VERIFY_TLS") == "YES_I_KNOW_WHAT_I_AM_DOING"
+ if skipVerifyTLS {
+ log.Printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
+ log.Printf("!!! WARNING !!! LIVEKIT_INSECURE_SKIP_VERIFY_TLS !!! WARNING !!!")
+ log.Printf("!!! WARNING !!! Allow to skip invalid TLS certificates !!! WARNING !!!")
+ log.Printf("!!! WARNING !!! Use only for testing or debugging !!! WARNING !!!")
+ log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
+ }
+
+ key, secret := readKeySecret()
+ lkUrl := os.Getenv("LIVEKIT_URL")
+
+ if key == "" || secret == "" || lkUrl == "" {
+ return nil, fmt.Errorf("LIVEKIT_KEY[_FILE], LIVEKIT_SECRET[_FILE] and LIVEKIT_URL environment variables must be set")
+ }
+
+ fullAccessHomeservers := os.Getenv("LIVEKIT_FULL_ACCESS_HOMESERVERS")
+
+ if len(fullAccessHomeservers) == 0 {
+ localHomeservers := os.Getenv("LIVEKIT_LOCAL_HOMESERVERS")
+ if len(localHomeservers) > 0 {
+ log.Printf("!!! LIVEKIT_LOCAL_HOMESERVERS is deprecated, please use LIVEKIT_FULL_ACCESS_HOMESERVERS instead !!!")
+ fullAccessHomeservers = localHomeservers
+ } else {
+ log.Printf("LIVEKIT_FULL_ACCESS_HOMESERVERS not set, defaulting to wildcard (*) for full access")
+ fullAccessHomeservers = "*"
+ }
+ }
+
+ lkJwtBind := os.Getenv("LIVEKIT_JWT_BIND")
+ lkJwtPort := os.Getenv("LIVEKIT_JWT_PORT")
+
+ if lkJwtBind == "" {
+ if lkJwtPort == "" {
+ lkJwtPort = "8080"
+ } else {
+ log.Printf("!!! LIVEKIT_JWT_PORT is deprecated, please use LIVEKIT_JWT_BIND instead !!!")
+ }
+ lkJwtBind = fmt.Sprintf(":%s", lkJwtPort)
+ } else if lkJwtPort != "" {
+ return nil, fmt.Errorf("LIVEKIT_JWT_BIND and LIVEKIT_JWT_PORT environment variables MUST NOT be set together")
+ }
+
+ return &Config{
+ Key: key,
+ Secret: secret,
+ LkUrl: lkUrl,
+ SkipVerifyTLS: skipVerifyTLS,
+ FullAccessHomeservers: strings.Fields(strings.ReplaceAll(fullAccessHomeservers, ",", " ")),
+ LkJwtBind: lkJwtBind,
+ }, nil
+}
+
+func main() {
+ config, err := parseConfig()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ log.Printf("LIVEKIT_URL: %s, LIVEKIT_JWT_BIND: %s", config.LkUrl, config.LkJwtBind)
+ log.Printf("LIVEKIT_FULL_ACCESS_HOMESERVERS: %v", config.FullAccessHomeservers)
+
+ handler := &Handler{
+ key: config.Key,
+ secret: config.Secret,
+ lkUrl: config.LkUrl,
+ skipVerifyTLS: config.SkipVerifyTLS,
+ fullAccessHomeservers: config.FullAccessHomeservers,
+ }
+
+ log.Fatal(http.ListenAndServe(config.LkJwtBind, handler.prepareMux()))
+}
diff --git a/lk-jwt-service/main_test.go b/lk-jwt-service/main_test.go
new file mode 100644
index 0000000..993647c
--- /dev/null
+++ b/lk-jwt-service/main_test.go
@@ -0,0 +1,1083 @@
+// Copyright 2025 Element Creations Ltd.
+// Copyright 2025 New Vector Ltd.
+//
+// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
+// Please see LICENSE files in the repository root for full details.
+
+package main
+
+import (
+ "bytes"
+ "context"
+ "crypto/sha256"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "os"
+ "reflect"
+ "runtime"
+ "strings"
+ "testing"
+
+ "github.com/golang-jwt/jwt/v5"
+ "github.com/matrix-org/gomatrix"
+ "github.com/matrix-org/gomatrixserverlib/fclient"
+)
+
+func TestHealthcheck(t *testing.T) {
+ handler := &Handler{}
+ req, err := http.NewRequest("GET", "/healthz", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ handler.prepareMux().ServeHTTP(rr, req)
+
+ if status := rr.Code; status != http.StatusOK {
+ t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
+ }
+}
+
+func TestHandleOptions(t *testing.T) {
+ handler := &Handler{}
+ req, err := http.NewRequest("OPTIONS", "/sfu/get", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ handler.prepareMux().ServeHTTP(rr, req)
+
+ if status := rr.Code; status != http.StatusOK {
+ t.Errorf("handler returned wrong status code for OPTIONS: got %v want %v", status, http.StatusOK)
+ }
+
+ if accessControlAllowOrigin := rr.Header().Get("Access-Control-Allow-Origin"); accessControlAllowOrigin != "*" {
+ t.Errorf("handler returned wrong Access-Control-Allow-Origin: got %v want %v", accessControlAllowOrigin, "*")
+ }
+
+ if accessControlAllowMethods := rr.Header().Get("Access-Control-Allow-Methods"); accessControlAllowMethods != "POST" {
+ t.Errorf("handler returned wrong Access-Control-Allow-Methods: got %v want %v", accessControlAllowMethods, "POST")
+ }
+}
+
+func TestHandlePostMissingParams(t *testing.T) {
+ handler := &Handler{}
+
+ testCases := []map[string]interface{}{
+ {},
+ {
+ "room": "",
+ },
+ }
+
+ for _, testCase := range testCases {
+ jsonBody, _ := json.Marshal(testCase)
+
+ req, err := http.NewRequest("POST", "/sfu/get", bytes.NewBuffer(jsonBody))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ handler.prepareMux().ServeHTTP(rr, req)
+
+ if status := rr.Code; status != http.StatusBadRequest {
+ t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
+ }
+
+ var resp gomatrix.RespError
+ err = json.NewDecoder(rr.Body).Decode(&resp)
+ if err != nil {
+ t.Errorf("failed to decode response body %v", err)
+ }
+
+ if resp.ErrCode != "M_BAD_JSON" {
+ t.Errorf("unexpected error code: got %v want %v", resp.ErrCode, "M_BAD_JSON")
+ }
+ }
+}
+
+func TestHandlePost(t *testing.T) {
+ handler := &Handler{
+ secret: "testSecret",
+ key: "testKey",
+ lkUrl: "wss://lk.local:8080/foo",
+ fullAccessHomeservers: []string{"example.com"},
+ skipVerifyTLS: true,
+ }
+
+ var matrixServerName = ""
+
+ testServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ t.Log("Received request")
+ // Inspect the request
+ if r.URL.Path != "/_matrix/federation/v1/openid/userinfo" {
+ t.Errorf("unexpected request path: got %v want %v", r.URL.Path, "/_matrix/federation/v1/openid/userinfo")
+ }
+
+ if accessToken := r.URL.Query().Get("access_token"); accessToken != "testAccessToken" {
+ t.Errorf("unexpected access token: got %v want %v", accessToken, "testAccessToken")
+ }
+
+ // Mock response
+ w.WriteHeader(http.StatusOK)
+ w.Header().Set("Content-Type", "application/json")
+ _, err := fmt.Fprintf(w, `{"sub": "@user:%s"}`, matrixServerName)
+ if err != nil {
+ t.Fatalf("failed to write response: %v", err)
+ }
+ }))
+ defer testServer.Close()
+
+ u, _ := url.Parse(testServer.URL)
+
+ matrixServerName = u.Host
+
+ testCase := map[string]interface{}{
+ "room_id": "!testRoom:example.com",
+ "slot_id": "m.call#ROOM",
+ "openid_token": map[string]interface{}{
+ "access_token": "testAccessToken",
+ "token_type": "testTokenType",
+ "matrix_server_name": u.Host,
+ "expires_in": 3600,
+ },
+ "member": map[string]interface{}{
+ "id": "member_test_id",
+ "claimed_user_id": "@user:" + matrixServerName,
+ "claimed_device_id": "testDevice",
+ },
+ }
+
+ jsonBody, _ := json.Marshal(testCase)
+
+ req, err := http.NewRequest("POST", "/get_token", bytes.NewBuffer(jsonBody))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ handler.prepareMux().ServeHTTP(rr, req)
+
+ if status := rr.Code; status != http.StatusOK {
+ t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
+ }
+
+ if contentType := rr.Header().Get("Content-Type"); contentType != "application/json" {
+ t.Errorf("handler returned wrong Content-Type: got %v want %v", contentType, "application/json")
+ }
+
+ var resp SFUResponse
+ err = json.NewDecoder(rr.Body).Decode(&resp)
+ if err != nil {
+ t.Errorf("failed to decode response body %v", err)
+ }
+
+ if resp.URL != "wss://lk.local:8080/foo" {
+ t.Errorf("unexpected URL: got %v want %v", resp.URL, "wss://lk.local:8080/foo")
+ }
+
+ if resp.JWT == "" {
+ t.Error("expected JWT to be non-empty")
+ }
+
+ // parse JWT checking the shared secret
+ token, err := jwt.Parse(resp.JWT, func(token *jwt.Token) (interface{}, error) {
+ return []byte(handler.secret), nil
+ })
+
+ if err != nil {
+ t.Fatalf("failed to parse JWT: %v", err)
+ }
+
+ claims, ok := token.Claims.(jwt.MapClaims)
+
+ if !ok || !token.Valid {
+ t.Fatalf("failed to parse claims from JWT: %v", err)
+ }
+
+ want_sub_hash := sha256.Sum256([]byte("@user:"+ matrixServerName + "|testDevice|member_test_id"))
+ want_sub := unpaddedBase64.EncodeToString(want_sub_hash[:])
+ if claims["sub"] != want_sub {
+ t.Errorf("unexpected sub: got %v want %v", claims["sub"], "member_test_id")
+ }
+
+ // should have permission for the room
+ want_room_hash := sha256.Sum256([]byte("!testRoom:example.com" + "|" + "m.call#ROOM"))
+ want_room := unpaddedBase64.EncodeToString(want_room_hash[:])
+ if claims["video"].(map[string]interface{})["room"] != want_room {
+ t.Errorf("unexpected room: got %v want %v", claims["video"].(map[string]interface{})["room"], want_room)
+ }
+}
+
+func TestLegacyHandlePost(t *testing.T) {
+ handler := &Handler{
+ secret: "testSecret",
+ key: "testKey",
+ lkUrl: "wss://lk.local:8080/foo",
+ fullAccessHomeservers: []string{"example.com"},
+ skipVerifyTLS: true,
+ }
+
+ var matrixServerName = ""
+
+ testServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ t.Log("Received request")
+ // Inspect the request
+ if r.URL.Path != "/_matrix/federation/v1/openid/userinfo" {
+ t.Errorf("unexpected request path: got %v want %v", r.URL.Path, "/_matrix/federation/v1/openid/userinfo")
+ }
+
+ if accessToken := r.URL.Query().Get("access_token"); accessToken != "testAccessToken" {
+ t.Errorf("unexpected access token: got %v want %v", accessToken, "testAccessToken")
+ }
+
+ // Mock response
+ w.WriteHeader(http.StatusOK)
+ w.Header().Set("Content-Type", "application/json")
+ _, err := fmt.Fprintf(w, `{"sub": "@user:%s"}`, matrixServerName)
+ if err != nil {
+ t.Fatalf("failed to write response: %v", err)
+ }
+ }))
+ defer testServer.Close()
+
+ u, _ := url.Parse(testServer.URL)
+
+ matrixServerName = u.Host
+ matrixRoom := "testRoom"
+
+ testCase := map[string]interface{}{
+ "room": matrixRoom,
+ "openid_token": map[string]interface{}{
+ "access_token": "testAccessToken",
+ "token_type": "testTokenType",
+ "matrix_server_name": u.Host,
+ "expires_in": 3600,
+ },
+ "device_id": "testDevice",
+ }
+
+ jsonBody, _ := json.Marshal(testCase)
+
+ req, err := http.NewRequest("POST", "/sfu/get", bytes.NewBuffer(jsonBody))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ rr := httptest.NewRecorder()
+ handler.prepareMux().ServeHTTP(rr, req)
+
+ if status := rr.Code; status != http.StatusOK {
+ t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
+ }
+
+ if contentType := rr.Header().Get("Content-Type"); contentType != "application/json" {
+ t.Errorf("handler returned wrong Content-Type: got %v want %v", contentType, "application/json")
+ }
+
+ var resp SFUResponse
+ err = json.NewDecoder(rr.Body).Decode(&resp)
+ if err != nil {
+ t.Errorf("failed to decode response body %v", err)
+ }
+
+ if resp.URL != "wss://lk.local:8080/foo" {
+ t.Errorf("unexpected URL: got %v want %v", resp.URL, "wss://lk.local:8080/foo")
+ }
+
+ if resp.JWT == "" {
+ t.Error("expected JWT to be non-empty")
+ }
+
+ // parse JWT checking the shared secret
+ token, err := jwt.Parse(resp.JWT, func(token *jwt.Token) (interface{}, error) {
+ return []byte(handler.secret), nil
+ })
+
+ if err != nil {
+ t.Fatalf("failed to parse JWT: %v", err)
+ }
+
+ claims, ok := token.Claims.(jwt.MapClaims)
+
+ if !ok || !token.Valid {
+ t.Fatalf("failed to parse claims from JWT: %v", err)
+ }
+
+ if claims["sub"] != "@user:"+matrixServerName+":testDevice" {
+ t.Errorf("unexpected sub: got %v want %v", claims["sub"], "@user:"+matrixServerName+":testDevice")
+ }
+
+ slotId := "m.call#ROOM"
+ lkRoomAliasHash := sha256.Sum256([]byte(matrixRoom + "|" + slotId))
+ lkRoomAlias := unpaddedBase64.EncodeToString(lkRoomAliasHash[:])
+
+ // should have permission for the room
+ if claims["video"].(map[string]interface{})["room"] != lkRoomAlias {
+ t.Errorf("unexpected room: got %v want %v", claims["room"], lkRoomAlias)
+ }
+}
+
+func TestIsFullAccessUser(t *testing.T) {
+ handler := &Handler{
+ secret: "testSecret",
+ key: "testKey",
+ lkUrl: "wss://lk.local:8080/foo",
+ fullAccessHomeservers: []string{"example.com", "another.example.com"},
+ skipVerifyTLS: true,
+ }
+
+ // Test cases for full access users
+ if handler.isFullAccessUser("example.com") {
+ t.Log("User has full access")
+ } else {
+ t.Error("User has restricted access")
+ }
+
+ if handler.isFullAccessUser("another.example.com") {
+ t.Log("User has full access")
+ } else {
+ t.Error("User has restricted access")
+ }
+
+ // Test cases for restricted access users
+ if handler.isFullAccessUser("aanother.example.com") {
+ t.Error("User has full access")
+ } else {
+ t.Log("User has restricted access")
+ }
+
+ if handler.isFullAccessUser("matrix.example.com") {
+ t.Error("User has full access")
+ } else {
+ t.Log("User has restricted access")
+ }
+
+ // test wildcard access
+ handler.fullAccessHomeservers = []string{"*"}
+ if handler.isFullAccessUser("other.com") {
+ t.Log("User has full access")
+ } else {
+ t.Error("User has restricted access")
+ }
+}
+
+func TestGetJoinToken(t *testing.T) {
+ apiKey := "testKey"
+ apiSecret := "testSecret"
+ room := "testRoom"
+ identity := "testIdentity@example.com"
+
+ tokenString, err := getJoinToken(apiKey, apiSecret, room, identity)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ if tokenString == "" {
+ t.Error("expected token to be non-empty")
+ }
+
+ // parse JWT checking the shared secret
+ token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
+ return []byte(apiSecret), nil
+ })
+ claims, ok := token.Claims.(jwt.MapClaims)
+
+ if !ok || !token.Valid {
+ t.Fatalf("failed to parse claims from JWT: %v", err)
+ }
+
+ claimRoomCreate := claims["video"].(map[string]interface{})["roomCreate"]
+ if claimRoomCreate == nil {
+ claimRoomCreate = false
+ }
+
+ if claimRoomCreate == true {
+ t.Fatalf("roomCreate property needs to be false, since the lk-jwt-service creates the room")
+ }
+}
+
+func TestReadKeySecret(t *testing.T) {
+ testCases := []struct {
+ name string
+ env map[string]string
+ expectedKey string
+ expectedSecret string
+ err bool
+ }{
+ {
+ name: "Read from env",
+ env: map[string]string{
+ "LIVEKIT_KEY": "from_env_pheethiewixohp9eecheeGhuayeeph4l",
+ "LIVEKIT_SECRET": "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
+ },
+ expectedKey: "from_env_pheethiewixohp9eecheeGhuayeeph4l",
+ expectedSecret: "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
+ err: false,
+ },
+ {
+ name: "Read from livekit keysecret",
+ env: map[string]string{
+ "LIVEKIT_KEY_FILE": "./tests/keysecret.yaml",
+ },
+ expectedKey: "keysecret_iethuB2LeLiNuishiaKeephei9jaatio",
+ expectedSecret: "keysecret_xefaingo4oos6ohla9phiMieBu3ohJi2",
+ },
+ {
+ name: "Read from file",
+ env: map[string]string{
+ "LIVEKIT_KEY_FROM_FILE": "./tests/key",
+ "LIVEKIT_SECRET_FROM_FILE": "./tests/secret",
+ },
+ expectedKey: "from_file_oquusheiheiw4Iegah8te3Vienguus5a",
+ expectedSecret: "from_file_vohmahH3eeyieghohSh3kee8feuPhaim",
+ },
+ {
+ name: "Read from file key only",
+ env: map[string]string{
+ "LIVEKIT_KEY_FROM_FILE": "./tests/key",
+ "LIVEKIT_SECRET": "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
+ },
+ expectedKey: "from_file_oquusheiheiw4Iegah8te3Vienguus5a",
+ expectedSecret: "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
+ },
+ {
+ name: "Read from file secret only",
+ env: map[string]string{
+ "LIVEKIT_SECRET_FROM_FILE": "./tests/secret",
+ "LIVEKIT_KEY": "from_env_qui8aiTopiekiechah9oocbeimeew2O",
+ },
+ expectedKey: "from_env_qui8aiTopiekiechah9oocbeimeew2O",
+ expectedSecret: "from_file_vohmahH3eeyieghohSh3kee8feuPhaim",
+ },
+ {
+ name: "Empty if secret no env",
+ env: map[string]string{},
+ expectedKey: "",
+ expectedSecret: "",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ for k, v := range tc.env {
+ if err := os.Setenv(k, v); err != nil {
+ t.Errorf("Failed to set environment variable %s: %v", k, err)
+ }
+ }
+
+ key, secret := readKeySecret()
+ if secret != tc.expectedSecret || key != tc.expectedKey {
+ t.Errorf("Expected secret and key to be %s and %s but got %s and %s",
+ tc.expectedSecret,
+ tc.expectedKey,
+ secret,
+ key)
+ }
+ for k := range tc.env {
+ if err := os.Unsetenv(k); err != nil {
+ t.Errorf("Failed to unset environment variable %s: %v", k, err)
+ }
+ }
+ })
+ }
+}
+
+func TestParseConfig(t *testing.T) {
+ testCases := []struct {
+ name string
+ env map[string]string
+ wantConfig *Config
+ wantErrMsg string
+ }{
+ {
+ name: "Minimal valid config",
+ env: map[string]string{
+ "LIVEKIT_KEY": "test_key",
+ "LIVEKIT_SECRET": "test_secret",
+ "LIVEKIT_URL": "wss://test.livekit.cloud",
+ },
+ wantConfig: &Config{
+ Key: "test_key",
+ Secret: "test_secret",
+ LkUrl: "wss://test.livekit.cloud",
+ SkipVerifyTLS: false,
+ FullAccessHomeservers: []string{"*"},
+ LkJwtBind: ":8080",
+ },
+ },
+ {
+ name: "Full config with all options",
+ env: map[string]string{
+ "LIVEKIT_KEY": "test_key",
+ "LIVEKIT_SECRET": "test_secret",
+ "LIVEKIT_URL": "wss://test.livekit.cloud",
+ "LIVEKIT_FULL_ACCESS_HOMESERVERS": "example.com, test.com",
+ "LIVEKIT_JWT_BIND": ":9090",
+ "LIVEKIT_INSECURE_SKIP_VERIFY_TLS": "YES_I_KNOW_WHAT_I_AM_DOING",
+ },
+ wantConfig: &Config{
+ Key: "test_key",
+ Secret: "test_secret",
+ LkUrl: "wss://test.livekit.cloud",
+ SkipVerifyTLS: true,
+ FullAccessHomeservers: []string{"example.com", "test.com"},
+ LkJwtBind: ":9090",
+ },
+ },
+ {
+ name: "Legacy port configuration",
+ env: map[string]string{
+ "LIVEKIT_KEY": "test_key",
+ "LIVEKIT_SECRET": "test_secret",
+ "LIVEKIT_URL": "wss://test.livekit.cloud",
+ "LIVEKIT_JWT_PORT": "9090",
+ },
+ wantConfig: &Config{
+ Key: "test_key",
+ Secret: "test_secret",
+ LkUrl: "wss://test.livekit.cloud",
+ SkipVerifyTLS: false,
+ FullAccessHomeservers: []string{"*"},
+ LkJwtBind: ":9090",
+ },
+ },
+ {
+ name: "Legacy full-access homeservers configuration",
+ env: map[string]string{
+ "LIVEKIT_KEY": "test_key",
+ "LIVEKIT_SECRET": "test_secret",
+ "LIVEKIT_URL": "wss://test.livekit.cloud",
+ "LIVEKIT_LOCAL_HOMESERVERS": "legacy.com",
+ },
+ wantConfig: &Config{
+ Key: "test_key",
+ Secret: "test_secret",
+ LkUrl: "wss://test.livekit.cloud",
+ SkipVerifyTLS: false,
+ FullAccessHomeservers: []string{"legacy.com"},
+ LkJwtBind: ":8080",
+ },
+ },
+ {
+ name: "Missing required config",
+ env: map[string]string{
+ "LIVEKIT_KEY": "test_key",
+ },
+ wantErrMsg: "LIVEKIT_KEY[_FILE], LIVEKIT_SECRET[_FILE] and LIVEKIT_URL environment variables must be set",
+ },
+ {
+ name: "Conflicting bind configuration",
+ env: map[string]string{
+ "LIVEKIT_KEY": "test_key",
+ "LIVEKIT_SECRET": "test_secret",
+ "LIVEKIT_URL": "wss://test.livekit.cloud",
+ "LIVEKIT_JWT_BIND": ":9090",
+ "LIVEKIT_JWT_PORT": "8080",
+ },
+ wantErrMsg: "LIVEKIT_JWT_BIND and LIVEKIT_JWT_PORT environment variables MUST NOT be set together",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ // Setup: set env variables
+ for k, v := range tc.env {
+ if err := os.Setenv(k, v); err != nil {
+ t.Fatalf("Failed to set environment variable %s: %v", k, err)
+ }
+ }
+ defer func() {
+ // Cleanup: reset env variables after test
+ for k := range tc.env {
+ if err := os.Unsetenv(k); err != nil {
+ t.Errorf("Failed to unset environment variable %s: %v", k, err)
+ }
+ }
+ }()
+
+ // parse config from env variables
+ got, err := parseConfig()
+
+ // Given error(s), check potential error messages
+ if tc.wantErrMsg != "" {
+ if err == nil {
+ t.Errorf("parseConfig() error = nil, wantErr %q", tc.wantErrMsg)
+ return
+ }
+ if err.Error() != tc.wantErrMsg {
+ t.Errorf("parseConfig() error = %q, wantErr %q", err.Error(), tc.wantErrMsg)
+ }
+ return
+ }
+
+ // Given no error, check for unexpected error messages
+ if err != nil {
+ t.Errorf("parseConfig() unexpected error: %v", err)
+ return
+ }
+
+ // Compare parsed (got) config with wanted config
+ if got.Key != tc.wantConfig.Key {
+ t.Errorf("Key = %q, want %q", got.Key, tc.wantConfig.Key)
+ }
+ if got.Secret != tc.wantConfig.Secret {
+ t.Errorf("Secret = %q, want %q", got.Secret, tc.wantConfig.Secret)
+ }
+ if got.LkUrl != tc.wantConfig.LkUrl {
+ t.Errorf("LkUrl = %q, want %q", got.LkUrl, tc.wantConfig.LkUrl)
+ }
+ if got.SkipVerifyTLS != tc.wantConfig.SkipVerifyTLS {
+ t.Errorf("SkipVerifyTLS = %v, want %v", got.SkipVerifyTLS, tc.wantConfig.SkipVerifyTLS)
+ }
+ if !reflect.DeepEqual(got.FullAccessHomeservers, tc.wantConfig.FullAccessHomeservers) {
+ t.Errorf("FullAccessHomeservers = %v, want %v", got.FullAccessHomeservers, tc.wantConfig.FullAccessHomeservers)
+ }
+ if got.LkJwtBind != tc.wantConfig.LkJwtBind {
+ t.Errorf("JwtBind = %q, want %q", got.LkJwtBind, tc.wantConfig.LkJwtBind)
+ }
+ })
+ }
+}
+
+func TestMapSFURequest(t *testing.T) {
+ testCases := []struct {
+ name string
+ input string
+ want any
+ wantErrCode string
+ }{
+ {
+ name: "Valid legacy request",
+ input: `{
+ "room": "testRoom",
+ "openid_token": {
+ "access_token": "test_token",
+ "token_type": "Bearer",
+ "matrix_server_name": "example.com",
+ "expires_in": 3600
+ },
+ "device_id": "testDevice"
+ }`,
+ want: &LegacySFURequest{
+ Room: "testRoom",
+ OpenIDToken: OpenIDTokenType{
+ AccessToken: "test_token",
+ TokenType: "Bearer",
+ MatrixServerName: "example.com",
+ ExpiresIn: 3600,
+ },
+ DeviceID: "testDevice",
+ },
+ },
+ {
+ name: "Valid Matrix2 request",
+ input: `{
+ "room_id": "!testRoom:example.com",
+ "slot_id": "123",
+ "openid_token": {
+ "access_token": "test_token",
+ "token_type": "Bearer",
+ "matrix_server_name": "example.com",
+ "expires_in": 3600
+ },
+ "member": {
+ "id": "test_id",
+ "claimed_user_id": "@test:example.com",
+ "claimed_device_id": "testDevice"
+ }
+ }`,
+ want: &SFURequest{
+ RoomID: "!testRoom:example.com",
+ SlotID: "123",
+ OpenIDToken: OpenIDTokenType{
+ AccessToken: "test_token",
+ TokenType: "Bearer",
+ MatrixServerName: "example.com",
+ ExpiresIn: 3600,
+ },
+ Member: MatrixRTCMemberType{
+ ID: "test_id",
+ ClaimedUserID: "@test:example.com",
+ ClaimedDeviceID: "testDevice",
+ },
+ },
+ },
+ {
+ name: "Invalid JSON",
+ input: `{"invalid": json}`,
+ want: nil,
+ wantErrCode: "M_BAD_JSON",
+ },
+ {
+ name: "Empty request",
+ input: `{}`,
+ want: nil,
+ wantErrCode: "M_BAD_JSON",
+ },
+ {
+ name: "Invalid legacy request with extra field",
+ input: `{
+ "room": "testRoom",
+ "openid_token": {
+ "access_token": "test_token",
+ "token_type": "Bearer",
+ "matrix_server_name": "example.com",
+ "expires_in": 3600
+ },
+ "device_id": "testDevice",
+ "extra_field": "should_fail"
+ }`,
+ want: nil,
+ wantErrCode: "M_BAD_JSON",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ // Convert string to []byte for input
+ input := []byte(tc.input)
+
+ // Call mapSFURequest
+ got, err := mapSFURequest(&input)
+
+ // Check error cases
+ if tc.wantErrCode != "" {
+ matrixErr := &MatrixErrorResponse{}
+ if !errors.As(err, &matrixErr) {
+ t.Errorf("mapSFURequest() error = %v, want MatrixErrorResponse", err)
+ return
+ }
+ if matrixErr.ErrCode != tc.wantErrCode {
+ t.Errorf("mapSFURequest() error code = %v, want %v", matrixErr.ErrCode, tc.wantErrCode)
+ }
+ return
+ }
+
+ // Check success cases
+ if err != nil {
+ t.Errorf("mapSFURequest() unexpected error: %v", err)
+ return
+ }
+
+ // Type-specific comparisons
+ switch expected := tc.want.(type) {
+ case *LegacySFURequest:
+ actual, ok := got.(*LegacySFURequest)
+ if !ok {
+ t.Errorf("mapSFURequest() returned wrong type, got %T, want *LegacySFURequest", got)
+ return
+ }
+ if !reflect.DeepEqual(actual, expected) {
+ t.Errorf("mapSFURequest() = %+v, want %+v", actual, expected)
+ }
+ case *SFURequest:
+ actual, ok := got.(*SFURequest)
+ if !ok {
+ t.Errorf("mapSFURequest() returned wrong type, got %T, want *SFURequest", got)
+ return
+ }
+ if !reflect.DeepEqual(actual, expected) {
+ t.Errorf("mapSFURequest() = %+v, want %+v", actual, expected)
+ }
+ }
+ })
+ }
+}
+
+func TestMapSFURequestMemoryLeak(t *testing.T) {
+ const iterations = 100000
+
+ input := []byte(`{
+ "room_id": "!testRoom:example.com",
+ "slot_id": "123",
+ "openid_token": {
+ "access_token": "test_token",
+ "token_type": "Bearer",
+ "matrix_server_name": "example.com",
+ "expires_in": 3600
+ },
+ "member": {
+ "id": "test_id",
+ "claimed_user_id": "@test:example.com",
+ "claimed_device_id": "testDevice"
+ }
+ }`)
+
+ // Force a garbage collection to start from a clean slate.
+ var mStart, mEnd runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&mStart)
+
+ for i := 0; i < iterations; i++ {
+ _, err := mapSFURequest(&input)
+ if err != nil {
+ t.Fatalf("unexpected error in mapSFURequest iteration %d: %v", i, err)
+ }
+ }
+
+ // Force another GC to clear unreferenced memory
+ runtime.GC()
+ runtime.ReadMemStats(&mEnd)
+
+ t.Logf("Start Alloc: %d bytes, End Alloc: %d bytes", mStart.Alloc, mEnd.Alloc)
+
+ // Check that allocated heap hasnβt grown unboundedly
+ if mEnd.Alloc > mStart.Alloc {
+ allocDiff := mEnd.Alloc - mStart.Alloc
+ t.Logf("Heap allocation growth after %d iterations: %d bytes", iterations, allocDiff)
+
+ // Heuristic threshold: less than 100KB growth across 100k iterations is fine
+ const leakThreshold uint64 = 100 * 1024 // 100KB
+ if allocDiff > leakThreshold {
+ t.Errorf("Potential memory leak: heap grew by %d bytes (> %d)", allocDiff, leakThreshold)
+ }
+ }
+}
+
+func TestProcessSFURequest(t *testing.T) {
+ // mock createLiveKitRoom
+ var called_createLiveKitRoom bool
+ original_createLiveKitRoom := createLiveKitRoom
+ createLiveKitRoom = func(ctx context.Context, h *Handler, room, matrixUser, lkIdentity string) error {
+ called_createLiveKitRoom = true
+ if room == "" {
+ t.Error("expected room name passed into mock")
+ }
+ return nil
+ }
+ t.Cleanup(func() { createLiveKitRoom = original_createLiveKitRoom })
+
+ // mock OpenID lookup
+ var failed_exchangeOpenIdUserInfo bool
+ var exchangeOpenIdUserInfo_MatrixID string
+ original_exchangeOpenIdUserInfo := exchangeOpenIdUserInfo
+ exchangeOpenIdUserInfo = func(ctx context.Context, token OpenIDTokenType, skip bool) (*fclient.UserInfo, error) {
+ if failed_exchangeOpenIdUserInfo {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusUnauthorized,
+ ErrCode: "M_UNAUTHORIZED",
+ Err: "The request could not be authorised.",
+ }
+ }
+ return &fclient.UserInfo{Sub: exchangeOpenIdUserInfo_MatrixID}, nil
+ }
+ t.Cleanup(func() { exchangeOpenIdUserInfo = original_exchangeOpenIdUserInfo })
+
+ type testCase struct {
+ name string
+ MatrixID string
+ ClaimedMatrixID string
+ getJoinTokenErr error
+ expectJoinTokenError bool
+ expectExchangeOpendIdError bool
+ expectCreateRoomCall bool
+ expectError bool
+ exchangeErr error
+ }
+
+ tests := []testCase{
+ {
+ name: "Full access user + all OK",
+ MatrixID: "@user:example.com",
+ ClaimedMatrixID: "@user:example.com",
+ expectCreateRoomCall: true,
+ expectError: false,
+ },
+ {
+ name: "Restricted user + all OK",
+ MatrixID: "@user:otherdomain.com",
+ ClaimedMatrixID: "@user:otherdomain.com",
+ expectCreateRoomCall: false,
+ expectError: false,
+ },
+ {
+ name: "Full access user but exchangeOpenIdUserInfo fails",
+ MatrixID: "@user:example.com",
+ ClaimedMatrixID: "@user:example.com",
+ expectExchangeOpendIdError: true,
+ exchangeErr: &MatrixErrorResponse{},
+ expectCreateRoomCall: false,
+ expectError: true,
+ },
+ {
+ name: "Full access user but getJoinToken fails",
+ MatrixID: "@user:example.com",
+ ClaimedMatrixID: "@user:example.com",
+ expectJoinTokenError: true,
+ getJoinTokenErr: &MatrixErrorResponse{},
+ expectCreateRoomCall: false,
+ expectError: true,
+ },
+ {
+ name: "Full access user but claimed_matrix_id fails",
+ MatrixID: "@user:example.com",
+ ClaimedMatrixID: "@user:faked.com",
+ expectJoinTokenError: false,
+ getJoinTokenErr: &MatrixErrorResponse{},
+ expectCreateRoomCall: false,
+ expectError: true,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ // --- mock createLiveKitRoom ---
+ called_createLiveKitRoom = false
+ failed_exchangeOpenIdUserInfo = tc.expectExchangeOpendIdError
+ exchangeOpenIdUserInfo_MatrixID = tc.MatrixID
+
+ handler := &Handler{
+ key: map[bool]string{true: "", false: "the_api_key"}[tc.expectJoinTokenError],
+ secret: "secret",
+ lkUrl: "wss://lk.local:8080/foo",
+ fullAccessHomeservers: []string{"example.com"},
+ }
+
+ req := &SFURequest{
+ RoomID: "!room:example.com",
+ SlotID: "slot",
+ OpenIDToken: OpenIDTokenType{
+ AccessToken: "token",
+ MatrixServerName: strings.Split(tc.ClaimedMatrixID, ":")[1],
+ },
+ Member: MatrixRTCMemberType{
+ ID: "device",
+ ClaimedUserID: tc.ClaimedMatrixID,
+ ClaimedDeviceID: "dev",
+ },
+ }
+
+ _, err := handler.processSFURequest(&http.Request{}, req)
+ if tc.expectError && err == nil {
+ t.Fatalf("expected error but got nil")
+ }
+ if !tc.expectError && err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ if called_createLiveKitRoom != tc.expectCreateRoomCall {
+ t.Errorf("expected createLiveKitRoom called=%v, got %v", tc.expectCreateRoomCall, called_createLiveKitRoom)
+ }
+
+ })
+ }
+
+
+}
+
+func TestProcessLegacySFURequest(t *testing.T) {
+ // mock createLiveKitRoom
+ var called_createLiveKitRoom bool
+ original_createLiveKitRoom := createLiveKitRoom
+ createLiveKitRoom = func(ctx context.Context, h *Handler, room, matrixUser, lkIdentity string) error {
+ called_createLiveKitRoom = true
+ if room == "" {
+ t.Error("expected room name passed into mock")
+ }
+ return nil
+ }
+ t.Cleanup(func() { createLiveKitRoom = original_createLiveKitRoom })
+
+ // mock OpenID lookup
+ var failed_exchangeOpenIdUserInfo bool
+ original_exchangeOpenIdUserInfo := exchangeOpenIdUserInfo
+ exchangeOpenIdUserInfo = func(ctx context.Context, token OpenIDTokenType, skip bool) (*fclient.UserInfo, error) {
+ if failed_exchangeOpenIdUserInfo {
+ return nil, &MatrixErrorResponse{
+ Status: http.StatusUnauthorized,
+ ErrCode: "M_UNAUTHORIZED",
+ Err: "The request could not be authorised.",
+ }
+ }
+ return &fclient.UserInfo{Sub: "@mock:example.com"}, nil
+ }
+ t.Cleanup(func() { exchangeOpenIdUserInfo = original_exchangeOpenIdUserInfo })
+
+ type testCase struct {
+ name string
+ MatrixID string
+ getJoinTokenErr error
+ expectJoinTokenError bool
+ expectExchangeOpendIdError bool
+ expectCreateRoomCall bool
+ expectError bool
+ exchangeErr error
+ }
+
+ tests := []testCase{
+ {
+ name: "Full access user + all OK",
+ MatrixID: "@user:example.com",
+ expectCreateRoomCall: true,
+ expectError: false,
+ },
+ {
+ name: "Restricted user + all OK",
+ MatrixID: "@user:otherdomain.com",
+ expectCreateRoomCall: false,
+ expectError: false,
+ },
+ {
+ name: "Full access user but exchangeOpenIdUserInfo fails",
+ MatrixID: "@user:example.com",
+ expectExchangeOpendIdError: true,
+ exchangeErr: &MatrixErrorResponse{},
+ expectCreateRoomCall: false,
+ expectError: true,
+ },
+ {
+ name: "Full access user but getJoinToken fails",
+ MatrixID: "@user:example.com",
+ expectJoinTokenError: true,
+ getJoinTokenErr: &MatrixErrorResponse{},
+ expectCreateRoomCall: false,
+ expectError: true,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ // --- mock createLiveKitRoom ---
+ called_createLiveKitRoom = false
+ failed_exchangeOpenIdUserInfo = tc.expectExchangeOpendIdError
+
+ handler := &Handler{
+ key: map[bool]string{true: "", false: "the_api_key"}[tc.expectJoinTokenError],
+ secret: "secret",
+ lkUrl: "wss://lk.local:8080/foo",
+ fullAccessHomeservers: []string{"example.com"},
+ }
+
+ req := &LegacySFURequest{
+ Room: "!room:example.com",
+ OpenIDToken: OpenIDTokenType{
+ AccessToken: "token",
+ MatrixServerName: strings.Split(tc.MatrixID, ":")[1],
+ },
+ DeviceID: "dev",
+ }
+
+ _, err := handler.processLegacySFURequest(&http.Request{}, req)
+ if tc.expectError && err == nil {
+ t.Fatalf("expected error but got nil")
+ }
+ if !tc.expectError && err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ if called_createLiveKitRoom != tc.expectCreateRoomCall {
+ t.Errorf("expected createLiveKitRoom called=%v, got %v", tc.expectCreateRoomCall, called_createLiveKitRoom)
+ }
+
+ })
+ }
+
+
+}
diff --git a/lk-jwt-service/renovate.json b/lk-jwt-service/renovate.json
new file mode 100644
index 0000000..068c0ea
--- /dev/null
+++ b/lk-jwt-service/renovate.json
@@ -0,0 +1,25 @@
+{
+ "$schema": "https://docs.renovatebot.com/renovate-schema.json",
+ "extends": [
+ "config:recommended",
+ "schedule:monthly",
+ "helpers:pinGitHubActionDigestsToSemver",
+ ":enableVulnerabilityAlertsWithLabel(security)"
+ ],
+ "addLabels": ["dependencies"],
+ "vulnerabilityAlerts": {
+ "schedule": [
+ "at any time"
+ ],
+ "prHourlyLimit": 0,
+ "minimumReleaseAge": null
+ },
+ "packageRules": [
+ {
+ "groupName": "GitHub Actions",
+ "matchDepTypes": ["action"],
+ "pinDigests": true
+ }
+ ],
+ "minimumReleaseAge": "5 days"
+}
diff --git a/lk-jwt-service/tests/key b/lk-jwt-service/tests/key
new file mode 100644
index 0000000..25f83e1
--- /dev/null
+++ b/lk-jwt-service/tests/key
@@ -0,0 +1 @@
+from_file_oquusheiheiw4Iegah8te3Vienguus5a
diff --git a/lk-jwt-service/tests/keysecret.yaml b/lk-jwt-service/tests/keysecret.yaml
new file mode 100644
index 0000000..29d1c7b
--- /dev/null
+++ b/lk-jwt-service/tests/keysecret.yaml
@@ -0,0 +1 @@
+keysecret_iethuB2LeLiNuishiaKeephei9jaatio: keysecret_xefaingo4oos6ohla9phiMieBu3ohJi2
diff --git a/lk-jwt-service/tests/secret b/lk-jwt-service/tests/secret
new file mode 100644
index 0000000..58bf88f
--- /dev/null
+++ b/lk-jwt-service/tests/secret
@@ -0,0 +1 @@
+from_file_vohmahH3eeyieghohSh3kee8feuPhaim