@productcraft/auth-passport 1.0.0 → 1.0.1
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/dist/index.d.cts +127 -0
- package/package.json +11 -6
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { KeyObject } from 'node:crypto';
|
|
2
|
+
import { ConsumerScope } from '@productcraft/auth';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@productcraft/auth-passport` — passport-jwt adapter for Auth.
|
|
6
|
+
*
|
|
7
|
+
* Install alongside `@productcraft/auth` + `passport-jwt`:
|
|
8
|
+
*
|
|
9
|
+
* ```bash
|
|
10
|
+
* npm install @productcraft/auth @productcraft/auth-passport passport-jwt
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Recommended one-call form:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import passportJwt from "passport-jwt";
|
|
17
|
+
* import passport from "passport";
|
|
18
|
+
* import { Auth } from "@productcraft/auth";
|
|
19
|
+
* import { createAuthJwtStrategy } from "@productcraft/auth-passport";
|
|
20
|
+
*
|
|
21
|
+
* const auth = new Auth({ auth: { type: "apiKey", key: process.env.PCFT_KEY! } });
|
|
22
|
+
* const scope = auth.consumer("my-app-slug");
|
|
23
|
+
*
|
|
24
|
+
* passport.use(createAuthJwtStrategy(scope, (claims, done) => {
|
|
25
|
+
* // claims is the verified Auth claims object (sub, role, permissions, ...)
|
|
26
|
+
* return done(null, claims);
|
|
27
|
+
* }));
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* The helper wires `secretOrKeyProvider` + `issuer` (per-app URL) +
|
|
31
|
+
* `audience` (app slug) + algorithms automatically from the scope.
|
|
32
|
+
*
|
|
33
|
+
* If you need the underlying primitives — to compose with your own
|
|
34
|
+
* options or pass a custom `jwtFromRequest` — use the lower-level
|
|
35
|
+
* `createPassportSecretOrKeyProvider` directly:
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* new passportJwt.Strategy(
|
|
39
|
+
* {
|
|
40
|
+
* jwtFromRequest: passportJwt.ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
41
|
+
* secretOrKeyProvider: createPassportSecretOrKeyProvider(scope),
|
|
42
|
+
* issuer: scope.acceptedIssuers as string[],
|
|
43
|
+
* audience: scope.expectedAudience,
|
|
44
|
+
* algorithms: ["RS256", "ES256"],
|
|
45
|
+
* },
|
|
46
|
+
* (payload, done) => done(null, payload),
|
|
47
|
+
* );
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* passport-jwt's `secretOrKeyProvider` callback signature. We don't
|
|
53
|
+
* depend on passport-jwt's types directly (to keep it a soft dep),
|
|
54
|
+
* so this is the same shape as `SecretOrKeyProvider` in their typings.
|
|
55
|
+
*/
|
|
56
|
+
type PassportSecretOrKeyProvider = (request: unknown, rawJwtToken: string, done: (err: unknown, secretOrKey?: string | Buffer | KeyObject) => void) => void;
|
|
57
|
+
/**
|
|
58
|
+
* Returns a passport-jwt `secretOrKeyProvider` bound to an Auth
|
|
59
|
+
* `ConsumerScope`. Decodes the protected header, looks up the matching
|
|
60
|
+
* key via the scope's JWKS cache, and converts the resulting jose
|
|
61
|
+
* `CryptoKey` into a Node `KeyObject` that `jsonwebtoken` (the library
|
|
62
|
+
* passport-jwt delegates to) accepts.
|
|
63
|
+
*/
|
|
64
|
+
declare function createPassportSecretOrKeyProvider(scope: ConsumerScope): PassportSecretOrKeyProvider;
|
|
65
|
+
/**
|
|
66
|
+
* Shape of the verified-claims callback passport delegates to once a
|
|
67
|
+
* token passes signature + issuer + audience + expiry checks. Mirrors
|
|
68
|
+
* `passport-jwt`'s own `VerifyCallback` / `VerifyCallbackWithRequest`.
|
|
69
|
+
*/
|
|
70
|
+
type AuthVerifyCallback = (payload: Record<string, unknown>, done: (err: unknown, user?: unknown, info?: unknown) => void) => void;
|
|
71
|
+
/**
|
|
72
|
+
* Extra options for `createAuthJwtStrategy`. Defaults are the
|
|
73
|
+
* Auth-recommended values; override here if you need a different
|
|
74
|
+
* token source (cookie, custom header, ...), a stricter algorithm
|
|
75
|
+
* allow-list, or want to skip the legacy-issuer acceptance window.
|
|
76
|
+
*/
|
|
77
|
+
interface CreateAuthJwtStrategyOptions {
|
|
78
|
+
/**
|
|
79
|
+
* Where to read the bearer from. Defaults to
|
|
80
|
+
* `ExtractJwt.fromAuthHeaderAsBearerToken()`. Pass any
|
|
81
|
+
* passport-jwt extractor when you need a different source.
|
|
82
|
+
*/
|
|
83
|
+
jwtFromRequest?: (req: unknown) => string | null;
|
|
84
|
+
/**
|
|
85
|
+
* JWS algorithms accepted by the underlying verifier. Defaults to
|
|
86
|
+
* the algorithms Auth mints today (`['RS256']`); set explicitly
|
|
87
|
+
* if your app is on a different signing alg or you also accept
|
|
88
|
+
* customer-minted tokens with a different alg.
|
|
89
|
+
*/
|
|
90
|
+
algorithms?: string[];
|
|
91
|
+
/**
|
|
92
|
+
* Pin only the per-app issuer URL (skip the legacy `'heimdall'`
|
|
93
|
+
* literal). Defaults to `false` — verifying both keeps in-flight
|
|
94
|
+
* tokens minted before the per-app-issuer migration valid. Set to
|
|
95
|
+
* `true` once you're confident all old tokens have expired.
|
|
96
|
+
*/
|
|
97
|
+
strictIssuer?: boolean;
|
|
98
|
+
/** Whether the request object should be passed to the verify callback. Defaults to `false`. */
|
|
99
|
+
passReqToCallback?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Constructor signature for `passport-jwt`'s `Strategy`. We don't take
|
|
103
|
+
* a direct dep on passport-jwt to keep it a peer/soft dep — at runtime
|
|
104
|
+
* the caller imports passportJwt and the helper does
|
|
105
|
+
* `new passportJwt.Strategy(...)` via the constructor reference.
|
|
106
|
+
*/
|
|
107
|
+
interface PassportJwtStrategyCtor {
|
|
108
|
+
new (options: Record<string, unknown>, verify: AuthVerifyCallback): any;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Build a fully-configured `passport-jwt` strategy from an Auth
|
|
112
|
+
* `ConsumerScope`. Wires:
|
|
113
|
+
*
|
|
114
|
+
* - `jwtFromRequest` → `ExtractJwt.fromAuthHeaderAsBearerToken()` (override via opts)
|
|
115
|
+
* - `secretOrKeyProvider` → JWKS-backed key resolution via `scope.jwks`
|
|
116
|
+
* - `issuer` → both `scope.expectedIssuer` AND the legacy `'heimdall'` literal (or only the per-app URL if `strictIssuer: true`)
|
|
117
|
+
* - `audience` → `scope.expectedAudience` (the app slug)
|
|
118
|
+
* - `algorithms` → `['RS256']` (override via opts)
|
|
119
|
+
*
|
|
120
|
+
* The verify callback you pass receives the parsed claims after
|
|
121
|
+
* passport-jwt has finished its checks. Refresh tokens
|
|
122
|
+
* (`token_use: "refresh"` or `type: "refresh"`) are refused before
|
|
123
|
+
* your callback runs: the request fails authentication.
|
|
124
|
+
*/
|
|
125
|
+
declare function createAuthJwtStrategy(scope: ConsumerScope, verify: AuthVerifyCallback, options?: CreateAuthJwtStrategyOptions): any;
|
|
126
|
+
|
|
127
|
+
export { type AuthVerifyCallback, type CreateAuthJwtStrategyOptions, type PassportJwtStrategyCtor, createAuthJwtStrategy, createPassportSecretOrKeyProvider };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@productcraft/auth-passport",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Passport-JWT adapter for ProductCraft Auth. Plug an Auth ConsumerScope into a `passport-jwt` Strategy so existing Passport setups can authenticate Auth-issued tokens.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -8,9 +8,14 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
},
|
|
16
21
|
"files": [
|
|
@@ -48,11 +53,11 @@
|
|
|
48
53
|
"jose": "^6.2.3"
|
|
49
54
|
},
|
|
50
55
|
"peerDependencies": {
|
|
51
|
-
"@productcraft/auth": "^1.0.
|
|
56
|
+
"@productcraft/auth": "^1.0.1",
|
|
52
57
|
"passport-jwt": "^4.0.0"
|
|
53
58
|
},
|
|
54
59
|
"devDependencies": {
|
|
55
|
-
"@productcraft/auth": "1.0.
|
|
60
|
+
"@productcraft/auth": "1.0.1"
|
|
56
61
|
},
|
|
57
62
|
"scripts": {
|
|
58
63
|
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
|