@nathapp/nestjs-iam-prisma 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/AGENTS.md +160 -0
  2. package/CLAUDE.md +158 -0
  3. package/GEMINI.md +158 -0
  4. package/README.md +360 -2
  5. package/codex.md +158 -0
  6. package/dist/credential-id-hash.d.ts +1 -0
  7. package/dist/credential-id-hash.js +8 -0
  8. package/dist/credential-id-hash.js.map +1 -0
  9. package/dist/iam-prisma.module.d.ts +7 -0
  10. package/dist/iam-prisma.module.js +73 -0
  11. package/dist/iam-prisma.module.js.map +1 -0
  12. package/dist/index.d.ts +15 -0
  13. package/dist/index.js +47 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/prisma-api-key.repository.d.ts +28 -0
  16. package/dist/prisma-api-key.repository.js +90 -0
  17. package/dist/prisma-api-key.repository.js.map +1 -0
  18. package/dist/prisma-auth-attempt.store.d.ts +42 -0
  19. package/dist/prisma-auth-attempt.store.js +112 -0
  20. package/dist/prisma-auth-attempt.store.js.map +1 -0
  21. package/dist/prisma-identity.repository.d.ts +26 -0
  22. package/dist/prisma-identity.repository.js +107 -0
  23. package/dist/prisma-identity.repository.js.map +1 -0
  24. package/dist/prisma-login-history.repository.d.ts +24 -0
  25. package/dist/prisma-login-history.repository.js +58 -0
  26. package/dist/prisma-login-history.repository.js.map +1 -0
  27. package/dist/prisma-mfa.repository.d.ts +20 -0
  28. package/dist/prisma-mfa.repository.js +57 -0
  29. package/dist/prisma-mfa.repository.js.map +1 -0
  30. package/dist/prisma-model.types.d.ts +188 -0
  31. package/dist/prisma-model.types.js +36 -0
  32. package/dist/prisma-model.types.js.map +1 -0
  33. package/dist/prisma-session.repository.d.ts +31 -0
  34. package/dist/prisma-session.repository.js +97 -0
  35. package/dist/prisma-session.repository.js.map +1 -0
  36. package/dist/prisma-sso-account.repository.d.ts +24 -0
  37. package/dist/prisma-sso-account.repository.js +71 -0
  38. package/dist/prisma-sso-account.repository.js.map +1 -0
  39. package/dist/prisma-sso-provider.repository.d.ts +28 -0
  40. package/dist/prisma-sso-provider.repository.js +91 -0
  41. package/dist/prisma-sso-provider.repository.js.map +1 -0
  42. package/dist/prisma-totp-replay.store.d.ts +34 -0
  43. package/dist/prisma-totp-replay.store.js +78 -0
  44. package/dist/prisma-totp-replay.store.js.map +1 -0
  45. package/dist/prisma-user.repository.d.ts +27 -0
  46. package/dist/prisma-user.repository.js +101 -0
  47. package/dist/prisma-user.repository.js.map +1 -0
  48. package/dist/prisma-verification-token.repository.d.ts +23 -0
  49. package/dist/prisma-verification-token.repository.js +66 -0
  50. package/dist/prisma-verification-token.repository.js.map +1 -0
  51. package/dist/prisma-webauthn-credential.repository.d.ts +32 -0
  52. package/dist/prisma-webauthn-credential.repository.js +113 -0
  53. package/dist/prisma-webauthn-credential.repository.js.map +1 -0
  54. package/dist/prisma-webauthn-mfa-binding.store.d.ts +26 -0
  55. package/dist/prisma-webauthn-mfa-binding.store.js +66 -0
  56. package/dist/prisma-webauthn-mfa-binding.store.js.map +1 -0
  57. package/package.json +45 -4
  58. package/prisma/iam.prisma +246 -0
  59. package/prisma/sql/api-key-scopes.postgresql.sql +18 -0
  60. package/prisma/sql/long-text-columns.mysql.sql +15 -0
  61. package/prisma/sql/primary-identity.mysql.sql +10 -0
  62. package/prisma/sql/primary-identity.postgresql.sql +11 -0
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@nathapp/nestjs-iam-prisma",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Prisma adapter for @nathapp/nestjs-iam",
5
+ "scripts": {
6
+ "build": "rimraf dist && tsc -p tsconfig.json",
7
+ "clean": "rimraf dist/ .turbo/",
8
+ "lint": "eslint -c .eslintrc.js --ext .ts .",
9
+ "lint:fix": "eslint -c .eslintrc.js --ext .ts . --fix",
10
+ "test": "jest --config ./test/jest-test.json --forceExit --no-colors --silent",
11
+ "test:json": "jest --config ./test/jest-test.json --json --no-colors --silent",
12
+ "test:verbose": "jest --config ./test/jest-test.json",
13
+ "test:cov": "jest --config ./test/jest-test.json --coverage",
14
+ "type-check": "tsc --noEmit -p tsconfig.json",
15
+ "test:db": "jest --config ./test/jest-db.json --runInBand --forceExit"
16
+ },
17
+ "author": "",
18
+ "license": "ISC",
19
+ "engines": {
20
+ "node": "^20.19.0 || >=22.12.0"
21
+ },
22
+ "types": "dist/index.d.ts",
23
+ "main": "dist/index.js",
24
+ "directories": {
25
+ "lib": "lib"
26
+ },
27
+ "dependencies": {
28
+ "@nathapp/nestjs-data": "^3.3.0 || ^4.0.2",
29
+ "@nathapp/nestjs-iam": "1.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "@nathapp/nestjs-oauth": "1.0.0",
33
+ "@nathapp/nestjs-oauth-prisma": "1.0.0",
34
+ "@nathapp/nestjs-prisma": "^4.0.2",
35
+ "@prisma/client": "6.19.3",
36
+ "eslint": "^8.57.0",
37
+ "prisma": "6.19.3",
38
+ "testcontainers": "12.0.3",
39
+ "typescript": "^5"
40
+ },
41
+ "peerDependencies": {
42
+ "@nathapp/nestjs-prisma": "^3.3.0 || ^4.0.2",
43
+ "@nestjs/common": "^11.0.0 || ^12.1.0",
44
+ "@nestjs/core": "^11.0.0 || ^12.1.0",
45
+ "@prisma/client": "^6.0.0"
46
+ }
47
+ }
@@ -0,0 +1,246 @@
1
+ // === @nathapp/nestjs-iam schema fragment ===
2
+ // Append these models to your application's schema.prisma.
3
+ //
4
+ // TZ NOTE: All DateTime fields use @db.Timestamptz(6) (timestamptz in PostgreSQL).
5
+ // When migrating an existing database, use:
6
+ // ALTER TABLE t ALTER COLUMN col TYPE TIMESTAMPTZ(6) USING col AT TIME ZONE 'UTC';
7
+ // The USING clause treats existing values as UTC — safe if your DB was UTC-configured.
8
+ // Verify your DB timezone (SHOW timezone;) before running migrations on production data.
9
+
10
+ model User {
11
+ id String @id @default(uuid())
12
+ tenantId String @map("tenant_id")
13
+ // FLAG: `email` is declared on the User domain interface and queried by
14
+ // findByEmail, but PrismaUserRepository.toDomain/toPersistenceCreate never
15
+ // read or write it (latent adapter gap). Kept nullable + indexed (not unique,
16
+ // not NOT NULL) so user creation through the adapter does not break. Revisit
17
+ // when the adapter is fixed to populate email (separate from Deliverable A).
18
+ email String?
19
+ displayName String? @map("display_name")
20
+ avatarUrl String? @db.Text @map("avatar_url")
21
+ passwordHash String? @map("password_hash")
22
+ passwordVersion Int @map("password_version")
23
+ status String
24
+ failedLoginAttempts Int @default(0) @map("failed_login_attempts")
25
+ lockedUntil DateTime? @db.Timestamptz(6) @map("locked_until")
26
+ lastLoginAt DateTime? @db.Timestamptz(6) @map("last_login_at")
27
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
28
+ updatedAt DateTime @db.Timestamptz(6) @updatedAt @map("updated_at")
29
+
30
+ // Reverse side of UserIdentity.user. Enables PrismaUserRepository.findByEmail
31
+ // to resolve the user via `include: { user: true }` on the identity lookup.
32
+ identities UserIdentity[]
33
+
34
+ @@index([tenantId])
35
+ @@index([email])
36
+ @@map("users")
37
+ }
38
+
39
+ model UserIdentity {
40
+ id String @id @default(uuid())
41
+ userId String @map("user_id")
42
+ tenantId String @map("tenant_id")
43
+ type String
44
+ value String
45
+ isPrimary Boolean @map("is_primary")
46
+ isVerified Boolean @map("is_verified")
47
+ verifiedAt DateTime? @db.Timestamptz(6) @map("verified_at")
48
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
49
+
50
+ // Required by PrismaUserRepository.findByEmail, which joins the user via
51
+ // `include: { user: true }`. The FK column (user_id) already exists; this
52
+ // directive only adds the relation + FK constraint to users(id).
53
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
54
+
55
+ @@unique([tenantId, type, value])
56
+ @@index([userId])
57
+ @@index([tenantId])
58
+ @@map("user_identities")
59
+ }
60
+
61
+ model LoginHistory {
62
+ id String @id @default(uuid())
63
+ userId String? @map("user_id")
64
+ tenantId String @map("tenant_id")
65
+ identifier String
66
+ ipAddress String? @map("ip_address")
67
+ userAgent String? @db.Text @map("user_agent")
68
+ userAgentId String? @map("user_agent_id")
69
+ method String
70
+ status String
71
+ failureReason String? @map("failure_reason")
72
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
73
+
74
+ @@index([userId])
75
+ @@index([tenantId])
76
+ @@map("login_histories")
77
+ }
78
+
79
+ model UserMfa {
80
+ id String @id @default(uuid())
81
+ userId String @map("user_id")
82
+ type String
83
+ secretEncrypted String @map("secret_encrypted")
84
+ backupCodesHash String? @map("backup_codes_hash")
85
+ isEnabled Boolean @map("is_enabled")
86
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
87
+ updatedAt DateTime @db.Timestamptz(6) @updatedAt @map("updated_at")
88
+
89
+ @@unique([userId])
90
+ @@map("user_mfas")
91
+ }
92
+
93
+ model UserSession {
94
+ id String @id @default(uuid())
95
+ userId String @map("user_id")
96
+ tenantId String @map("tenant_id")
97
+ refreshTokenHash String @map("refresh_token_hash")
98
+ previousRefreshTokenHash String? @map("previous_refresh_token_hash")
99
+ ipAddress String? @map("ip_address")
100
+ userAgent String? @db.Text @map("user_agent")
101
+ deviceFingerprint String? @map("device_fingerprint")
102
+ expiresAt DateTime @db.Timestamptz(6) @map("expires_at")
103
+ lastActiveAt DateTime @db.Timestamptz(6) @map("last_active_at")
104
+ revokedAt DateTime? @db.Timestamptz(6) @map("revoked_at")
105
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
106
+
107
+ @@index([userId])
108
+ @@index([tenantId])
109
+ @@index([refreshTokenHash])
110
+ @@index([previousRefreshTokenHash])
111
+ @@map("user_sessions")
112
+ }
113
+
114
+ model UserSsoAccount {
115
+ id String @id @default(uuid())
116
+ userId String @map("user_id")
117
+ providerId String @map("provider_id")
118
+ externalId String @map("external_id")
119
+ email String?
120
+ profile Json?
121
+ accessTokenEnc String? @db.Text @map("access_token_enc")
122
+ refreshTokenEnc String? @db.Text @map("refresh_token_enc")
123
+ tokenExpiresAt DateTime? @db.Timestamptz(6) @map("token_expires_at")
124
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
125
+ updatedAt DateTime @db.Timestamptz(6) @updatedAt @map("updated_at")
126
+
127
+ @@unique([providerId, externalId])
128
+ @@index([userId])
129
+ @@map("user_sso_accounts")
130
+ }
131
+
132
+ model SsoProvider {
133
+ id String @id @default(uuid())
134
+ tenantId String @map("tenant_id")
135
+ providerId String @map("provider_id")
136
+ issuer String @db.Text
137
+ clientId String @map("client_id")
138
+ clientSecretEnc String @db.Text @map("client_secret_enc")
139
+ scopes Json @default("[]")
140
+ redirectUri String? @db.Text @map("redirect_uri")
141
+ enabled Boolean
142
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
143
+ updatedAt DateTime @db.Timestamptz(6) @updatedAt @map("updated_at")
144
+
145
+ @@unique([tenantId, providerId])
146
+ @@index([tenantId])
147
+ @@map("sso_providers")
148
+ }
149
+
150
+ model VerificationToken {
151
+ id String @id @default(uuid())
152
+ identityId String @map("identity_id")
153
+ tenantId String @map("tenant_id")
154
+ tokenHash String @map("token_hash")
155
+ type String
156
+ expiresAt DateTime @db.Timestamptz(6) @map("expires_at")
157
+ consumedAt DateTime? @db.Timestamptz(6) @map("consumed_at")
158
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
159
+
160
+ @@index([identityId])
161
+ @@index([tenantId])
162
+ @@index([tokenHash])
163
+ @@map("verification_tokens")
164
+ }
165
+
166
+ model ApiKey {
167
+ id String @id @default(uuid())
168
+ tenantId String @map("tenant_id")
169
+ userId String @map("user_id")
170
+ name String
171
+ prefix String @unique
172
+ secretHash String @map("secret_hash")
173
+ scopes Json @default("[]")
174
+ lastUsedAt DateTime? @db.Timestamptz(6) @map("last_used_at")
175
+ expiresAt DateTime? @db.Timestamptz(6) @map("expires_at")
176
+ revokedAt DateTime? @db.Timestamptz(6) @map("revoked_at")
177
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
178
+
179
+ @@index([userId])
180
+ @@index([tenantId])
181
+ @@map("api_keys")
182
+ }
183
+
184
+ model WebAuthnCredential {
185
+ id String @id @default(uuid())
186
+ userId String @map("user_id")
187
+ tenantId String @map("tenant_id")
188
+ credentialId String @db.Text @map("credential_id")
189
+ credentialIdHash String @unique @db.VarChar(64) @map("credential_id_hash")
190
+ publicKey String @db.Text @map("public_key")
191
+ counter BigInt
192
+ transports Json @default("[]")
193
+ aaguid String?
194
+ name String?
195
+ lastUsedAt DateTime? @db.Timestamptz(6) @map("last_used_at")
196
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
197
+
198
+ @@index([userId])
199
+ @@index([tenantId])
200
+ @@map("webauthn_credentials")
201
+ }
202
+
203
+ model WebAuthnMfaBinding {
204
+ id String @id
205
+ userId String @map("user_id")
206
+ tenantId String? @map("tenant_id")
207
+ expiresAt DateTime @db.Timestamptz(6) @map("expires_at")
208
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
209
+
210
+ @@index([tenantId])
211
+ @@index([expiresAt])
212
+ @@map("webauthn_mfa_bindings")
213
+ }
214
+
215
+ // Sliding-TTL replay reservation, not a wall-clock time-step bucket: the
216
+ // unique key is (tenantId, userId, codeHash) alone. Bucketing by a wall-clock
217
+ // time-step (e.g. floor(now/30s)) would let a code resubmitted just after a
218
+ // step boundary evade replay detection, since it would land in a different
219
+ // bucket than its first use — this matches InMemoryTotpReplayStore's sliding
220
+ // TTL semantics exactly (see PrismaTotpReplayStore.checkAndReserve).
221
+ model TotpReplay {
222
+ id String @id @default(uuid())
223
+ tenantId String @map("tenant_id")
224
+ userId String @map("user_id")
225
+ codeHash String @map("code_hash")
226
+ expiresAt DateTime @db.Timestamptz(6) @map("expires_at")
227
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
228
+
229
+ @@unique([tenantId, userId, codeHash])
230
+ @@index([expiresAt])
231
+ @@map("totp_replays")
232
+ }
233
+
234
+ model AuthAttempt {
235
+ id String @id @default(uuid())
236
+ tenantId String @map("tenant_id")
237
+ key String
238
+ attemptCount Int @map("attempt_count")
239
+ windowStart DateTime @db.Timestamptz(6) @map("window_start")
240
+ windowSeconds Int @map("window_seconds")
241
+ createdAt DateTime @db.Timestamptz(6) @default(now()) @map("created_at")
242
+ updatedAt DateTime @db.Timestamptz(6) @updatedAt @map("updated_at")
243
+
244
+ @@unique([tenantId, key])
245
+ @@map("auth_attempts")
246
+ }
@@ -0,0 +1,18 @@
1
+ BEGIN;
2
+
3
+ LOCK TABLE "api_keys" IN ACCESS EXCLUSIVE MODE;
4
+
5
+ -- Adding a validated constraint scans existing rows and fails if an operator
6
+ -- must repair a NULL array or NULL element before this conversion can proceed.
7
+ ALTER TABLE "api_keys"
8
+ ADD CONSTRAINT "api_keys_scopes_legacy_valid_check"
9
+ CHECK ("scopes" IS NOT NULL AND array_position("scopes", NULL) IS NULL);
10
+ ALTER TABLE "api_keys" DROP CONSTRAINT "api_keys_scopes_legacy_valid_check";
11
+
12
+ ALTER TABLE "api_keys" ALTER COLUMN "scopes" DROP DEFAULT;
13
+ ALTER TABLE "api_keys"
14
+ ALTER COLUMN "scopes" TYPE jsonb USING to_jsonb("scopes");
15
+ ALTER TABLE "api_keys" ALTER COLUMN "scopes" SET DEFAULT '[]'::jsonb;
16
+ ALTER TABLE "api_keys" ALTER COLUMN "scopes" SET NOT NULL;
17
+
18
+ COMMIT;
@@ -0,0 +1,15 @@
1
+ -- MySQL only. Widens columns whose values can exceed VARCHAR(191) (Prisma's MySQL
2
+ -- mapping for a bare String). PostgreSQL needs nothing: String is already text there.
3
+ -- Apply once to a MySQL database created from an earlier version of this fragment.
4
+ ALTER TABLE `users` MODIFY `avatar_url` TEXT NULL;
5
+
6
+ ALTER TABLE `login_histories` MODIFY `user_agent` TEXT NULL;
7
+
8
+ ALTER TABLE `user_sessions` MODIFY `user_agent` TEXT NULL;
9
+
10
+ ALTER TABLE `user_sso_accounts` MODIFY `access_token_enc` TEXT NULL,
11
+ MODIFY `refresh_token_enc` TEXT NULL;
12
+
13
+ ALTER TABLE `sso_providers` MODIFY `issuer` TEXT NOT NULL,
14
+ MODIFY `client_secret_enc` TEXT NOT NULL,
15
+ MODIFY `redirect_uri` TEXT NULL;
@@ -0,0 +1,10 @@
1
+ -- Preflight before applying this index; it must return no rows:
2
+ -- SELECT tenant_id, user_id, COUNT(*) AS primary_count
3
+ -- FROM user_identities
4
+ -- WHERE is_primary = TRUE
5
+ -- GROUP BY tenant_id, user_id
6
+ -- HAVING COUNT(*) > 1;
7
+ -- If rows exist, stop and clean duplicates using an operator-selected identity.
8
+ -- Do not choose a winner automatically.
9
+ CREATE UNIQUE INDEX user_identities_one_primary
10
+ ON user_identities (tenant_id, (CASE WHEN is_primary THEN user_id ELSE NULL END));
@@ -0,0 +1,11 @@
1
+ -- Preflight before applying this index; it must return no rows:
2
+ -- SELECT tenant_id, user_id, COUNT(*) AS primary_count
3
+ -- FROM user_identities
4
+ -- WHERE is_primary = TRUE
5
+ -- GROUP BY tenant_id, user_id
6
+ -- HAVING COUNT(*) > 1;
7
+ -- If rows exist, stop and clean duplicates using an operator-selected identity.
8
+ -- Do not choose a winner automatically.
9
+ CREATE UNIQUE INDEX user_identities_one_primary
10
+ ON user_identities (tenant_id, user_id)
11
+ WHERE is_primary = true;