メインコンテンツまでスキップ

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 アクセスを許可します。

Backend code
import { SquidService, secureTopic } from '@squidcloud/backend';

export class ExampleService extends SquidService {
@secureTopic('topic-name', 'all')
allowTopicAccess(): boolean {
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 を保護します。

Backend code
import { SquidService, secureTopic, TopicReadContext } from '@squidcloud/backend';

@secureTopic('topic-name', 'read')
allowTopicRead(context: TopicReadContext): boolean {
console.log(context.topicName);

return this.isAuthenticated();
}

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 を保護する方法を示しています。

Backend code
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;
}

Apache Kafka または Confluent integration を使用する場合は、decorator の 3 番目の parameter として integration ID を指定します。次の例は、integration ID が 'kafka-integration-id' の queue の security function を示しています。

Backend code
import { SquidService, secureTopic } from '@squidcloud/backend';

export class ExampleService extends SquidService {
@secureTopic('topic-name', 'all', 'kafka-integration-id')
allowTopicAccess(): boolean {
return true;
}
}