Files
fdf40b94a3 feat: add token revocation list (#4344)
* feat: add token revocation

* token revocation: match LiveKit Cloud API, make it optional

- add token_revocation config section (enabled: false by default, ttl)
- RemoveParticipant: cutoff defaults to now, revoke_token_ts must be within
  60s and is stored with the validation leeway, revoke absent participants,
  keep not found unless revoke_token_ts is given
- UpdateParticipant revokes older tokens on permission updates and the
  participant gets a refreshed token
- share the RoomService logic with Cloud through RevokeTokenClient
- revocations merge to the later cutoff and expiry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* token revocation: clean up expired revocations lazily

Drop the cleanup goroutine of LocalStore. Revocations are kept in a queue
ordered by expiry, and expired ones are removed from its front when
revoking or checking, at most once per minute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* token revocation: request context for redis, own lock for LocalStore

- RedisStore revocation calls use the request context
- LocalStore revocations use their own lock instead of the store lock
- remove TestRTCValidateRoomlessTokenAfterRevocation, which passes without revocation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* token revocation: length checks before auth, test all auth chains

Check lengths before the admin permission check, as LiveKit Cloud does, and
test that the API, agent endpoint and WebTransport chains reject revoked tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* token revocation: do not return store errors to clients

Log the error of a failed revocation lookup and respond with a fixed message,
as the store error may contain internal addresses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: cnderrauber <zengjie9004@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 21:55:28 +08:00

128 lines
6.3 KiB
Go

// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"context"
"time"
"github.com/livekit/protocol/livekit"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
// encapsulates CRUD operations for room settings
//
//counterfeiter:generate . ObjectStore
type ObjectStore interface {
ServiceStore
OSSServiceStore
// enable locking on a specific room to prevent race
// returns a (lock uuid, error)
LockRoom(ctx context.Context, roomName livekit.RoomName, duration time.Duration) (string, error)
UnlockRoom(ctx context.Context, roomName livekit.RoomName, uid string) error
StoreRoom(ctx context.Context, room *livekit.Room, internal *livekit.RoomInternal) error
StoreParticipant(ctx context.Context, roomName livekit.RoomName, participant *livekit.ParticipantInfo) error
DeleteParticipant(ctx context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity) error
}
//counterfeiter:generate . ServiceStore
type ServiceStore interface {
LoadRoom(ctx context.Context, roomName livekit.RoomName, includeInternal bool) (*livekit.Room, *livekit.RoomInternal, error)
RoomExists(ctx context.Context, roomName livekit.RoomName) (bool, error)
// ListRooms returns currently active rooms. if names is not nil, it'll filter and return
// only rooms that match
ListRooms(ctx context.Context, roomNames []livekit.RoomName) ([]*livekit.Room, error)
}
type OSSServiceStore interface {
DeleteRoom(ctx context.Context, roomName livekit.RoomName) error
HasParticipant(context.Context, livekit.RoomName, livekit.ParticipantIdentity) (bool, error)
LoadParticipant(ctx context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity) (*livekit.ParticipantInfo, error)
ListParticipants(ctx context.Context, roomName livekit.RoomName) ([]*livekit.ParticipantInfo, error)
}
//counterfeiter:generate . EgressStore
type EgressStore interface {
StoreEgress(ctx context.Context, info *livekit.EgressInfo) error
LoadEgress(ctx context.Context, egressID string) (*livekit.EgressInfo, error)
ListEgress(ctx context.Context, roomName livekit.RoomName, active bool) ([]*livekit.EgressInfo, error)
UpdateEgress(ctx context.Context, info *livekit.EgressInfo) error
}
//counterfeiter:generate . IngressStore
type IngressStore interface {
StoreIngress(ctx context.Context, info *livekit.IngressInfo) error
LoadIngress(ctx context.Context, ingressID string) (*livekit.IngressInfo, error)
LoadIngressFromStreamKey(ctx context.Context, streamKey string) (*livekit.IngressInfo, error)
ListIngress(ctx context.Context, roomName livekit.RoomName) ([]*livekit.IngressInfo, error)
UpdateIngress(ctx context.Context, info *livekit.IngressInfo) error
UpdateIngressState(ctx context.Context, ingressId string, state *livekit.IngressState) error
DeleteIngress(ctx context.Context, info *livekit.IngressInfo) error
}
//counterfeiter:generate . RoomAllocator
type RoomAllocator interface {
AutoCreateEnabled(ctx context.Context) bool
SelectRoomNode(ctx context.Context, roomName livekit.RoomName, nodeID livekit.NodeID) error
// CreateRoom also returns the request with its room preset applied,
// or req unchanged when the implementation does not resolve presets
CreateRoom(ctx context.Context, req *livekit.CreateRoomRequest, isExplicit bool) (*livekit.Room, *livekit.RoomInternal, *livekit.CreateRoomRequest, bool, error)
ValidateCreateRoom(ctx context.Context, roomName livekit.RoomName) error
}
//counterfeiter:generate . SIPStore
type SIPStore interface {
StoreSIPTrunk(ctx context.Context, info *livekit.SIPTrunkInfo) error
StoreSIPInboundTrunk(ctx context.Context, info *livekit.SIPInboundTrunkInfo) error
StoreSIPOutboundTrunk(ctx context.Context, info *livekit.SIPOutboundTrunkInfo) error
LoadSIPTrunk(ctx context.Context, sipTrunkID string) (*livekit.SIPTrunkInfo, error)
LoadSIPInboundTrunk(ctx context.Context, sipTrunkID string) (*livekit.SIPInboundTrunkInfo, error)
LoadSIPOutboundTrunk(ctx context.Context, sipTrunkID string) (*livekit.SIPOutboundTrunkInfo, error)
ListSIPTrunk(ctx context.Context, opts *livekit.ListSIPTrunkRequest) (*livekit.ListSIPTrunkResponse, error)
ListSIPInboundTrunk(ctx context.Context, opts *livekit.ListSIPInboundTrunkRequest) (*livekit.ListSIPInboundTrunkResponse, error)
ListSIPOutboundTrunk(ctx context.Context, opts *livekit.ListSIPOutboundTrunkRequest) (*livekit.ListSIPOutboundTrunkResponse, error)
DeleteSIPTrunk(ctx context.Context, sipTrunkID string) error
StoreSIPDispatchRule(ctx context.Context, info *livekit.SIPDispatchRuleInfo) error
LoadSIPDispatchRule(ctx context.Context, sipDispatchRuleID string) (*livekit.SIPDispatchRuleInfo, error)
ListSIPDispatchRule(ctx context.Context, opts *livekit.ListSIPDispatchRuleRequest) (*livekit.ListSIPDispatchRuleResponse, error)
DeleteSIPDispatchRule(ctx context.Context, sipDispatchRuleID string) error
}
//counterfeiter:generate . AgentStore
type AgentStore interface {
StoreAgentDispatch(ctx context.Context, dispatch *livekit.AgentDispatch) error
DeleteAgentDispatch(ctx context.Context, dispatch *livekit.AgentDispatch) error
ListAgentDispatches(ctx context.Context, roomName livekit.RoomName) ([]*livekit.AgentDispatch, error)
StoreAgentJob(ctx context.Context, job *livekit.Job) error
DeleteAgentJob(ctx context.Context, job *livekit.Job) error
}
type RevokeTokenClient interface {
IsRevokeTokenEnabled(ctx context.Context, room livekit.RoomName) bool
RevokeToken(ctx context.Context, room livekit.RoomName, identity livekit.ParticipantIdentity, notBefore time.Time) error
}
type TokenRevocationStore interface {
RevokeRoomParticipant(ctx context.Context, room livekit.RoomName, identity livekit.ParticipantIdentity, notBefore time.Time, ttl time.Duration) error
IsRoomParticipantRevoked(ctx context.Context, identity livekit.ParticipantIdentity, room livekit.RoomName) (bool, *time.Time, error)
}