1414 * limitations under the License.
1515 */
1616
17- import JWTKit
1817@testable import LiveKit
18+ import LiveKitFFI
1919
2020public class TokenGenerator {
2121 // 30 mins
@@ -31,10 +31,6 @@ public class TokenGenerator {
3131 public var metadata : String ?
3232 public var videoGrant : LiveKitJWTPayload . VideoGrant ?
3333
34- // MARK: - Private
35-
36- private let signers = JWTSigners ( )
37-
3834 public init ( apiKey: String ,
3935 apiSecret: String ,
4036 identity: String ,
@@ -47,19 +43,40 @@ public class TokenGenerator {
4743 }
4844
4945 public func sign( ) throws -> String {
50- // Add HMAC with SHA-256 signer.
51- signers. use ( . hs256( key: apiSecret) )
52-
53- let n = Date ( ) . timeIntervalSince1970
46+ var ffiVideoGrants : VideoGrants ?
47+ if let grant = videoGrant {
48+ ffiVideoGrants = VideoGrants (
49+ roomCreate: grant. roomCreate ?? false ,
50+ roomList: grant. roomList ?? false ,
51+ roomRecord: grant. roomRecord ?? false ,
52+ roomAdmin: grant. roomAdmin ?? false ,
53+ roomJoin: grant. roomJoin ?? false ,
54+ room: grant. room ?? " " ,
55+ destinationRoom: " " ,
56+ canPublish: grant. canPublish ?? false ,
57+ canSubscribe: grant. canSubscribe ?? false ,
58+ canPublishData: grant. canPublishData ?? false ,
59+ canPublishSources: grant. canPublishSources ?? [ ] ,
60+ canUpdateOwnMetadata: false ,
61+ ingressAdmin: false ,
62+ hidden: grant. hidden ?? false ,
63+ recorder: grant. recorder ?? false
64+ )
65+ }
5466
55- let p = LiveKitJWTPayload ( exp: . init( value: Date ( timeIntervalSince1970: floor ( n + ttl) ) ) ,
56- iss: . init( stringLiteral: apiKey) ,
57- nbf: . init( value: Date ( timeIntervalSince1970: floor ( n) ) ) ,
58- sub: . init( stringLiteral: identity) ,
59- name: name,
60- metadata: metadata,
61- video: videoGrant)
67+ let credentials = ApiCredentials ( key: apiKey, secret: apiSecret)
68+ let options = TokenOptions (
69+ ttl: ttl,
70+ videoGrants: ffiVideoGrants,
71+ sipGrants: nil ,
72+ identity: identity,
73+ name: name,
74+ metadata: metadata,
75+ attributes: nil ,
76+ sha256: nil ,
77+ roomName: videoGrant? . room
78+ )
6279
63- return try signers . sign ( p )
80+ return try generateToken ( options : options , credentials : credentials )
6481 }
6582}
0 commit comments