Skip to content

Commit

Permalink
feat: add WriteOnly cache role and PublishOnly topics role (#776)
Browse files Browse the repository at this point in the history
* feat: add WriteOnly role to cache and topics

* chore: update TVM token scope doc examples to include WriteOnly role
  • Loading branch information
anitarua authored Aug 29, 2023
1 parent d8c10e3 commit aa4bc8c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {AllDataReadWrite, TopicRole, CacheRole, ExpiresIn, TokenScope, AllTopics
* export const tokenPermissions: TokenScope = {
* permissions: [
* {
* role: CacheRole.ReadWrite | CacheRole.ReadOnly,
* role: CacheRole.ReadWrite | CacheRole.ReadOnly | CacheRole.WriteOnly,
* cache: AllCaches | "your-cache-name"
* },
* {
* role: TopicRole.PublishSubscribe | TopicRole.SubscribeOnly,
* role: TopicRole.PublishSubscribe | TopicRole.SubscribeOnly | TopicRole.PublishOnly,
* cache: AllCaches | "your-cache-name",
* topic: AllTopics | "your-topic-name"
* }
Expand Down
4 changes: 2 additions & 2 deletions examples/web/nextjs-chat/src/app/api/momento/token/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
* export const tokenPermissions: TokenScope = {
* permissions: [
* {
* role: CacheRole.ReadWrite | CacheRole.ReadOnly,
* role: CacheRole.ReadWrite | CacheRole.ReadOnly | CacheRole.WriteOnly,
* cache: AllCaches | "your-cache-name"
* },
* {
* role: TopicRole.PublishSubscribe | TopicRole.SubscribeOnly,
* role: TopicRole.PublishSubscribe | TopicRole.SubscribeOnly | TopicRole.PublishOnly,
* cache: AllCaches | "your-cache-name",
* topic: AllTopics | "your-topic-name"
* }
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/auth/tokens/token-scope.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum CacheRole {
ReadWrite = 'readwrite',
ReadOnly = 'readonly',
WriteOnly = 'writeonly',
}

class All {}
Expand Down Expand Up @@ -39,6 +40,7 @@ export function asCachePermission(p: Permission): CachePermission {
export enum TopicRole {
PublishSubscribe = 'publishsubscribe',
SubscribeOnly = 'subscribeonly',
PublishOnly = 'publishonly',
}

export interface TopicName {
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/auth/tokens/token-scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export function cacheReadOnly(cacheSelector: CacheSelector): TokenScope {
};
}

export function cacheWriteOnly(cacheSelector: CacheSelector): TokenScope {
return {
permissions: [{role: CacheRole.WriteOnly, cache: cacheSelector}],
};
}

export function topicSubscribeOnly(
cacheSelector: CacheSelector,
topicSelector: TopicSelector
Expand Down Expand Up @@ -47,3 +53,18 @@ export function topicPublishSubscribe(
],
};
}

export function topicPublishOnly(
cacheSelector: CacheSelector,
topicSelector: TopicSelector
): TokenScope {
return {
permissions: [
{
role: TopicRole.PublishOnly,
cache: cacheSelector,
topic: topicSelector,
},
],
};
}
44 changes: 44 additions & 0 deletions packages/core/test/unit/auth/token-scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ describe('TokenScope', () => {
permissions: [{role: CacheRole.ReadOnly, cache: {name: 'mycache'}}],
});
});
it('cacheWriteOnly', () => {
let scope: TokenScope = TokenScopes.cacheWriteOnly('mycache');
expect(scope).toEqual({
permissions: [{role: CacheRole.WriteOnly, cache: 'mycache'}],
});
scope = TokenScopes.cacheWriteOnly(AllCaches);
expect(scope).toEqual({
permissions: [{role: CacheRole.WriteOnly, cache: AllCaches}],
});
scope = TokenScopes.cacheWriteOnly({name: 'mycache'});
expect(scope).toEqual({
permissions: [{role: CacheRole.WriteOnly, cache: {name: 'mycache'}}],
});
});
it('topicSubscribeOnly', () => {
let scope: TokenScope = TokenScopes.topicSubscribeOnly(
'mycache',
Expand Down Expand Up @@ -245,6 +259,36 @@ describe('TokenScope', () => {
],
});
});
it('topicPublishOnly', () => {
let scope: TokenScope = TokenScopes.topicPublishOnly(
'mycache',
'mytopic'
);
expect(scope).toEqual({
permissions: [
{role: TopicRole.PublishOnly, cache: 'mycache', topic: 'mytopic'},
],
});
scope = TokenScopes.topicPublishOnly(AllCaches, AllTopics);
expect(scope).toEqual({
permissions: [
{role: TopicRole.PublishOnly, cache: AllCaches, topic: AllTopics},
],
});
scope = TokenScopes.topicPublishOnly(
{name: 'mycache'},
{name: 'mytopic'}
);
expect(scope).toEqual({
permissions: [
{
role: TopicRole.PublishOnly,
cache: {name: 'mycache'},
topic: {name: 'mytopic'},
},
],
});
});
it('topicPublishSubscribe', () => {
let scope: TokenScope = TokenScopes.topicPublishSubscribe(
'mycache',
Expand Down

0 comments on commit aa4bc8c

Please sign in to comment.