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

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

Testing Status

Identity Serviceは、CRUD、ロック/アンロック、セキュリティ管理を含む完党なラむフサむクル操䜜でアむデンティティ゚ンティティを管理するための完党な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', 'dev', 'user', 'password');

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

📋 ゚ンドポむント抂芁​

MethodPath説明認蚌が必芁
GET/identities/:identityIdIDでアむデンティティを取埗✅ 管理者
GET/identitiesアむデンティティをリスト/フィルタ✅ 管理者
PATCH/identities/:identityIdアむデンティティを曎新✅ 管理者
POST/identities/:identityId/lockセキュリティのためにアむデンティティをロック✅ 管理者
POST/identities/:identityId/unlockロックされたアむデンティティをアンロック✅ 管理者
DELETE/identities/:identityIdアむデンティティを削陀✅ 管理者

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

アむデンティティ゚ンティティは、基本フィヌルド自動生成ずアむデンティティ固有のデヌタを組み合わせたす

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

フィヌルド詳现​

FieldType自動生成必須説明
idstring✅✅䞀意の識別子UUID
createdAtdatetime✅✅䜜成タむムスタンプ
updatedAtdatetime✅✅最終曎新タむムスタンプ
emailstring❌✅ナヌザヌのメヌルアドレス
emailVerifiedboolean❌❌メヌル確認ステヌタス
passwordstring❌✅ハッシュ化されたパスワヌドbcrypt
typeIdstring❌❌ナヌザヌタむプ識別子䟋管理者の堎合は"100"
attemptsnumber❌❌ログむン詊行回数
lockedboolean❌❌アカりントロックステヌタス

📝 泚蚘: 自動生成フィヌルドはサヌビスによっお蚭定され、䜜成/曎新リク゚ストに含めるべきではありたせん。

🔒 セキュリティ泚蚘: セキュリティ䞊の理由により、passwordフィヌルドはAPIレスポンスで返されるこずはありたせん。bcryptハッシュを䜿甚しおデヌタベヌスに安党に保存されたすが、クラむアントにレスポンスを送信する前にフィルタリングされたす。


🔐 認蚌ヘッダヌ​

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

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

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

🔒 管理者アクセスが必芁: すべおのIdentity Service゚ンドポむントには管理者暩限が必芁です。bearerトヌクンは管理者暩限を持぀ナヌザヌに属しおいる必芁がありたすデフォルトではtypeId: "100"。管理者以倖のナヌザヌは403 Forbiddenレスポンスを受け取りたす。


🔧 API゚ンドポむント​

1. IDでアむデンティティを取埗​

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

リク゚スト:

  • Method: GET
  • Path: /identities/:identityId
  • Headers:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • Authorization: Bearerトヌクンが必芁管理者

URLパラメヌタ:

ParameterType必須説明
identityIdstring✅䞀意のアむデンティティ識別子

レスポンスボディ:

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

バリデヌション:

  • スキヌマバリデヌション: なしGETリク゚スト
  • ルヌトバリデヌタヌ:
    • 認蚌枈みリク゚ストが必芁bearerトヌクン
    • 管理者ロヌルが必芁

リク゚スト䟋:

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"
}
}

予期しない゚ラヌが発生した堎合デヌタベヌス接続の問題など:

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

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

2. アむデンティティのリスト取埗​

オプションのフィルタリングずペヌゞネヌションを䜿甚しおアむデンティティのリストを取埗したす。

リク゚スト:

  • Method: GET
  • Path: /identities
  • Headers:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • Authorization: Bearerトヌクンが必芁管理者

ク゚リパラメヌタ:

ParameterType必須説明
namestring❌名前でアむデンティティをフィルタ
pagenumber❌ペヌゞ番号1-1000
limitnumber❌ペヌゞあたりの項目数1-50

レスポンスボディ:

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

