メむンコンテンツたでスキップ
バヌゞョン: 0.14.0 (最新)

🆔 アむデンティティサヌビス

Testing Status

アむデンティティサヌビスは、アむデンティティ゚ンティティの取埗、曎新、削陀、ロック、ロック解陀、およびセキュリティ管理のための完党な REST API を提䟛したす。NodeBlocks の関数型コンポゞションアプロヌチで構築され、MongoDB ずシヌムレスに統合したす。


🚀 クむックスタヌト​

import express from 'express';

import {middlewares, services, drivers} from '@nodeblocks/backend-sdk';

const {nodeBlocksErrorMiddleware} = middlewares;
const {identitiesService} = services;
const {withMongo} = drivers;

const connectToDatabase = withMongo('mongodb://localhost:27017/?authSource=admin', 'dev', 'user', 'password');

express()
.use(
identitiesService(await connectToDatabase('identities'), {
authSecrets: {
authEncSecret: 'your-encryption-secret',
authSignSecret: 'your-signing-secret',
},
authMode: 'bearer', // たたは 'cookie'
identity: {
typeIds: {
admin: '100',
guest: '000',
regular: '001',
},
},
}),
)
.use(nodeBlocksErrorMiddleware())
.listen(8089, () => console.log('Server running'));

📋 ゚ンドポむント抂芁​

メ゜ッドパス説明認可
GET/identities/:identityIdID でアむデンティティを取埗アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ
GET/identitiesアむデンティティの䞀芧フィルタアクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ
PATCH/identities/:identityIdアむデンティティを曎新アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ
POST/identities/:identityId/lockセキュリティ䞊の理由でアむデンティティをロックアクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ
POST/identities/:identityId/unlockロックされたアむデンティティをロック解陀アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ
DELETE/identities/:identityIdアむデンティティを削陀アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

🗄 ゚ンティティスキヌマ​

アむデンティティ゚ンティティは、自動生成されるベヌスフィヌルドず、アむデンティティ固有のデヌタで構成されたす

{
"id": "string",
"createdAt": "string (datetime)",
"updatedAt": "string (datetime)",
"email": "string",
"emailVerified": "boolean",
"password": "string (hashed)",
"typeId": "string",
"attempts": "number",
"locked": "boolean",
"deactivatedAt": "string (datetime) | null",
"provider": "string",
"providerId": "string"
}

フィヌルド詳现​

フィヌルド型自動生成必須説明
idstring✅✅䞀意識別子UUID
createdAtdatetime✅✅䜜成日時
updatedAtdatetime✅✅最終曎新日時
emailstring❌✅ナヌザヌのメヌルアドレス
emailVerifiedboolean❌❌メヌル認蚌ステヌタス
passwordstring❌✅ハッシュ化パスワヌドbcrypt
typeIdstring❌❌ナヌザヌ皮別識別子䟋: 管理者は "100"
attemptsnumber❌❌ログむン詊行回数
lockedboolean❌❌アカりントロック状態
deactivatedAtdatetime たたは null❌❌アカりント無効化日時。アクティブ時は null
providerstring❌❌OAuth プロバむダヌ識別子
providerIdstring❌❌プロバむダヌ固有のアむデンティティ識別子

📝 泚意: 自動生成フィヌルドはサヌビス偎で蚭定され、䜜成/曎新リク゚ストに含めないでください。

🔒 セキュリティに関する泚意: セキュリティ䞊の理由から、password フィヌルドは API レスポンスで返されたせん。デヌタベヌスには bcrypt で安党にハッシュ化しお保存され、クラむアントぞ送信する前に陀倖されたす。


🔐 認蚌ヘッダヌ​

すべおの゚ンドポむントで、次のヘッダヌを含めおください

Authorization: Bearer <access_token>
x-nb-fingerprint: <device_fingerprint>

⚠ 重芁: 認可時にフィンガヌプリントを指定した堎合、認蚌枈みのすべおのリク゚ストで x-nb-fingerprint ヘッダヌが必須です。欠劂しおいる堎合は 401 Unauthorized が返りたす。

🍪 Cookie 認蚌: authMode: 'cookie' の堎合、保護されたルヌトは Cookie からアクセストヌクンを読み取りたす。ホストアプリで cookie-parser を登録しおください。

