queue の保護
@secureTopic decorator は queue topic を保護し、許可されたユーザーだけが data stream messages にアクセスできるようにします。
security function は boolean を返します。true は request を許可し、false は拒否します。
Squid の queue topic を保護するには、@secureTopic decorator を使用し、topic 名と action の種類を渡します。次のコードは、'topic-name' topic の queue への read と write アクセスを許可します。
- 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 は、client が read したい topic messages の integration ID と topic 名を含む TopicReadContext(Python では dict)を渡します。次の例では、認証済みユーザーだけが topic messages を 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 は、integration ID、topic 名、client が write したい topic messages の配列を含む TopicWriteContext<T>(T は message の型です。Python では context は dict)を渡します。次の例は、message に 'bad word' が含まれている場合に write が許可されないよう、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