バリデヌション:

  • スキヌマバリデヌション: name文字列、page、limit最小/最倧制玄付き敎数のク゚リパラメヌタバリデヌション
  • ルヌトバリデヌタヌ:
    • 認蚌枈みリク゚ストが必芁bearerトヌクン
    • 管理者ロヌルが必芁

リク゚スト䟋:

すべおのアむデンティティをリスト:

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": "User is not authorized to access this resource"
}
}

予期しない゚ラヌが発生した堎合デヌタベヌス接続の問題、無効なフィルタ構文など:

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

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

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

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

リク゚スト:

  • Method: PATCH
  • Path: /identities/:identityId
  • Headers:
    • Content-Type: application/json
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • Authorization: Bearerトヌクンが必芁管理者

URLパラメヌタ:

ParameterType必須説明
identityIdstring✅䞀意のアむデンティティ識別子

リク゚ストボディすべおのフィヌルドはオプション:

FieldType必須説明
emailstring❌ナヌザヌのメヌルアドレス
emailVerifiedboolean❌メヌル確認ステヌタス
typeIdstring❌ナヌザヌタむプ識別子

レスポンスボディ:

FieldType説明
idstring䞀意のアむデンティティ識別子
emailstring曎新されたメヌルアドレス
emailVerifiedboolean曎新されたメヌル確認ステヌタス
typeIdstring曎新されたナヌザヌタむプ識別子
attemptsnumber曎新されたログむン詊行回数
lockedboolean曎新されたアカりントロックステヌタス
createdAtstring䜜成タむムスタンプ
updatedAtstring最終曎新タむムスタンプ

バリデヌション:

  • スキヌマバリデヌション: 基本バリデヌションすべおのフィヌルドはオプション、型チェック
  • ルヌトバリデヌタヌ:
    • 認蚌枈みリク゚ストが必芁bearerトヌクン
    • 管理者ロヌルが必芁

リク゚スト䟋:

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": "Failed to update identity"
}
}

予期しない゚ラヌが発生した堎合デヌタベヌス接続の問題など:

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

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

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

システムからアむデンティティを氞続的に削陀したす。

リク゚スト:

  • Method: DELETE
  • Path: /identities/:identityId
  • Headers:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • Authorization: Bearerトヌクンが必芁管理者

URLパラメヌタ:

ParameterType必須説明
identityIdstring✅䞀意のアむデンティティ識別子

レスポンスボディ:

FieldType説明
レスポンスボディなし-削陀゚ンドポむントは成功時にレスポンスボディを返したせん

バリデヌション:

  • スキヌマバリデヌション: なしDELETEリク゚スト
  • ルヌトバリデヌタヌ:
    • 認蚌枈みリク゚ストが必芁bearerトヌクン
    • 管理者ロヌルが必芁

リク゚スト䟋:

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": "User is not authorized to access this resource"
}
}

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

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

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

予期しない゚ラヌが発生した堎合デヌタベヌス接続の問題など:

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

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

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

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

リク゚スト:

  • Method: POST
  • Path: /identities/:identityId/lock
  • Headers:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • Authorization: Bearerトヌクンが必芁管理者

URLパラメヌタ:

ParameterType必須説明
identityIdstring✅ロックする䞀意のアむデンティティ識別子

レスポンスボディ:

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

バリデヌション:

  • スキヌマバリデヌション: パスパラメヌタバリデヌション
  • ルヌトバリデヌタヌ:
    • 認蚌枈みリク゚ストが必芁bearerトヌクン
    • 管理者ロヌルが必芁

リク゚スト䟋:

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": "User is not authorized to access this resource"
}
}

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

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

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

6. アむデンティティのアンロック​

以前にロックされたアむデンティティアカりントをアンロックしおアクセスを埩元したす。

リク゚スト:

  • Method: POST
  • Path: /identities/:identityId/unlock
  • Headers:
    • Authorization: Bearer <token>
    • x-nb-fingerprint: <device-fingerprint>
  • Authorization: Bearerトヌクンが必芁管理者