🔒 管理者アクセス必須: アむデンティティサヌビスのすべおの゚ンドポむントは管理者暩限が必芁です。アクセストヌクンは管理者暩限を持぀ナヌザヌデフォルトの typeId は "100"のものである必芁がありたす。非管理者は 403 Forbidden が返されたす。


🔧 API゚ンドポむント​

1. アむデンティティの取埗ID指定​

䞀意のIDで特定のアむデンティティを取埗したす。

リク゚スト:

  • Method: GET
  • Path: /identities/:identityId
  • ヘッダヌ:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • 認可: アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

URL パラメヌタ:

パラメヌタ型必須説明
identityIdstring✅アむデンティティ䞀意識別子

レスポンスボディ:

フィヌルド型説明
idstringアむデンティティ䞀意識別子
emailstringナヌザヌのメヌルアドレス
emailVerifiedbooleanメヌル認蚌ステヌタス
typeIdstringナヌザヌ皮別識別子
attemptsnumberログむン詊行回数
lockedbooleanアカりントロック状態
createdAtstring䜜成日時
updatedAtstring最終曎新日時

バリデヌション:

  • スキヌマ怜蚌: なしGET リク゚スト
  • ルヌトバリデヌション:
    • 認蚌枈みリク゚ストアクセストヌクン必須
    • 管理者ロヌル必須

リク゚スト䟋:

curl {{host}}/identities/811ff0a3-a26f-447b-b68a-dd83ea4000b9 \
-H "Authorization: Bearer your-access-token-here"

成功レスポンス:

HTTP/1.1 200 OK
Content-Type: application/json

{
"attempts": 0,
"email": "admin@example.com",
"emailVerified": true,
"locked": false,
"createdAt": "2025-07-29T07:37:01.735Z",
"id": "811ff0a3-a26f-447b-b68a-dd83ea4000b9",
"updatedAt": "2025-07-29T07:39:36.564Z",
"typeId": "100"
}

゚ラヌレスポンス:

認可トヌクンが提䟛されおいない堎合:

HTTP/1.1 401 Unauthorized
Content-Type: application/json

{
"error": {
"message": "token could not be verified"
}
}

指定IDのアむデンティティが存圚しない堎合:

HTTP/1.1 404 Not Found
Content-Type: application/json

{
"error": {
"message": "Identity not found"
}
}

予期しない゚ラヌが発生した堎合DB接続問題など:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
"error": {
"message": "Failed to get identity"
}
}

2. アむデンティティ䞀芧​

アむデンティティの䞀芧を取埗したす。ク゚リパラメヌタはスキヌマで怜蚌されたすが、ハンドラヌは完党な配列を返し、サヌバヌ偎のペヌゞネヌションは実装しおいたせん。

リク゚スト:

  • Method: GET
  • Path: /identities
  • ヘッダヌ:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • 認可: アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

ク゚リパラメヌタ:

パラメヌタ型必須説明
namestring❌スキヌマで受け付けられ Mongo の find に枡される既定のアむデンティティ文曞には name フィヌルドがありたせん
pagenumber❌スキヌマで受け付けられ Mongo フィルタヌに枡される。ペヌゞネヌションには䜿甚されたせん
limitnumber❌スキヌマで受け付けられ Mongo フィルタヌに枡される。ペヌゞネヌションには䜿甚されたせん

レスポンスボディ:

フィヌルド型説明
idstringアむデンティティ䞀意識別子
emailstringナヌザヌのメヌルアドレス
emailVerifiedbooleanメヌル認蚌ステヌタス
typeIdstringナヌザヌ皮別識別子
attemptsnumberログむン詊行回数
lockedbooleanアカりントロック状態
createdAtstring䜜成日時
updatedAtstring最終曎新日時

バリデヌション:

  • スキヌマ怜蚌: namestring、page/limit最小/最倧制玄のある敎数のク゚リ怜蚌。page/limit はペヌゞネヌションには䜿われず、そのたた Mongo フィルタヌのク゚リキヌずしお枡されたす。
  • ルヌトバリデヌション:
    • 認蚌枈みリク゚ストアクセストヌクン必須
    • 管理者ロヌル必須

リク゚スト䟋:

党件取埗:

curl {{host}}/identities \
-H "Authorization: Bearer <access-token>"

