@serafort/core 0.1.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.
- package/LICENSE +21 -0
- package/README.md +93 -0
- package/dist/index.cjs +651 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +331 -0
- package/dist/index.d.ts +331 -0
- package/dist/index.js +634 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Serafort
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# @serafort/core — Serafort JavaScript / TypeScript Isomorphic SDK
|
|
2
|
+
|
|
3
|
+
Isomorphic TypeScript core SDK for the Serafort identity and multi-tenant authentication platform. Works seamlessly in Node.js, browsers, Cloudflare Workers, Vercel Edge, Deno, and Bun with **zero external runtime dependencies**.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Machine Identity (M2M)**: Client Credentials Grant (`/oauth/token`) with thread-safe / Promise-safe in-memory caching, proactive 5-minute pre-expiration token refresh, and exponential backoff retry policies.
|
|
8
|
+
- **B2B User Identity & RBAC**: Fast, zero-network-hop local JWT verification using cached JWKS with Web Crypto API (`crypto.subtle`). Decodes user context (`userId`, `tenantId`, `roles`, `permissions`) and provides wildcard RBAC guards (`hasPermission('org:*')`).
|
|
9
|
+
- **Enterprise SSO**: Discovery and redirect URL generators for SAML 2.0 and OIDC tenants.
|
|
10
|
+
- **Dual Module Exports**: Native ESM (`dist/index.js`) and CommonJS (`dist/index.cjs`) with complete TypeScript declarations.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @serafort/core
|
|
16
|
+
# or
|
|
17
|
+
pnpm add @serafort/core
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quickstart
|
|
21
|
+
|
|
22
|
+
### 1. Machine-to-Machine (M2M) Authentication
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { SerafortClient } from '@serafort/core';
|
|
26
|
+
|
|
27
|
+
const client = new SerafortClient({
|
|
28
|
+
endpoint: 'https://auth.acme.com',
|
|
29
|
+
clientId: process.env.SERAFORT_CLIENT_ID,
|
|
30
|
+
clientSecret: process.env.SERAFORT_CLIENT_SECRET,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Retrieves access token from cache, or fetches automatically if nearing expiration
|
|
34
|
+
const accessToken = await client.getAccessToken(['read:users', 'write:users']);
|
|
35
|
+
|
|
36
|
+
// Use in outgoing API requests
|
|
37
|
+
const res = await fetch('https://api.acme.com/v1/data', {
|
|
38
|
+
headers: {
|
|
39
|
+
Authorization: `Bearer ${accessToken}`,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Local JWT Validation & RBAC (Zero Network Hops)
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { SerafortClient } from '@serafort/core';
|
|
48
|
+
|
|
49
|
+
const client = new SerafortClient({
|
|
50
|
+
endpoint: 'https://auth.acme.com',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Validates token signature against cached JWKS and returns UserContext
|
|
54
|
+
const userContext = await client.validateToken(bearerToken);
|
|
55
|
+
|
|
56
|
+
console.log(userContext.userId); // "usr_123"
|
|
57
|
+
console.log(userContext.tenantId); // "ten_enterprise"
|
|
58
|
+
|
|
59
|
+
// Check granular permissions (supports wildcards like "org:*")
|
|
60
|
+
if (client.b2b.hasPermission(userContext, 'org:write')) {
|
|
61
|
+
// Allow action
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 3. Enterprise SSO Login URL
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const loginUrl = client.b2b.getLoginUrl('ten_enterprise', 'https://app.acme.com/auth/callback', {
|
|
69
|
+
state: 'secure_csrf_state',
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Contributing
|
|
74
|
+
|
|
75
|
+
Before committing, changes are checked with `pnpm run type-check`. This is
|
|
76
|
+
wired up two ways — pick whichever fits your setup:
|
|
77
|
+
|
|
78
|
+
- **Husky (npm-idiomatic, default for contributors who run `pnpm install`)**:
|
|
79
|
+
the `prepare` script installs a Husky hook automatically, so once you've run
|
|
80
|
+
`pnpm install` in a git checkout, `git commit` runs the check for you.
|
|
81
|
+
- **`.githooks/` (portable, no Husky/Node required to install)**: run
|
|
82
|
+
`git config core.hooksPath .githooks` once to point git directly at the
|
|
83
|
+
checked-in `.githooks/pre-commit` script, which runs the same check.
|
|
84
|
+
|
|
85
|
+
Both hooks run the same command, so pick one — you don't need both active
|
|
86
|
+
at once.
|
|
87
|
+
|
|
88
|
+
CI (`.github/workflows/ci.yml`) runs `type-check`, `test`, and `build` on
|
|
89
|
+
every push to `main` and on every pull request.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|