プライバシー法規制とAPIの関係
GDPR(EU一般データ保護規則)・日本の個人情報保護法・CCPA(カリフォルニア州消費者プライバシー法)など、世界各地でデータプライバシー法規制が強化されています。APIを通じて個人データを取り扱う場合、これらの法規制への準拠が求められます。
GDPRの主要な原則とAPIへの影響
- 目的の制限:収集時の目的以外にデータを使用しない
- データ最小化:必要最小限のデータのみ収集・返却する
- 正確性:データを最新の状態に保つ
- 保存制限:必要な期間のみデータを保持する
- 完全性・機密性:適切なセキュリティ対策
データ最小化の実装
// 全フィールドを返す代わりに必要なフィールドのみ返す
// 悪い例
app.get('/api/users/:id', async (req, res) => {
const user = await db.users.findUnique({ where: { id: req.params.id } });
return res.json(user); // password_hash・内部ID等も返してしまう
});
// 良い例:フィールドを明示的に選択
app.get('/api/users/:id', async (req, res) => {
const user = await db.users.findUnique({
where: { id: req.params.id },
select: {
id: true,
name: true,
email: true,
createdAt: true
// password_hash・internalFlags等は除外
}
});
return res.json(user);
});
同意管理(Consent Management)API
// 同意の記録
app.post('/api/consent', authenticate, async (req, res) => {
const { purpose, granted } = req.body;
await db.consents.upsert({
where: { userId_purpose: { userId: req.user.id, purpose } },
update: { granted, grantedAt: granted ? new Date() : null, revokedAt: !granted ? new Date() : null },
create: { userId: req.user.id, purpose, granted, grantedAt: granted ? new Date() : null }
});
res.json({ status: 'updated' });
});
// 同意状態の確認
const hasConsent = async (userId, purpose) => {
const consent = await db.consents.findUnique({
where: { userId_purpose: { userId, purpose } }
});
return consent?.granted === true;
};
忘れられる権利(データ削除API)
// アカウント削除リクエストの処理
app.delete('/api/account', authenticate, async (req, res) => {
const userId = req.user.id;
// 削除ジョブをキューに追加(非同期処理)
await deleteQueue.add({
userId,
requestedAt: new Date(),
reason: req.body.reason
});
res.json({
status: 'deletion_scheduled',
completionDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000) // 30日以内
});
});
// 削除ジョブの実装
const processAccountDeletion = async (userId) => {
// 全関連データの物理削除
await db.$transaction([
db.orders.deleteMany({ where: { userId } }),
db.consents.deleteMany({ where: { userId } }),
db.sessions.deleteMany({ where: { userId } }),
db.users.delete({ where: { id: userId } })
]);
// サードパーティサービスへの削除リクエスト
await stripe.customers.del(user.stripeCustomerId);
// 削除完了をログに記録(監査用)
await auditLog.record({ event: 'ACCOUNT_DELETED', userId, completedAt: new Date() });
};
データポータビリティAPI
// ユーザーデータのエクスポート(GDPRアクセス権)
app.get('/api/account/data-export', authenticate, async (req, res) => {
const userId = req.user.id;
const userData = {
profile: await db.users.findUnique({ where: { id: userId }, select: { name: true, email: true, createdAt: true } }),
orders: await db.orders.findMany({ where: { userId } }),
consents: await db.consents.findMany({ where: { userId } }),
exportedAt: new Date().toISOString()
};
res.set({
'Content-Type': 'application/json',
'Content-Disposition': 'attachment; filename="my-data.json"'
});
res.json(userData);
});
まとめ
GDPRと個人情報保護法への準拠はAPIを通じた個人データ処理全体に影響します。データ最小化・同意管理・削除権・データポータビリティをAPIとして整備することが求められます。法律の解釈・自社サービスへの適用については法律の専門家への相談も重要です。