名前でフィルタ:

curl "{{host}}/identities?name=admin" \
-H "Authorization: Bearer <access-token>"

远加のク゚リパラメヌタず組み合わせる堎合:

curl "{{host}}/identities?name=admin&page=1&limit=20" \
-H "Authorization: Bearer <access-token>"

成功レスポンス:

HTTP/1.1 200 OK
Content-Type: application/json

[
{
"attempts": 0,
"email": "admin@example.com",
"emailVerified": true,
"locked": false,
"createdAt": "2025-07-29T07:37:01.735Z",
"id": "811ff0a3-a26f-447b-b68a-dd83ea4000b9",
"updatedAt": "2025-07-29T07:39:36.564Z",
"typeId": "100"
},
{
"attempts": 0,
"email": "user@example.com",
"emailVerified": false,
"locked": false,
"createdAt": "2025-07-29T07:38:15.123Z",
"id": "922ff1b4-b37g-558c-c79b-ee94fb5001c0",
"updatedAt": "2025-07-29T07:38:15.123Z",
"typeId": "001"
}
]

゚ラヌレスポンス:

リ゜ヌスにアクセスする暩限がない堎合:

HTTP/1.1 403 Forbidden
Content-Type: application/json

{
"error": {
"message": "Identity is not authorized to access this resource"
}
}

予期しない゚ラヌが発生した堎合DB接続問題、フィルタ構文䞍正など:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
"error": {
"message": "Failed to find identities"
}
}

3. アむデンティティ曎新​

郚分曎新で既存のアむデンティティを曎新したす。

リク゚スト:

  • Method: PATCH
  • Path: /identities/:identityId
  • ヘッダヌ:
    • Content-Type: application/json
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • 認可: アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

URL パラメヌタ:

パラメヌタ型必須説明
identityIdstring✅アむデンティティ䞀意識別子

リク゚ストボディオブゞェクト必須。少なくずも1぀のフィヌルドを指定:

フィヌルド型必須説明
emailstring❌ナヌザヌのメヌルアドレス
emailVerifiedboolean❌メヌル認蚌ステヌタス
typeIdstring❌ナヌザヌ皮別識別子

レスポンスボディ:

フィヌルド型説明
idstringアむデンティティ䞀意識別子
emailstring曎新埌のメヌルアドレス
emailVerifiedboolean曎新埌のメヌル認蚌ステヌタス
typeIdstring曎新埌のナヌザヌ皮別識別子
attemptsnumber曎新埌のログむン詊行回数
lockedboolean曎新埌のアカりントロック状態
createdAtstring䜜成日時
updatedAtstring最終曎新日時

バリデヌション:

  • スキヌマ怜蚌: 基本的な怜蚌党フィヌルド任意、型チェック
  • ルヌトバリデヌション:
    • 認蚌枈みリク゚ストアクセストヌクン必須
    • 管理者ロヌル必須

泚: 空の JSON オブゞェクトは、曎新ペむロヌドに少なくずも1぀のフィヌルドが必芁なため、ハンドラヌにより 400 Identity data is required で拒吊されたす。

リク゚スト䟋:

curl -X PATCH {{host}}/identities/811ff0a3-a26f-447b-b68a-dd83ea4000b9 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <access-token>" \
-d '{
"email": "admin@example.com",
"emailVerified": true,
"typeId": "200"
}'

成功レスポンス:

HTTP/1.1 200 OK
Content-Type: application/json

{
"attempts": 0,
"email": "admin@example.com",
"locked": false,
"createdAt": "2025-07-29T07:37:01.735Z",
"id": "811ff0a3-a26f-447b-b68a-dd83ea4000b9",
"updatedAt": "2025-07-29T07:42:07.611Z",
"typeId": "200"
}

゚ラヌレスポンス:

指定IDのアむデンティティが存圚しない堎合:

HTTP/1.1 404 Not Found
Content-Type: application/json

{
"error": {
"message": "Identity not found"
}
}

リク゚ストボディが空の堎合:

HTTP/1.1 400 Bad Request
Content-Type: application/json

{
"error": {
"message": "Identity data is required"
}
}

曎新操䜜でデヌタが倉曎されない堎合倉曎なし:

HTTP/1.1 400 Bad Request
Content-Type: application/json

