Queue の保護
@secureTopic decorator は queue topic を保護し、authorized user のみが data stream message に access できるようにします。
security function は boolean を返します。true は request を許可し、false は拒否します。
Squid queue topic を保護するには、topic name と action type を渡して @secureTopic decorator を使用します。次の code は、'topic-name' topic の queue に read および write access を許可します。
- TypeScript
- Python
import { SquidService, secureTopic } from '@squidcloud/backend';
export class ExampleService extends SquidService {
@secureTopic('topic-name', 'all')
allowTopicAccess(): boolean {
return true;
}
}
from squidcloud_backend import SquidService, secure_topic
class ExampleService(SquidService):
@secure_topic('topic-name', 'all')
def allow_topic_access(self, context: dict) -> bool:
return True
action type には 'read'、'write'、'all' を指定できます。
Topic Message Read の保護
topic message read を保護する場合、security function には TopicReadContext(Python では dict)が渡されます。これには、client が read しようとする topic message の integration ID と topic name が含まれます。次の例では、authenticated user のみが topic message を read できるよう 'topic-name' topic を保護します。
- TypeScript
- Python
import { SquidService, secureTopic, TopicReadContext } from '@squidcloud/backend';
@secureTopic('topic-name', 'read')
allowTopicRead(context: TopicReadContext): boolean {
console.log(context.topicName);
return this.isAuthenticated();
}
from squidcloud_backend import SquidService, secure_topic
class ExampleService(SquidService):
@secure_topic('topic-name', 'read')
def allow_topic_read(self, context: dict) -> bool:
print(context['topicName'])
return self.is_authenticated()
Topic Message Write の保護
topic message write を保護する場合、security function には TopicWriteContext<T>(T は message の type、Python では context は dict)が渡されます。これには、client が write しようとする topic message の integration ID、topic name、array が含まれます。次の例では、いずれかの message に 'bad word' が含まれる場合、topic への write を許可しない方法を示します。
- TypeScript
- Python
import { SquidService, secureTopic, TopicWriteContext } from '@squidcloud/backend';
@secureTopic('topic-name', 'write')
allowTopicWrite(context: TopicWriteContext<string>): boolean {
console.log(context.topicName);
for (const message of context.messages) {
console.log(message);
if (message.includes('bad word')) {
return false;
}
}
return true;
}
from squidcloud_backend import SquidService, secure_topic
class ExampleService(SquidService):
@secure_topic('topic-name', 'write')
def allow_topic_write(self, context: dict) -> bool:
print(context['topicName'])
for message in context['messages']:
print(message)
if 'bad word' in message:
return False
return True
Apache Kafka または Confluent integration を使用する場合は、decorator の 3 番目の parameter として integration ID を指定します。次の例は、integration ID が 'kafka-integration-id' の queue 用 security function を示します。
- TypeScript
- Python
import { SquidService, secureTopic } from '@squidcloud/backend';
export class ExampleService extends SquidService {
@secureTopic('topic-name', 'all', 'kafka-integration-id')
allowTopicAccess(): boolean {
return true;
}
}
from squidcloud_backend import SquidService, secure_topic
class ExampleService(SquidService):
@secure_topic('topic-name', 'all', 'kafka-integration-id')
def allow_topic_access(self, context: dict) -> bool:
return True