Storage の保護
@secureStorage decorator は storage bucket を保護し、authorized user のみが file に access できるようにします。
Squid の storage feature では、Squid Client SDK を使用して file を管理できるため、任意の client から file を操作できます。Squid Client SDK を使用して Squid storage を利用する詳細については、Squid storage documentationを参照してください。
security function は boolean を返します。true は request を許可し、false は拒否します。
Squid storage bucket integration を保護するには、Squid backend の SquidService class 内で @secureStorage decorator を使用し、action type と integration ID を渡します。次の code は built-in storage bucket への完全な access を許可します。
- TypeScript
- Python
import { secureStorage, SquidService } from '@squidcloud/backend';
@secureStorage('all', 'built_in_storage')
allowAllAccessToBuiltInStorage(): boolean {
return true;
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('all', 'built_in_storage')
def allow_all_access_to_built_in_storage(self, context: dict) -> bool:
return True
異なる storage integration の Squid storage bucket を保護するには、decorator に integration ID を指定します。
- TypeScript
- Python
import { secureStorage, SquidService } from '@squidcloud/backend';
@secureStorage('all', 'YOUR_STORAGE_INTEGRATION_ID')
allowAllAccessToStorageIntegration(): boolean {
return true;
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('all', 'YOUR_STORAGE_INTEGRATION_ID')
def allow_all_access_to_storage_integration(self, context: dict) -> bool:
return True
使用可能な action type は以下のとおりです。
'read'
'read' action には、metadata の読み取り、download URL の生成、file の download、directory content の list 化が含まれます。
'write'
'write' action には、新規 file の insert、既存 file の update、file の delete が含まれます。
'insert'
'insert' action は新規 file の insert を許可しますが、既存 file content の update や file の delete は許可しません。
'delete'
'delete' action は既存 file の delete を許可します。
'all'
'all' action には、使用可能なすべての bucket action が含まれます。
Write の保護
次の function は、authenticated user が built-in storage bucket 内の file を upload、update、delete することを許可します。
- TypeScript
- Python
import { secureStorage, SquidService } from '@squidcloud/backend';
@secureStorage('write', 'built_in_storage')
allowAuthenticatedWrites(): boolean {
return this.isAuthenticated();
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('write', 'built_in_storage')
def allow_authenticated_writes(self, context: dict) -> bool:
return self.is_authenticated()
Read の保護
directory name、file metadata、download URL の生成を保護するには、'read' action type を使用します。次の function は、file path が user ID である場合に user が download URL を生成することを許可します。
- TypeScript
- Python
import { secureStorage, SquidService, StorageContext } from '@squidcloud/backend';
@secureStorage('read', 'built_in_storage')
allowReadUserFiles(context: StorageContext): boolean {
// Validate if the requested action is to get a download URL
if (context.functionality !== 'getDownloadUrl') {
return false;
}
const userId = this.getUserAuth()?.userId;
if (!userId) return false;
// Check any paths the user is trying to read to verify they're in the user's directory
for (const path of context.pathsInBucket) {
if (!path.startsWith(`user/${userId}`)) {
return false;
}
}
return true;
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('read', 'built_in_storage')
def allow_read_user_files(self, context: dict) -> bool:
# Validate if the requested action is to get a download URL
if context['functionality'] != 'getDownloadUrl':
return False
user_auth = self.get_user_auth()
if not user_auth:
return False
user_id = user_auth['userId']
# Check any paths the user is trying to read to verify they're in the user's directory
for path in context['pathsInBucket']:
if not path.startswith(f'user/{user_id}'):
return False
return True
function の parameter として StorageContext object を含めると、security function は client が実行しようとする action の information に access できます。Python では、同じ information が dict として届きます。以下は StorageContext object の例です。
{
integrationId: 'built_in_storage',
pathsInBucket: [ 'test/path/img.jpg' ],
action: 'read',
functionality: 'getFileMetadata'
}