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

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 を許可します。

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 には TopicReadContext(Python では dict)が渡されます。これには、client が read しようとする topic message の integration ID と topic name が含まれます。次の例では、authenticated user のみが topic message を 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 には TopicWriteContext<T>(T は message の type、Python では context は dict)が渡されます。これには、client が write しようとする topic message の integration ID、topic name、array が含まれます。次の例では、いずれかの message に 'bad word' が含まれる場合、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;
}
}