Skip to content

Commit

Permalink
fix(realtime): remove jwt check
Browse files Browse the repository at this point in the history
Also:
- Call `setAuth` after heartbeat
- Send `x-client-info` when joining channel
  • Loading branch information
grdsdev committed Feb 7, 2025
1 parent dea204f commit c74ce06
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Sources/Realtime/RealtimeChannelV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public final class RealtimeChannelV2: Sendable {

let payload = RealtimeJoinPayload(
config: joinConfig,
accessToken: await socket._getAccessToken()
accessToken: await socket._getAccessToken(),
version: socket.options.headers[.xClientInfo]
)

let joinRef = socket.makeRef()
Expand Down
20 changes: 9 additions & 11 deletions Sources/Realtime/RealtimeClientV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,18 @@ public final class RealtimeClientV2: Sendable {
wsTransport: @escaping WebSocketTransport,
http: any HTTPClientType
) {
var options = options
if options.headers[.xClientInfo] == nil {
options.headers[.xClientInfo] = "realtime-swift/\(version)"
}

self.url = url
self.options = options
self.wsTransport = wsTransport
self.http = http
apikey = options.apikey

mutableState.withValue {
mutableState.withValue { [options] in
if let accessToken = options.headers[.authorization]?.split(separator: " ").last {
$0.accessToken = String(accessToken)
} else {
Expand Down Expand Up @@ -353,15 +358,15 @@ public final class RealtimeClientV2: Sendable {
if Task.isCancelled {
break
}
self?.sendHeartbeat()
await self?.sendHeartbeat()
}
}
mutableState.withValue {
$0.heartbeatTask = heartbeatTask
}
}

private func sendHeartbeat() {
private func sendHeartbeat() async {
let pendingHeartbeatRef: String? = mutableState.withValue {
if $0.pendingHeartbeatRef != nil {
$0.pendingHeartbeatRef = nil
Expand All @@ -383,6 +388,7 @@ public final class RealtimeClientV2: Sendable {
payload: [:]
)
)
await setAuth()
} else {
options.logger?.debug("Heartbeat timeout")
reconnect()
Expand Down Expand Up @@ -426,14 +432,6 @@ public final class RealtimeClientV2: Sendable {
token = mutableState.accessToken
}

if let token, let payload = JWT.decodePayload(token),
let exp = payload["exp"] as? TimeInterval, exp < Date().timeIntervalSince1970
{
options.logger?.warning(
"InvalidJWTToken: Invalid value for JWT claim \"exp\" with value \(exp)")
return
}

mutableState.withValue { [token] in
$0.accessToken = token
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Realtime/RealtimeJoinConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import Foundation
struct RealtimeJoinPayload: Codable {
var config: RealtimeJoinConfig
var accessToken: String?
var version: String?

enum CodingKeys: String, CodingKey {
case config
case accessToken = "access_token"
case version
}
}

Expand Down

0 comments on commit c74ce06

Please sign in to comment.