Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent sending expired tokens #437

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 132 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@supabase/node-fetch": "^2.6.14",
"@types/phoenix": "^1.5.4",
"@types/ws": "^8.5.10",
"ws": "^8.14.2"
"ws": "^8.18.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.2",
Expand All @@ -51,6 +51,7 @@
"esm": "^3.2.25",
"jsdom": "^16.7.0",
"jsdom-global": "3.0.0",
"jsonwebtoken": "^9.0.2",
"mock-socket": "^9.0.3",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
Expand All @@ -62,4 +63,4 @@
"vitest": "^2.0.5",
"web-worker": "1.2.0"
}
}
}
15 changes: 15 additions & 0 deletions src/RealtimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ export default class RealtimeClient {
setAuth(token: string | null): void {
this.accessToken = token
filipecabaco marked this conversation as resolved.
Show resolved Hide resolved

if (token) {
let parsed = null
try {
parsed = JSON.parse(atob(token.split('.')[1]))
} catch (_error) {}
if (parsed && parsed.exp) {
let now = Math.floor(Date.now() / 1000)
let valid = now - parsed.exp < 0
if (!valid) {
this.log('auth', `${token} has expired, not sending it to realtime`)
filipecabaco marked this conversation as resolved.
Show resolved Hide resolved
return
}
}
}

this.channels.forEach((channel) => {
token && channel.updateJoinPayload({ access_token: token })

Expand Down
Loading