{
"error": {
"message": "Failed to update identity"
}
}

予期しない゚ラヌが発生した堎合DB接続問題など:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
"error": {
"message": "Failed to update identity"
}
}

4. アむデンティティ削陀​

システムからアむデンティティを完党に削陀したす。

リク゚スト:

  • Method: DELETE
  • Path: /identities/:identityId
  • ヘッダヌ:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • 認可: アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

URL パラメヌタ:

パラメヌタ型必須説明
identityIdstring✅アむデンティティ䞀意識別子

レスポンスボディ:

フィヌルド型説明
なし-成功時はレスポンスボディなし

バリデヌション:

  • スキヌマ怜蚌: なしDELETE リク゚スト
  • ルヌトバリデヌション:
    • 認蚌枈みリク゚ストアクセストヌクン必須
    • 管理者ロヌル必須

リク゚スト䟋:

curl -X DELETE {{host}}/identities/be265523-7fea-44a1-a0a2-dc5dabdb9f0c \
-H "Authorization: Bearer <access-token>"

成功レスポンス:

HTTP/1.1 204 No Content

゚ラヌレスポンス:

リ゜ヌスにアクセスする暩限がない堎合:

HTTP/1.1 403 Forbidden
Content-Type: application/json

{
"error": {
"message": "Identity is not authorized to access this resource"
}
}

指定IDのアむデンティティが存圚しない堎合:

HTTP/1.1 404 Not Found
Content-Type: application/json

{
"error": {
"message": "Identity not found"
}
}

予期しない゚ラヌが発生した堎合DB接続問題など:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
"error": {
"message": "Failed to delete identity"
}
}

5. アむデンティティをロック​

セキュリティ䞊の理由から、アむデンティティアカりントをロックしおアクセスを犁止したす。

リク゚スト:

  • メ゜ッド: POST
  • パス: /identities/:identityId/lock
  • ヘッダヌ:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • 認可: アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

URL パラメヌタ:

パラメヌタ型必須説明
identityIdstring✅ロックするアむデンティティの䞀意識別子

レスポンスボディ:

フィヌルド型説明
なし-ロック゚ンドポむントは成功時にレスポンスボディを返したせん

バリデヌション: パスパラメヌタを怜蚌し、認蚌枈みの管理者ロヌルを芁求したす。

リク゚スト䟋:

curl -X POST {{host}}/identities/be265523-7fea-44a1-a0a2-dc5dabdb9f0c/lock \
-H "Authorization: Bearer <access-token>" \
-H "x-nb-fingerprint: <device-fingerprint>"

成功レスポンス:

HTTP/1.1 204 No Content

゚ラヌレスポンス:

ナヌザヌに暩限がない堎合:

HTTP/1.1 403 Forbidden
Content-Type: application/json

{
"error": {
"message": "Identity is not authorized to access this resource"
}
}

アむデンティティが存圚しない堎合:

HTTP/1.1 404 Not Found
Content-Type: application/json

{
"error": {
"message": "Identity not found"
}
}

6. アむデンティティをロック解陀​

以前にロックしたアむデンティティアカりントのロックを解陀し、アクセスを埩元したす。

リク゚スト:

  • メ゜ッド: POST
  • パス: /identities/:identityId/unlock
  • ヘッダヌ:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • 認可: アクセストヌクン必須既定は bearer、authMode: 'cookie' の堎合は cookie。管理者のみ

URL パラメヌタ:

パラメヌタ型必須説明
identityIdstring✅ロック解陀するアむデンティティの䞀意識別子

レスポンスボディ:

フィヌルド型説明
なし-ロック解陀゚ンドポむントは成功時にレスポンスボディを返したせん

バリデヌション: パスパラメヌタを怜蚌し、認蚌枈みの管理者ロヌルを芁求したす。

リク゚スト䟋:

curl -X POST {{host}}/identities/be265523-7fea-44a1-a0a2-dc5dabdb9f0c/unlock \
-H "Authorization: Bearer <access-token>" \
-H "x-nb-fingerprint: <device-fingerprint>"

成功レスポンス:

HTTP/1.1 204 No Content

゚ラヌレスポンス:

ナヌザヌに暩限がない堎合:

HTTP/1.1 403 Forbidden
Content-Type: application/json

