-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): check permissions before sending signals (#161)
* feat(lib): check permissions before sending signals * feat(lib): be more aggressive towards error * PubNub SDK v0.6.0 release. --------- Co-authored-by: PubNub Release Bot <[email protected]>
- Loading branch information
1 parent
7816cb9
commit 2c47233
Showing
6 changed files
with
433 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@pubnub/chat", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "PubNub JavaScript Chat SDK", | ||
"author": "PubNub <[email protected]>", | ||
"license": "SEE LICENSE IN LICENSE", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { GrantTokenPermissions } from "pubnub" | ||
import type { Chat } from "./entities/chat" | ||
|
||
export class AccessManager { | ||
chat: Chat | ||
|
||
constructor(chat: Chat) { | ||
this.chat = chat | ||
} | ||
|
||
canI({ | ||
permission, | ||
resourceType, | ||
resourceName, | ||
}: { | ||
permission: keyof GrantTokenPermissions | ||
resourceName: string | ||
resourceType: "channels" | "uuids" | ||
}) { | ||
const authKey = this.chat.config.authKey | ||
// we assume PAM is not enabled | ||
if (!authKey) { | ||
return true | ||
} | ||
|
||
const parsedToken = this.chat.sdk.parseToken(authKey) | ||
const resourcePermission = parsedToken.resources?.[resourceType]?.[resourceName]?.[permission] | ||
if (typeof resourcePermission === "boolean") { | ||
return resourcePermission | ||
} | ||
const resourcePatterns = parsedToken.patterns?.[resourceType] || {} | ||
const resourcePatternsKeys = Object.keys(resourcePatterns) | ||
for (const pattern of resourcePatternsKeys) { | ||
const regexp = new RegExp(pattern) | ||
const matches = regexp.test(resourceName) | ||
if (matches) { | ||
return resourcePatterns[pattern][permission] || false | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.