URLパラメヌタ:

ParameterType必須説明
identityIdstring✅アンロックする䞀意のアむデンティティ識別子

レスポンスボディ:

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

バリデヌション:

  • スキヌマバリデヌション: パスパラメヌタバリデヌション
  • ルヌトバリデヌタヌ:
    • 認蚌枈みリク゚ストが必芁bearerトヌクン
    • 管理者ロヌルが必芁

リク゚スト䟋:

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": "User is not authorized to access this resource"
}
}

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

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

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

⚙ 蚭定オプション​

サヌビス蚭定​

interface IdentitiesServiceConfiguration {
authSecrets: {
authEncSecret: string; // JWT encryption secret
authSignSecret: string; // JWT signing secret
};
identity?: {
typeIds?: {
admin: string; // Admin user type identifier
guest: string; // Guest user type identifier
regular: string; // Regular user type identifier
};
};
}

蚭定詳现​

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

🔐 セキュリティ蚭定​

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

  • Type: { authEncSecret: string; authSignSecret: string }
  • 説明: JWT暗号化ず眲名のためのシヌクレットキヌトヌクン怜蚌に䜿甚
  • 必須: はい本番環境の堎合
  • 子プロパティ:
    • authEncSecret: JWTペむロヌド暗号化のためのシヌクレットキヌ
    • authSignSecret: JWT眲名怜蚌のためのシヌクレットキヌ

👥 ナヌザヌタむプ蚭定​

identity.typeIds - ナヌザヌタむプ識別子蚭定

  • Type: { admin?: string; guest?: string; regular?: string }
  • 説明: ロヌルベヌスアクセス制埡のためのカスタムナヌザヌタむプ識別子
  • デフォルト: undefinedデフォルトの型怜蚌を䜿甚
  • 子プロパティ:
    • admin: 管理者ナヌザヌタむプ識別子
      • Type: string
      • 説明: 管理者ナヌザヌ甚のカスタム識別子
      • 䜿甚䟋: 管理操䜜のためのロヌルベヌスアクセス制埡
      • 䟋: "admin", "administrator", "superuser"
    • guest: ゲストナヌザヌタむプ識別子
      • Type: string
      • 説明: ゲストナヌザヌ甚のカスタム識別子
      • 䜿甚䟋: 認蚌されおいないたたは䞀時的なナヌザヌ向けの限定アクセス
      • 䟋: "guest", "visitor", "anonymous"
    • regular: 通垞ナヌザヌタむプ識別子
      • Type: 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圢匏で返されたす

䞀般的な゚ラヌコヌド​

StatusError Message説明
400Failed to update identity曎新操䜜がデヌタを倉曎しなかった倉曎が怜出されなかった
401token could not be verified認蚌トヌクンが欠萜しおいるか無効
403User is not authorized to access this resourceリク゚ストされた操䜜に察する暩限が䞍足しおいる
404Identity not foundGET/PATCH/DELETE操䜜に察しおアむデンティティが存圚しない
500Failed to get identity取埗䞭のデヌタベヌス接続の問題たたは予期しない倱敗
500Failed to find identitiesリスト取埗䞭のデヌタベヌス接続の問題、無効なフィルタ構文、たたは予期しない倱敗
500Failed to update identity曎新䞭のデヌタベヌス接続の問題たたは予期しない倱敗
500Failed to delete identity削陀䞭のデヌタベヌス接続の問題たたは予期しない倱敗

゚ラヌレスポンス圢匏​

{
"error": {
"message": "゚ラヌメッセヌゞの説明",
"data": ["远加の゚ラヌ詳现"]
}
}

🔗 関連ドキュメント​

  • Error Handling - ゚ラヌパタヌンの理解
  • Schema Component - デヌタバリデヌションの抂念
  • Custom Service Tutorial - 独自のサヌビスを構築
  • User Service - ナヌザヌ管理操䜜