@auth-craft/backend-sdk 0.7.5 → 0.7.6
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.
- package/README.md +125 -113
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,15 +23,15 @@ npm install @auth-craft/backend-sdk
|
|
|
23
23
|
## Quick Start
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
|
-
import {
|
|
26
|
+
import { InternalClient } from '@auth-craft/backend-sdk';
|
|
27
27
|
|
|
28
|
-
const client =
|
|
28
|
+
const client = new InternalClient({
|
|
29
29
|
baseUrl: 'https://auth.example.com',
|
|
30
|
-
basePath: '/system',
|
|
31
30
|
jwt: {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
privateKey: process.env.INTERNAL_JWT_PRIVATE_KEY!, // hex 64-char or base64 JWK
|
|
32
|
+
serviceId: 'my-service',
|
|
33
|
+
audience: process.env.INTERNAL_JWT_AUDIENCE,
|
|
34
|
+
issuer: process.env.INTERNAL_JWT_ISSUER,
|
|
35
35
|
},
|
|
36
36
|
});
|
|
37
37
|
|
|
@@ -40,168 +40,180 @@ const result = await client.bootstrapTenantOwner({
|
|
|
40
40
|
userId: 'user-123',
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
if (result.
|
|
43
|
+
if (result.ok) {
|
|
44
44
|
console.log(result.data);
|
|
45
|
-
// { message: '...', tenantId: 'tenant-abc', userId: 'user-123', role: 'owner' }
|
|
46
45
|
}
|
|
47
46
|
```
|
|
48
47
|
|
|
49
|
-
##
|
|
48
|
+
## Configuration
|
|
50
49
|
|
|
51
|
-
### `
|
|
52
|
-
|
|
53
|
-
Creates an `InternalClient` instance.
|
|
54
|
-
|
|
55
|
-
**Parameters:**
|
|
50
|
+
### `InternalClientConfig`
|
|
56
51
|
|
|
57
52
|
| Field | Type | Default | Description |
|
|
58
53
|
|---|---|---|---|
|
|
59
54
|
| `baseUrl` | `string` | — | Base URL of the auth service |
|
|
60
|
-
| `basePath` | `string` | `''` | Path prefix before
|
|
61
|
-
| `
|
|
55
|
+
| `basePath` | `string` | `''` | Path prefix before `serviceRoutePath` |
|
|
56
|
+
| `serviceRoutePath` | `string` | `'/internal'` | Internal route path segment |
|
|
57
|
+
| `jwt` | `ServiceJwtConfig` | — | Service JWT signing configuration |
|
|
62
58
|
| `fetchImpl` | `typeof fetch` | `globalThis.fetch` | Custom fetch implementation |
|
|
63
59
|
| `timeout` | `number` | `30000` | Request timeout in milliseconds |
|
|
64
60
|
| `headers` | `Record<string, string>` | `{}` | Custom headers for all requests |
|
|
65
61
|
|
|
66
|
-
|
|
62
|
+
### `ServiceJwtConfig`
|
|
67
63
|
|
|
68
|
-
|
|
64
|
+
| Field | Type | Default | Description |
|
|
65
|
+
|---|---|---|---|
|
|
66
|
+
| `privateKey` | `string` | — | Private key — hex 64-char (EdDSA) or base64-encoded JWK |
|
|
67
|
+
| `alg` | `'EdDSA' \| 'ES256' \| 'RS256' \| 'PS256'` | `'EdDSA'` | Signing algorithm |
|
|
68
|
+
| `serviceId` | `string` | — | Service identity — becomes JWT `sub` claim |
|
|
69
|
+
| `audience` | `string` | `'auth-craft:internal'` | JWT `aud` claim — must match server config |
|
|
70
|
+
| `issuer` | `string` | `'auth-craft'` | JWT `iss` claim |
|
|
71
|
+
| `expiresInMinutes` | `number` | `10` | Token TTL in minutes |
|
|
72
|
+
| `roles` | `string[]` | — | Service roles embedded in token |
|
|
69
73
|
|
|
70
|
-
|
|
74
|
+
**Key formats for `privateKey`:**
|
|
71
75
|
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
```bash
|
|
77
|
+
# EdDSA — hex string (64 chars = 32-byte seed)
|
|
78
|
+
INTERNAL_JWT_PRIVATE_KEY=06A70CB7A313A1CEA2371F8178EAF6CF...
|
|
79
|
+
|
|
80
|
+
# All algorithms — base64-encoded JWK
|
|
81
|
+
# Generate (Node.js):
|
|
82
|
+
# crypto.subtle.generateKey({name:'Ed25519'}, true, ['sign','verify'])
|
|
83
|
+
# .then(k => crypto.subtle.exportKey('jwk', k.privateKey))
|
|
84
|
+
# .then(jwk => btoa(JSON.stringify(jwk)))
|
|
85
|
+
```
|
|
75
86
|
|
|
76
|
-
|
|
77
|
-
jwt: { strategy: 'hmac', secret: '...' }
|
|
87
|
+
Tokens are auto-cached and reused until 60 seconds before expiry.
|
|
78
88
|
|
|
79
|
-
|
|
80
|
-
jwt: { strategy: 'rs256', privateKey: '...' } // PEM string or JsonWebKey
|
|
89
|
+
## Methods
|
|
81
90
|
|
|
82
|
-
|
|
83
|
-
jwt: { strategy: 'es256', privateKey: '...' } // PEM string or JsonWebKey
|
|
84
|
-
```
|
|
91
|
+
#### `bootstrapTenantOwner(request)` — `POST /internal/tenant/bootstrap-owner`
|
|
85
92
|
|
|
86
|
-
|
|
93
|
+
Assign owner role to a user for a new tenant. Typically called by tenant-provisioning service after creating a tenant.
|
|
87
94
|
|
|
88
|
-
|
|
89
|
-
|---|---|---|---|
|
|
90
|
-
| `issuer` | `string` | — | JWT `iss` claim |
|
|
91
|
-
| `audience` | `string` | — | JWT `aud` claim |
|
|
92
|
-
| `expiresInSeconds` | `number` | `300` | Token TTL in seconds |
|
|
95
|
+
#### `getTenantProfile(request)` — `POST /internal/tenant/profile`
|
|
93
96
|
|
|
94
|
-
|
|
97
|
+
Get per-tenant user profile with membership and permissions.
|
|
95
98
|
|
|
96
|
-
|
|
99
|
+
#### `updateTenantProfile(request)` — `POST /internal/tenant/profile/update`
|
|
97
100
|
|
|
98
|
-
|
|
101
|
+
Update per-tenant user profile fields.
|
|
99
102
|
|
|
100
|
-
|
|
103
|
+
#### `listTenantMembers(request)` — `POST /internal/tenant/members/list`
|
|
101
104
|
|
|
102
|
-
|
|
105
|
+
List members of a tenant with profiles and permissions.
|
|
103
106
|
|
|
104
|
-
|
|
105
|
-
const result = await client.bootstrapTenantOwner({
|
|
106
|
-
tenantId: 'tenant-abc',
|
|
107
|
-
userId: 'user-123',
|
|
108
|
-
});
|
|
107
|
+
#### `updateTenantMemberStatus(request)` — `POST /internal/tenant/members/update-status`
|
|
109
108
|
|
|
110
|
-
|
|
111
|
-
console.log(result.data);
|
|
112
|
-
// { message: string, tenantId: string, userId: string, role: string }
|
|
113
|
-
}
|
|
114
|
-
```
|
|
109
|
+
Update a tenant member's status.
|
|
115
110
|
|
|
116
|
-
#### `
|
|
111
|
+
#### `updateTenantMemberPermission(request)` — `POST /internal/tenant/members/update-permission`
|
|
117
112
|
|
|
118
|
-
|
|
113
|
+
Update a tenant member's roles and permissions.
|
|
119
114
|
|
|
120
|
-
|
|
115
|
+
#### `removeTenantMember(request)` — `POST /internal/tenant/members/remove`
|
|
121
116
|
|
|
122
|
-
|
|
123
|
-
const result = await client.getTenantProfile({
|
|
124
|
-
tenantId: 'tenant-abc',
|
|
125
|
-
userId: 'user-123',
|
|
126
|
-
});
|
|
117
|
+
Remove a member from a tenant.
|
|
127
118
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
119
|
+
#### `inviteToTenant(request)` — `POST /internal/tenant/invites/create`
|
|
120
|
+
|
|
121
|
+
Create a tenant invite (bypasses INVITE_MEMBERS permission — service is trusted).
|
|
122
|
+
|
|
123
|
+
#### `acceptInvite(request)` — `POST /internal/tenant/invites/accept`
|
|
124
|
+
|
|
125
|
+
Accept a tenant invite on behalf of a user.
|
|
126
|
+
|
|
127
|
+
#### `declineInvite(request)` — `POST /internal/tenant/invites/decline`
|
|
128
|
+
|
|
129
|
+
Decline a tenant invite.
|
|
130
|
+
|
|
131
|
+
#### `revokeInvite(request)` — `POST /internal/tenant/invites/revoke`
|
|
132
|
+
|
|
133
|
+
Revoke a pending invite.
|
|
134
|
+
|
|
135
|
+
#### `listTenantInvites(request)` — `POST /internal/tenant/invites/list`
|
|
136
|
+
|
|
137
|
+
List invites for a tenant.
|
|
138
|
+
|
|
139
|
+
#### `checkPendingInvites(request)` — `POST /internal/tenant/invites/check-pending`
|
|
140
|
+
|
|
141
|
+
Check pending invites by email or phone.
|
|
142
|
+
|
|
143
|
+
#### `listUserTenants(request)` — `POST /internal/user/tenants/list`
|
|
144
|
+
|
|
145
|
+
List all tenants a user belongs to.
|
|
146
|
+
|
|
147
|
+
#### `listAccounts(request)` — `POST /internal/accounts/list`
|
|
148
|
+
|
|
149
|
+
List user accounts (requires `VIEW_ACCOUNTS` permission in system tenant).
|
|
150
|
+
|
|
151
|
+
#### `getAccount(request)` — `POST /internal/accounts/get`
|
|
152
|
+
|
|
153
|
+
Get account details (requires `VIEW_ACCOUNTS` permission in system tenant).
|
|
154
|
+
|
|
155
|
+
#### `updateAccountProfile(request)` — `POST /internal/accounts/update-profile`
|
|
156
|
+
|
|
157
|
+
Update account profile (requires `UPDATE_ACCOUNT_PROFILE` permission).
|
|
158
|
+
|
|
159
|
+
#### `updateAccountStatus(request)` — `POST /internal/accounts/update-status`
|
|
160
|
+
|
|
161
|
+
Update account status (requires `UPDATE_ACCOUNT_STATUS` permission).
|
|
162
|
+
|
|
163
|
+
#### `resetAccountCredentials(request)` — `POST /internal/accounts/reset-credentials`
|
|
164
|
+
|
|
165
|
+
Reset account credentials (requires `RESET_ACCOUNT_CREDENTIALS` permission).
|
|
166
|
+
|
|
167
|
+
#### `removeAccount(request)` — `POST /internal/accounts/remove`
|
|
168
|
+
|
|
169
|
+
Hard-delete a user account (requires `REMOVE_ACCOUNTS` permission).
|
|
146
170
|
|
|
147
171
|
## Error Handling
|
|
148
172
|
|
|
149
173
|
All methods return `Result<T>` from `ts-micro-result`.
|
|
150
174
|
|
|
151
175
|
```typescript
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const result = await client.bootstrapTenantOwner({ tenantId, userId });
|
|
176
|
+
const result = await client.getTenantProfile({ tenantId, userId });
|
|
155
177
|
|
|
156
|
-
if (result.
|
|
178
|
+
if (!result.ok) {
|
|
157
179
|
const error = result.errors[0];
|
|
158
180
|
|
|
159
181
|
switch (error?.code) {
|
|
160
|
-
case 'SDK_NETWORK_ERROR':
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
case '
|
|
164
|
-
|
|
165
|
-
break;
|
|
166
|
-
case 'SDK_INVALID_RESPONSE':
|
|
167
|
-
// Non-JSON response from auth service
|
|
168
|
-
break;
|
|
169
|
-
case 'SDK_JWT_SIGN_FAILED':
|
|
170
|
-
// Failed to sign service JWT
|
|
171
|
-
break;
|
|
172
|
-
default:
|
|
173
|
-
// Auth service returned an application-level error
|
|
174
|
-
break;
|
|
182
|
+
case 'SDK_NETWORK_ERROR': // Network request failed
|
|
183
|
+
case 'SDK_TIMEOUT': // Request timed out
|
|
184
|
+
case 'SDK_INVALID_RESPONSE': // Non-JSON response
|
|
185
|
+
case 'SDK_JWT_SIGN_FAILED': // Failed to sign service JWT
|
|
186
|
+
default: // Auth service application error
|
|
175
187
|
}
|
|
176
188
|
}
|
|
177
189
|
```
|
|
178
190
|
|
|
179
|
-
##
|
|
191
|
+
## Auth Permission Strings
|
|
192
|
+
|
|
193
|
+
Use `AUTH_PERM_STRINGS` constants when setting `authPerms`:
|
|
180
194
|
|
|
181
195
|
```typescript
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
BootstrapTenantOwnerResponse,
|
|
191
|
-
GetTenantProfileRequest,
|
|
192
|
-
TenantProfileResponse,
|
|
193
|
-
} from '@auth-craft/backend-sdk';
|
|
194
|
-
|
|
195
|
-
export { InternalClient, sdkErrors } from '@auth-craft/backend-sdk';
|
|
196
|
+
import { AUTH_PERM_STRINGS } from '@auth-craft/backend-sdk';
|
|
197
|
+
|
|
198
|
+
await client.updateTenantMemberPermission({
|
|
199
|
+
tenantId: 'org-123',
|
|
200
|
+
userId: 'user-abc',
|
|
201
|
+
actorUserId: 'admin-xyz',
|
|
202
|
+
authPerms: [AUTH_PERM_STRINGS.INVITE_MEMBERS, AUTH_PERM_STRINGS.VIEW_MEMBERS],
|
|
203
|
+
});
|
|
196
204
|
```
|
|
197
205
|
|
|
198
206
|
## Changelog
|
|
199
207
|
|
|
208
|
+
### 0.2.0
|
|
209
|
+
|
|
210
|
+
- **Breaking:** `ServiceJwtConfig.privateKeyJwk` renamed to `privateKey` — accepts hex 64-char (EdDSA) or base64 JWK
|
|
211
|
+
- **Breaking:** Removed `strategy` field — use `alg` instead (`'EdDSA' | 'ES256' | 'RS256' | 'PS256'`)
|
|
212
|
+
- **Fix:** EdDSA hex key import now uses PKCS#8 envelope — compatible with Node.js v20+
|
|
213
|
+
|
|
200
214
|
### 0.1.0
|
|
201
215
|
|
|
202
|
-
-
|
|
203
|
-
- **Breaking:** `TenantProfileResponse` now includes membership fields (`status`, `permMask`, `authPermMask`, `isOwner`). `permMask` and `authPermMask` are always present (default `0`)
|
|
204
|
-
- **Fix:** EdDSA private key import — wrap 32-byte seed in PKCS8 envelope (RFC 8410) for Node.js 20+ compatibility
|
|
216
|
+
- Initial release
|
|
205
217
|
|
|
206
218
|
## License
|
|
207
219
|
|