Skip to content

Commit bfdb580

Browse files
committed
JWT logic
1 parent 68c05d8 commit bfdb580

File tree

5 files changed

+77
-151
lines changed

5 files changed

+77
-151
lines changed

Sources/LiveKit/Token/CachingTokenSource.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
internal import LiveKitFFI
1718
import Foundation
1819

1920
/// A token source that caches credentials from any other ``TokenSourceConfigurable`` using a configurable store.
@@ -148,20 +149,27 @@ public extension TokenSourceResponse {
148149
return false
149150
}
150151

151-
do {
152-
try jwt.nbf.verifyNotBefore()
153-
try jwt.exp.verifyNotExpired(currentDate: Date().addingTimeInterval(tolerance))
154-
} catch {
155-
return false
156-
}
157-
158-
return true
152+
return jwt.nbf.verifyNotBefore() && jwt.exp.verifyNotExpired(Date().addingTimeInterval(tolerance))
159153
}
160154

161155
/// Extracts the JWT payload from the participant token.
162156
///
163157
/// - Returns: The JWT payload if successfully parsed, nil otherwise
164-
internal func jwt() -> LiveKitJWTPayload? {
165-
LiveKitJWTPayload.fromUnverified(token: participantToken)
158+
internal func jwt() -> Claims? {
159+
try? tokenClaimsFromUnverified(token: participantToken)
160+
}
161+
}
162+
163+
private extension UInt64 {
164+
var asDate: Date {
165+
Date(timeIntervalSince1970: TimeInterval(self))
166+
}
167+
168+
func verifyNotBefore(_ date: Date = Date()) -> Bool {
169+
asDate >= date
170+
}
171+
172+
func verifyNotExpired(_ date: Date = Date()) -> Bool {
173+
asDate < date
166174
}
167175
}

Sources/LiveKit/Token/JWT.swift

Lines changed: 0 additions & 107 deletions
This file was deleted.

Tests/LiveKitCoreTests/Token/TokenSourceTests.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#if canImport(LiveKitTestSupport)
1919
import LiveKitTestSupport
2020
#endif
21+
import LiveKitFFI
2122

2223
class TokenSourceTests: LKTestCase {
2324
actor MockValidJWTSource: TokenSourceConfigurable {
@@ -38,7 +39,23 @@ class TokenSourceTests: LKTestCase {
3839
identity: options.participantIdentity ?? "test-identity"
3940
)
4041
tokenGenerator.name = options.participantName ?? participantName
41-
tokenGenerator.videoGrant = LiveKitJWTPayload.VideoGrant(room: options.roomName ?? "test-room", roomJoin: true)
42+
tokenGenerator.videoGrants = VideoGrants(
43+
roomCreate: false,
44+
roomList: false,
45+
roomRecord: false,
46+
roomAdmin: false,
47+
roomJoin: true,
48+
room: options.roomName ?? "test-room",
49+
destinationRoom: "",
50+
canPublish: false,
51+
canSubscribe: false,
52+
canPublishData: false,
53+
canPublishSources: [],
54+
canUpdateOwnMetadata: false,
55+
ingressAdmin: false,
56+
hidden: false,
57+
recorder: false
58+
)
4259

4360
let token = try tokenGenerator.sign()
4461

@@ -74,10 +91,26 @@ class TokenSourceTests: LKTestCase {
7491
apiKey: "test-api-key",
7592
apiSecret: "test-api-secret",
7693
identity: options.participantIdentity ?? "test-identity",
77-
ttl: -60
94+
ttl: 0
7895
)
7996
tokenGenerator.name = options.participantName ?? "test-participant"
80-
tokenGenerator.videoGrant = LiveKitJWTPayload.VideoGrant(room: options.roomName ?? "test-room", roomJoin: true)
97+
tokenGenerator.videoGrants = VideoGrants(
98+
roomCreate: false,
99+
roomList: false,
100+
roomRecord: false,
101+
roomAdmin: false,
102+
roomJoin: true,
103+
room: options.roomName ?? "test-room",
104+
destinationRoom: "",
105+
canPublish: false,
106+
canSubscribe: false,
107+
canPublishData: false,
108+
canPublishSources: [],
109+
canUpdateOwnMetadata: false,
110+
ingressAdmin: false,
111+
hidden: false,
112+
recorder: false
113+
)
81114

82115
let token = try tokenGenerator.sign()
83116

Tests/LiveKitTestSupport/Room.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
@testable import LiveKit
18+
import LiveKitFFI
1819

1920
public struct RoomTestingOptions {
2021
public let delegate: RoomDelegate?
@@ -78,12 +79,24 @@ public extension LKTestCase {
7879
apiSecret: apiSecret,
7980
identity: identity)
8081

81-
tokenGenerator.videoGrant = LiveKitJWTPayload.VideoGrant(room: room,
82-
roomJoin: true,
83-
canPublish: canPublish,
84-
canSubscribe: canSubscribe,
85-
canPublishData: canPublishData,
86-
canPublishSources: canPublishSources.map(String.init))
82+
tokenGenerator.videoGrants = VideoGrants(
83+
roomCreate: false,
84+
roomList: false,
85+
roomRecord: false,
86+
roomAdmin: false,
87+
roomJoin: true,
88+
room: room,
89+
destinationRoom: "",
90+
canPublish: canPublish,
91+
canSubscribe: canSubscribe,
92+
canPublishData: canPublishData,
93+
canPublishSources: canPublishSources.map(String.init),
94+
canUpdateOwnMetadata: false,
95+
ingressAdmin: false,
96+
hidden: false,
97+
recorder: false
98+
)
99+
87100
return try tokenGenerator.sign()
88101
}
89102

Tests/LiveKitTestSupport/TokenGenerator.swift

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class TokenGenerator {
2929
public var ttl: TimeInterval
3030
public var name: String?
3131
public var metadata: String?
32-
public var videoGrant: LiveKitJWTPayload.VideoGrant?
32+
public var videoGrants: VideoGrants?
3333

3434
public init(apiKey: String,
3535
apiSecret: String,
@@ -43,40 +43,19 @@ public class TokenGenerator {
4343
}
4444

4545
public func sign() throws -> String {
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-
}
66-
6746
let credentials = ApiCredentials(key: apiKey, secret: apiSecret)
6847
let options = TokenOptions(
6948
ttl: ttl,
70-
videoGrants: ffiVideoGrants,
49+
videoGrants: videoGrants,
7150
sipGrants: nil,
7251
identity: identity,
7352
name: name,
7453
metadata: metadata,
7554
attributes: nil,
7655
sha256: nil,
77-
roomName: videoGrant?.room
56+
roomName: videoGrants?.room
7857
)
7958

80-
return try generateToken(options: options, credentials: credentials)
59+
return try tokenGenerate(options: options, credentials: credentials)
8160
}
8261
}

0 commit comments

Comments
 (0)