|
| 1 | +/* |
| 2 | + * Copyright 2025 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import JWTKit |
| 18 | + |
| 19 | +public struct LiveKitJWTPayload: JWTPayload, Codable, Equatable { |
| 20 | + public struct VideoGrant: Codable, Equatable { |
| 21 | + /// Name of the room, must be set for admin or join permissions |
| 22 | + public let room: String? |
| 23 | + /// Permission to create a room |
| 24 | + public let roomCreate: Bool? |
| 25 | + /// Permission to join a room as a participant, room must be set |
| 26 | + public let roomJoin: Bool? |
| 27 | + /// Permission to list rooms |
| 28 | + public let roomList: Bool? |
| 29 | + /// Permission to start a recording |
| 30 | + public let roomRecord: Bool? |
| 31 | + /// Permission to control a specific room, room must be set |
| 32 | + public let roomAdmin: Bool? |
| 33 | + |
| 34 | + /// Allow participant to publish. If neither canPublish or canSubscribe is set, both publish and subscribe are enabled |
| 35 | + public let canPublish: Bool? |
| 36 | + /// Allow participant to subscribe to other tracks |
| 37 | + public let canSubscribe: Bool? |
| 38 | + /// Allow participants to publish data, defaults to true if not set |
| 39 | + public let canPublishData: Bool? |
| 40 | + /// Allowed sources for publishing |
| 41 | + public let canPublishSources: [String]? |
| 42 | + /// Participant isn't visible to others |
| 43 | + public let hidden: Bool? |
| 44 | + /// Participant is recording the room, when set, allows room to indicate it's being recorded |
| 45 | + public let recorder: Bool? |
| 46 | + |
| 47 | + public init(room: String? = nil, |
| 48 | + roomCreate: Bool? = nil, |
| 49 | + roomJoin: Bool? = nil, |
| 50 | + roomList: Bool? = nil, |
| 51 | + roomRecord: Bool? = nil, |
| 52 | + roomAdmin: Bool? = nil, |
| 53 | + canPublish: Bool? = nil, |
| 54 | + canSubscribe: Bool? = nil, |
| 55 | + canPublishData: Bool? = nil, |
| 56 | + canPublishSources: [String]? = nil, |
| 57 | + hidden: Bool? = nil, |
| 58 | + recorder: Bool? = nil) |
| 59 | + { |
| 60 | + self.room = room |
| 61 | + self.roomCreate = roomCreate |
| 62 | + self.roomJoin = roomJoin |
| 63 | + self.roomList = roomList |
| 64 | + self.roomRecord = roomRecord |
| 65 | + self.roomAdmin = roomAdmin |
| 66 | + self.canPublish = canPublish |
| 67 | + self.canSubscribe = canSubscribe |
| 68 | + self.canPublishData = canPublishData |
| 69 | + self.canPublishSources = canPublishSources |
| 70 | + self.hidden = hidden |
| 71 | + self.recorder = recorder |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Expiration time claim |
| 76 | + public let exp: ExpirationClaim |
| 77 | + /// Issuer claim |
| 78 | + public let iss: IssuerClaim |
| 79 | + /// Not before claim |
| 80 | + public let nbf: NotBeforeClaim |
| 81 | + /// Subject claim |
| 82 | + public let sub: SubjectClaim |
| 83 | + |
| 84 | + /// Participant name |
| 85 | + public let name: String? |
| 86 | + /// Participant metadata |
| 87 | + public let metadata: String? |
| 88 | + /// Video grants for the participant |
| 89 | + public let video: VideoGrant? |
| 90 | + |
| 91 | + public func verify(using _: JWTSigner) throws { |
| 92 | + try nbf.verifyNotBefore() |
| 93 | + try exp.verifyNotExpired() |
| 94 | + } |
| 95 | + |
| 96 | + static func fromUnverified(token: String) -> Self? { |
| 97 | + try? JWTSigners().unverified(token, as: Self.self) |
| 98 | + } |
| 99 | +} |
0 commit comments