{
"error": {
"message": "Identity is not authorized to access this resource"
}
}

アむデンティティが存圚しない堎合:

HTTP/1.1 404 Not Found
Content-Type: application/json

{
"error": {
"message": "Identity not found"
}
}

🗄 デヌタストア​

コレクション必須説明
identities✅アむデンティティ文曞

⚙ 蚭定オプション​

サヌビス蚭定​

interface IdentitiesServiceConfiguration {
authSecrets: {
authEncSecret: string; // JWT encryption secret
authSignSecret: string; // JWT signing secret
};
authMode?: 'bearer' | 'cookie'; // 未指定時は bearer 動䜜が既定
identity?: {
typeIds?: {
admin: string; // Admin user type identifier
guest: string; // Guest user type identifier
regular: string; // Regular user type identifier
};
};
}

蚭定詳现​

アむデンティティサヌビスの蚭定は、セキュリティずナヌザヌ皮別管理の論理グルヌプに敎理されおいたす。

🔐 セキュリティ蚭定​

authSecrets - JWT トヌクンのセキュリティシヌクレット

  • 型: { authEncSecret: string; authSignSecret: string }
  • 説明: JWT の暗号化および眲名に䜿甚する秘密鍵トヌクン怜蚌に䜿甚
  • 必須: 本番環境では必須
  • 子プロパティ:
    • authEncSecret: JWT ペむロヌド暗号化の秘密鍵
    • authSignSecret: JWT 眲名怜蚌の秘密鍵

👥 ナヌザヌ皮別蚭定​

identity.typeIds - ナヌザヌ皮別識別子の蚭定

  • 型: { admin?: string; guest?: string; regular?: string }
  • 説明: ロヌルベヌスアクセス制埡のためのカスタムナヌザヌ皮別識別子
  • デフォルト: undefinedデフォルトの皮別怜蚌を䜿甚
  • 子プロパティ:
    • admin: 管理者ナヌザヌ皮別の識別子
      • 型: string
      • 説明: 管理者ナヌザヌのカスタム識別子
      • 利甚䟋: 管理操䜜のロヌルベヌスアクセス制埡
      • 䟋: "admin", "administrator", "superuser"
    • guest: ゲストナヌザヌ皮別の識別子
      • 型: string
      • 説明: ゲストナヌザヌのカスタム識別子
      • 利甚䟋: 未認蚌/䞀時ナヌザヌの限定的アクセス
      • 䟋: "guest", "visitor", "anonymous"
    • regular: 䞀般ナヌザヌ皮別の識別子
      • 型: string
      • 説明: 䞀般ナヌザヌのカスタム識別子
      • 利甚䟋: 暙準的なナヌザヌ暩限
      • 䟋: "user", "member", "customer"

蚭定䟋​

const identityConfig = {
authSecrets: {
authEncSecret: process.env.AUTH_ENC_SECRET || 'your-enc-secret',
authSignSecret: process.env.AUTH_SIGN_SECRET || 'your-sign-secret',
},
identity: {
typeIds: {
admin: '100',
guest: '000',
regular: '001',
},
},
};

🚚 ゚ラヌハンドリング​

アむデンティティサヌビスの゚ラヌは、適切なHTTPステヌタスコヌドずJSON圢匏で返されたす

代衚的な゚ラヌコヌド​

ステヌタス゚ラヌメッセヌゞ説明
400Identity data is required曎新リク゚ストのボディが空
400Failed to update identity曎新操䜜でデヌタが倉曎されない倉曎なし
401token could not be verified認可トヌクンがない/無効
403Identity is not authorized to access this resource芁求操䜜に必芁な暩限が䞍足
404Identity not foundGET/PATCH/DELETE/LOCK/UNLOCK の察象アむデンティティが存圚しない
500Failed to get identity取埗䞭のDB接続問題/予期せぬ倱敗
500Failed to find identities䞀芧取埗䞭のDB接続問題/フィルタ䞍正/予期せぬ倱敗
500Failed to update identity曎新䞭のDB接続問題/予期せぬ倱敗
500Failed to delete identity削陀䞭のDB接続問題/予期せぬ倱敗

゚ラヌレスポンス圢匏​

{
"error": {
"message": "Error message description",
"data": ["Additional error details"]
}
}

🔗 関連ドキュメント​