@drawbridge/drawbridge-utils 0.0.40 → 0.0.41
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/{chunk-N5EMSZLI.js → chunk-2DRAIKGZ.js} +1 -1
- package/dist/chunk-OS2AHUWX.js +27 -0
- package/dist/encrypt.js +2 -2
- package/dist/oauth.cjs +94 -0
- package/dist/oauth.d.cts +129 -0
- package/dist/oauth.d.ts +129 -0
- package/dist/oauth.js +70 -0
- package/dist/shopify.js +2 -2
- package/dist/token.cjs +21 -2
- package/dist/token.d.cts +40 -1
- package/dist/token.d.ts +40 -1
- package/dist/token.js +7 -3
- package/package.json +6 -1
- package/dist/chunk-HOA2JHXC.js +0 -10
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// token.js
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
var generate = (bytes = 32, encoding = "base64url") => {
|
|
4
|
+
const buf = crypto.randomBytes(bytes);
|
|
5
|
+
return encoding ? buf.toString(encoding) : buf;
|
|
6
|
+
};
|
|
7
|
+
var hash = (value, key) => {
|
|
8
|
+
if (!value) return null;
|
|
9
|
+
if (key) {
|
|
10
|
+
return crypto.createHmac("sha256", key).update(String(value)).digest("base64url");
|
|
11
|
+
}
|
|
12
|
+
return crypto.createHash("sha256").update(String(value)).digest("base64url");
|
|
13
|
+
};
|
|
14
|
+
var compare = (a, b) => {
|
|
15
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
16
|
+
if (a.length !== b.length) return false;
|
|
17
|
+
return crypto.timingSafeEqual(
|
|
18
|
+
Buffer.from(a),
|
|
19
|
+
Buffer.from(b)
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
generate,
|
|
25
|
+
hash,
|
|
26
|
+
compare
|
|
27
|
+
};
|
package/dist/encrypt.js
CHANGED
package/dist/oauth.cjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// oauth.js
|
|
20
|
+
var oauth_exports = {};
|
|
21
|
+
__export(oauth_exports, {
|
|
22
|
+
createOAuthClient: () => createOAuthClient
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(oauth_exports);
|
|
25
|
+
var REFRESH_LEAD_SECONDS = 60;
|
|
26
|
+
var createOAuthClient = ({
|
|
27
|
+
clientId,
|
|
28
|
+
clientSecret,
|
|
29
|
+
scopes = [],
|
|
30
|
+
tokenUri
|
|
31
|
+
}) => {
|
|
32
|
+
if (!clientId) throw new Error("createOAuthClient: clientId required");
|
|
33
|
+
if (!clientSecret) throw new Error("createOAuthClient: clientSecret required");
|
|
34
|
+
if (!tokenUri) throw new Error("createOAuthClient: tokenUri required");
|
|
35
|
+
let cached = null;
|
|
36
|
+
let pending = null;
|
|
37
|
+
const isFresh = () => {
|
|
38
|
+
if (!cached) return false;
|
|
39
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
40
|
+
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
41
|
+
};
|
|
42
|
+
const fetchToken = async () => {
|
|
43
|
+
const params = new URLSearchParams();
|
|
44
|
+
params.set("grant_type", "client_credentials");
|
|
45
|
+
if (scopes.length > 0) {
|
|
46
|
+
params.set("scope", scopes.join(" "));
|
|
47
|
+
}
|
|
48
|
+
const basic = Buffer.from(clientId + ":" + clientSecret).toString("base64");
|
|
49
|
+
const response = await fetch(
|
|
50
|
+
tokenUri,
|
|
51
|
+
{
|
|
52
|
+
body: params.toString(),
|
|
53
|
+
headers: {
|
|
54
|
+
"authorization": "Basic " + basic,
|
|
55
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
56
|
+
},
|
|
57
|
+
method: "POST"
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
const text = await response.text();
|
|
62
|
+
throw new Error("createOAuthClient: token fetch failed (" + response.status + "): " + text);
|
|
63
|
+
}
|
|
64
|
+
const body = await response.json();
|
|
65
|
+
cached = {
|
|
66
|
+
accessToken: body.access_token,
|
|
67
|
+
expiresAt: Math.floor(Date.now() / 1e3) + Number(body.expires_in || 3600),
|
|
68
|
+
scope: body.scope
|
|
69
|
+
};
|
|
70
|
+
return cached.accessToken;
|
|
71
|
+
};
|
|
72
|
+
const refresh = () => {
|
|
73
|
+
if (pending) return pending;
|
|
74
|
+
pending = fetchToken().finally(() => {
|
|
75
|
+
pending = null;
|
|
76
|
+
});
|
|
77
|
+
return pending;
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
token: async () => {
|
|
81
|
+
if (isFresh()) return cached.accessToken;
|
|
82
|
+
return refresh();
|
|
83
|
+
},
|
|
84
|
+
// Force a refresh on the next call. Use this when a request returns 401
|
|
85
|
+
// before the cached token's expiry — the server may have revoked it.
|
|
86
|
+
invalidate: () => {
|
|
87
|
+
cached = null;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
createOAuthClient
|
|
94
|
+
});
|
package/dist/oauth.d.cts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Client_credentials token caching helper for first-party Drawbridge apps
|
|
2
|
+
// that call drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
|
|
3
|
+
// drawbridge-webhooks, etc.).
|
|
4
|
+
//
|
|
5
|
+
// Caches the access token in process memory; refreshes when nearing expiry
|
|
6
|
+
// or after a 401. Single-flight refresh prevents thundering herd when many
|
|
7
|
+
// concurrent requests arrive while the token is being rotated.
|
|
8
|
+
//
|
|
9
|
+
// Usage:
|
|
10
|
+
// import { createOAuthClient } from '@drawbridge/drawbridge-utils/oauth';
|
|
11
|
+
//
|
|
12
|
+
// const client = createOAuthClient({
|
|
13
|
+
// clientId : process.env.OAUTH_CLIENT_ID,
|
|
14
|
+
// clientSecret : process.env.OAUTH_CLIENT_SECRET,
|
|
15
|
+
// tokenUri : process.env.NEXT_PUBLIC_API_URI + '/oauth/token',
|
|
16
|
+
// scopes : [ 'share:read' ]
|
|
17
|
+
// });
|
|
18
|
+
//
|
|
19
|
+
// await request({ endpoint : '/page/abc', token : await client.token() });
|
|
20
|
+
|
|
21
|
+
const REFRESH_LEAD_SECONDS = 60;
|
|
22
|
+
|
|
23
|
+
const createOAuthClient = ({
|
|
24
|
+
clientId,
|
|
25
|
+
clientSecret,
|
|
26
|
+
scopes = [],
|
|
27
|
+
tokenUri
|
|
28
|
+
}) => {
|
|
29
|
+
|
|
30
|
+
if( ! clientId ) throw new Error( 'createOAuthClient: clientId required' );
|
|
31
|
+
if( ! clientSecret ) throw new Error( 'createOAuthClient: clientSecret required' );
|
|
32
|
+
if( ! tokenUri ) throw new Error( 'createOAuthClient: tokenUri required' );
|
|
33
|
+
|
|
34
|
+
let cached = null;
|
|
35
|
+
let pending = null;
|
|
36
|
+
|
|
37
|
+
const isFresh = () => {
|
|
38
|
+
|
|
39
|
+
if( ! cached ) return false;
|
|
40
|
+
|
|
41
|
+
const now = Math.floor( Date.now() / 1000 );
|
|
42
|
+
|
|
43
|
+
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
44
|
+
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const fetchToken = async () => {
|
|
48
|
+
|
|
49
|
+
const params = new URLSearchParams();
|
|
50
|
+
|
|
51
|
+
params.set( 'grant_type', 'client_credentials' );
|
|
52
|
+
|
|
53
|
+
if( scopes.length > 0 ){
|
|
54
|
+
|
|
55
|
+
params.set( 'scope', scopes.join( ' ' ) );
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const basic = Buffer
|
|
60
|
+
.from( clientId + ':' + clientSecret )
|
|
61
|
+
.toString( 'base64' );
|
|
62
|
+
|
|
63
|
+
const response = await fetch(
|
|
64
|
+
tokenUri,
|
|
65
|
+
{
|
|
66
|
+
body : params.toString(),
|
|
67
|
+
headers : {
|
|
68
|
+
'authorization' : 'Basic ' + basic,
|
|
69
|
+
'content-type' : 'application/x-www-form-urlencoded'
|
|
70
|
+
},
|
|
71
|
+
method : 'POST'
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if( ! response.ok ){
|
|
76
|
+
|
|
77
|
+
const text = await response.text();
|
|
78
|
+
|
|
79
|
+
throw new Error( 'createOAuthClient: token fetch failed (' + response.status + '): ' + text );
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const body = await response.json();
|
|
84
|
+
|
|
85
|
+
cached = {
|
|
86
|
+
accessToken : body.access_token,
|
|
87
|
+
expiresAt : Math.floor( Date.now() / 1000 ) + Number( body.expires_in || 3600 ),
|
|
88
|
+
scope : body.scope
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return cached.accessToken;
|
|
92
|
+
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Single-flight: collapse concurrent token fetches into one network call.
|
|
96
|
+
const refresh = () => {
|
|
97
|
+
|
|
98
|
+
if( pending ) return pending;
|
|
99
|
+
|
|
100
|
+
pending = fetchToken().finally( () => {
|
|
101
|
+
|
|
102
|
+
pending = null;
|
|
103
|
+
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return pending;
|
|
107
|
+
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
token : async () => {
|
|
112
|
+
|
|
113
|
+
if( isFresh() ) return cached.accessToken;
|
|
114
|
+
|
|
115
|
+
return refresh();
|
|
116
|
+
|
|
117
|
+
},
|
|
118
|
+
// Force a refresh on the next call. Use this when a request returns 401
|
|
119
|
+
// before the cached token's expiry — the server may have revoked it.
|
|
120
|
+
invalidate : () => {
|
|
121
|
+
|
|
122
|
+
cached = null;
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export { createOAuthClient };
|
package/dist/oauth.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Client_credentials token caching helper for first-party Drawbridge apps
|
|
2
|
+
// that call drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
|
|
3
|
+
// drawbridge-webhooks, etc.).
|
|
4
|
+
//
|
|
5
|
+
// Caches the access token in process memory; refreshes when nearing expiry
|
|
6
|
+
// or after a 401. Single-flight refresh prevents thundering herd when many
|
|
7
|
+
// concurrent requests arrive while the token is being rotated.
|
|
8
|
+
//
|
|
9
|
+
// Usage:
|
|
10
|
+
// import { createOAuthClient } from '@drawbridge/drawbridge-utils/oauth';
|
|
11
|
+
//
|
|
12
|
+
// const client = createOAuthClient({
|
|
13
|
+
// clientId : process.env.OAUTH_CLIENT_ID,
|
|
14
|
+
// clientSecret : process.env.OAUTH_CLIENT_SECRET,
|
|
15
|
+
// tokenUri : process.env.NEXT_PUBLIC_API_URI + '/oauth/token',
|
|
16
|
+
// scopes : [ 'share:read' ]
|
|
17
|
+
// });
|
|
18
|
+
//
|
|
19
|
+
// await request({ endpoint : '/page/abc', token : await client.token() });
|
|
20
|
+
|
|
21
|
+
const REFRESH_LEAD_SECONDS = 60;
|
|
22
|
+
|
|
23
|
+
const createOAuthClient = ({
|
|
24
|
+
clientId,
|
|
25
|
+
clientSecret,
|
|
26
|
+
scopes = [],
|
|
27
|
+
tokenUri
|
|
28
|
+
}) => {
|
|
29
|
+
|
|
30
|
+
if( ! clientId ) throw new Error( 'createOAuthClient: clientId required' );
|
|
31
|
+
if( ! clientSecret ) throw new Error( 'createOAuthClient: clientSecret required' );
|
|
32
|
+
if( ! tokenUri ) throw new Error( 'createOAuthClient: tokenUri required' );
|
|
33
|
+
|
|
34
|
+
let cached = null;
|
|
35
|
+
let pending = null;
|
|
36
|
+
|
|
37
|
+
const isFresh = () => {
|
|
38
|
+
|
|
39
|
+
if( ! cached ) return false;
|
|
40
|
+
|
|
41
|
+
const now = Math.floor( Date.now() / 1000 );
|
|
42
|
+
|
|
43
|
+
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
44
|
+
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const fetchToken = async () => {
|
|
48
|
+
|
|
49
|
+
const params = new URLSearchParams();
|
|
50
|
+
|
|
51
|
+
params.set( 'grant_type', 'client_credentials' );
|
|
52
|
+
|
|
53
|
+
if( scopes.length > 0 ){
|
|
54
|
+
|
|
55
|
+
params.set( 'scope', scopes.join( ' ' ) );
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const basic = Buffer
|
|
60
|
+
.from( clientId + ':' + clientSecret )
|
|
61
|
+
.toString( 'base64' );
|
|
62
|
+
|
|
63
|
+
const response = await fetch(
|
|
64
|
+
tokenUri,
|
|
65
|
+
{
|
|
66
|
+
body : params.toString(),
|
|
67
|
+
headers : {
|
|
68
|
+
'authorization' : 'Basic ' + basic,
|
|
69
|
+
'content-type' : 'application/x-www-form-urlencoded'
|
|
70
|
+
},
|
|
71
|
+
method : 'POST'
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if( ! response.ok ){
|
|
76
|
+
|
|
77
|
+
const text = await response.text();
|
|
78
|
+
|
|
79
|
+
throw new Error( 'createOAuthClient: token fetch failed (' + response.status + '): ' + text );
|
|
80
|
+
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const body = await response.json();
|
|
84
|
+
|
|
85
|
+
cached = {
|
|
86
|
+
accessToken : body.access_token,
|
|
87
|
+
expiresAt : Math.floor( Date.now() / 1000 ) + Number( body.expires_in || 3600 ),
|
|
88
|
+
scope : body.scope
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return cached.accessToken;
|
|
92
|
+
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Single-flight: collapse concurrent token fetches into one network call.
|
|
96
|
+
const refresh = () => {
|
|
97
|
+
|
|
98
|
+
if( pending ) return pending;
|
|
99
|
+
|
|
100
|
+
pending = fetchToken().finally( () => {
|
|
101
|
+
|
|
102
|
+
pending = null;
|
|
103
|
+
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return pending;
|
|
107
|
+
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
token : async () => {
|
|
112
|
+
|
|
113
|
+
if( isFresh() ) return cached.accessToken;
|
|
114
|
+
|
|
115
|
+
return refresh();
|
|
116
|
+
|
|
117
|
+
},
|
|
118
|
+
// Force a refresh on the next call. Use this when a request returns 401
|
|
119
|
+
// before the cached token's expiry — the server may have revoked it.
|
|
120
|
+
invalidate : () => {
|
|
121
|
+
|
|
122
|
+
cached = null;
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export { createOAuthClient };
|
package/dist/oauth.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// oauth.js
|
|
2
|
+
var REFRESH_LEAD_SECONDS = 60;
|
|
3
|
+
var createOAuthClient = ({
|
|
4
|
+
clientId,
|
|
5
|
+
clientSecret,
|
|
6
|
+
scopes = [],
|
|
7
|
+
tokenUri
|
|
8
|
+
}) => {
|
|
9
|
+
if (!clientId) throw new Error("createOAuthClient: clientId required");
|
|
10
|
+
if (!clientSecret) throw new Error("createOAuthClient: clientSecret required");
|
|
11
|
+
if (!tokenUri) throw new Error("createOAuthClient: tokenUri required");
|
|
12
|
+
let cached = null;
|
|
13
|
+
let pending = null;
|
|
14
|
+
const isFresh = () => {
|
|
15
|
+
if (!cached) return false;
|
|
16
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
17
|
+
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
18
|
+
};
|
|
19
|
+
const fetchToken = async () => {
|
|
20
|
+
const params = new URLSearchParams();
|
|
21
|
+
params.set("grant_type", "client_credentials");
|
|
22
|
+
if (scopes.length > 0) {
|
|
23
|
+
params.set("scope", scopes.join(" "));
|
|
24
|
+
}
|
|
25
|
+
const basic = Buffer.from(clientId + ":" + clientSecret).toString("base64");
|
|
26
|
+
const response = await fetch(
|
|
27
|
+
tokenUri,
|
|
28
|
+
{
|
|
29
|
+
body: params.toString(),
|
|
30
|
+
headers: {
|
|
31
|
+
"authorization": "Basic " + basic,
|
|
32
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
33
|
+
},
|
|
34
|
+
method: "POST"
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
const text = await response.text();
|
|
39
|
+
throw new Error("createOAuthClient: token fetch failed (" + response.status + "): " + text);
|
|
40
|
+
}
|
|
41
|
+
const body = await response.json();
|
|
42
|
+
cached = {
|
|
43
|
+
accessToken: body.access_token,
|
|
44
|
+
expiresAt: Math.floor(Date.now() / 1e3) + Number(body.expires_in || 3600),
|
|
45
|
+
scope: body.scope
|
|
46
|
+
};
|
|
47
|
+
return cached.accessToken;
|
|
48
|
+
};
|
|
49
|
+
const refresh = () => {
|
|
50
|
+
if (pending) return pending;
|
|
51
|
+
pending = fetchToken().finally(() => {
|
|
52
|
+
pending = null;
|
|
53
|
+
});
|
|
54
|
+
return pending;
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
token: async () => {
|
|
58
|
+
if (isFresh()) return cached.accessToken;
|
|
59
|
+
return refresh();
|
|
60
|
+
},
|
|
61
|
+
// Force a refresh on the next call. Use this when a request returns 401
|
|
62
|
+
// before the cached token's expiry — the server may have revoked it.
|
|
63
|
+
invalidate: () => {
|
|
64
|
+
cached = null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export {
|
|
69
|
+
createOAuthClient
|
|
70
|
+
};
|
package/dist/shopify.js
CHANGED
package/dist/token.cjs
CHANGED
|
@@ -29,7 +29,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// token.js
|
|
30
30
|
var token_exports = {};
|
|
31
31
|
__export(token_exports, {
|
|
32
|
-
|
|
32
|
+
compare: () => compare,
|
|
33
|
+
generate: () => generate,
|
|
34
|
+
hash: () => hash
|
|
33
35
|
});
|
|
34
36
|
module.exports = __toCommonJS(token_exports);
|
|
35
37
|
var import_crypto = __toESM(require("crypto"), 1);
|
|
@@ -37,7 +39,24 @@ var generate = (bytes = 32, encoding = "base64url") => {
|
|
|
37
39
|
const buf = import_crypto.default.randomBytes(bytes);
|
|
38
40
|
return encoding ? buf.toString(encoding) : buf;
|
|
39
41
|
};
|
|
42
|
+
var hash = (value, key) => {
|
|
43
|
+
if (!value) return null;
|
|
44
|
+
if (key) {
|
|
45
|
+
return import_crypto.default.createHmac("sha256", key).update(String(value)).digest("base64url");
|
|
46
|
+
}
|
|
47
|
+
return import_crypto.default.createHash("sha256").update(String(value)).digest("base64url");
|
|
48
|
+
};
|
|
49
|
+
var compare = (a, b) => {
|
|
50
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
51
|
+
if (a.length !== b.length) return false;
|
|
52
|
+
return import_crypto.default.timingSafeEqual(
|
|
53
|
+
Buffer.from(a),
|
|
54
|
+
Buffer.from(b)
|
|
55
|
+
);
|
|
56
|
+
};
|
|
40
57
|
// Annotate the CommonJS export names for ESM import in node:
|
|
41
58
|
0 && (module.exports = {
|
|
42
|
-
|
|
59
|
+
compare,
|
|
60
|
+
generate,
|
|
61
|
+
hash
|
|
43
62
|
});
|
package/dist/token.d.cts
CHANGED
|
@@ -19,4 +19,43 @@ const generate = ( bytes = 32, encoding = 'base64url' ) => {
|
|
|
19
19
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// Hash a token value for storage. When `key` is provided, uses HMAC-SHA256
|
|
23
|
+
// so DB compromise alone doesn't allow forgery via reverse lookup. Without
|
|
24
|
+
// a key, falls back to plain SHA256 (matches the legacy session pattern
|
|
25
|
+
// during transition).
|
|
26
|
+
const hash = ( value, key ) => {
|
|
27
|
+
|
|
28
|
+
if( ! value ) return null;
|
|
29
|
+
|
|
30
|
+
if( key ){
|
|
31
|
+
|
|
32
|
+
return crypto
|
|
33
|
+
.createHmac( 'sha256', key )
|
|
34
|
+
.update( String( value ) )
|
|
35
|
+
.digest( 'base64url' );
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return crypto
|
|
40
|
+
.createHash( 'sha256' )
|
|
41
|
+
.update( String( value ) )
|
|
42
|
+
.digest( 'base64url' );
|
|
43
|
+
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Constant-time comparison. Returns false for length mismatches without
|
|
47
|
+
// timing leak. Use whenever comparing user-supplied input against stored
|
|
48
|
+
// hashes / secrets.
|
|
49
|
+
const compare = ( a, b ) => {
|
|
50
|
+
|
|
51
|
+
if( typeof a !== 'string' || typeof b !== 'string' ) return false;
|
|
52
|
+
if( a.length !== b.length ) return false;
|
|
53
|
+
|
|
54
|
+
return crypto.timingSafeEqual(
|
|
55
|
+
Buffer.from( a ),
|
|
56
|
+
Buffer.from( b )
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { compare, generate, hash };
|
package/dist/token.d.ts
CHANGED
|
@@ -19,4 +19,43 @@ const generate = ( bytes = 32, encoding = 'base64url' ) => {
|
|
|
19
19
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// Hash a token value for storage. When `key` is provided, uses HMAC-SHA256
|
|
23
|
+
// so DB compromise alone doesn't allow forgery via reverse lookup. Without
|
|
24
|
+
// a key, falls back to plain SHA256 (matches the legacy session pattern
|
|
25
|
+
// during transition).
|
|
26
|
+
const hash = ( value, key ) => {
|
|
27
|
+
|
|
28
|
+
if( ! value ) return null;
|
|
29
|
+
|
|
30
|
+
if( key ){
|
|
31
|
+
|
|
32
|
+
return crypto
|
|
33
|
+
.createHmac( 'sha256', key )
|
|
34
|
+
.update( String( value ) )
|
|
35
|
+
.digest( 'base64url' );
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return crypto
|
|
40
|
+
.createHash( 'sha256' )
|
|
41
|
+
.update( String( value ) )
|
|
42
|
+
.digest( 'base64url' );
|
|
43
|
+
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Constant-time comparison. Returns false for length mismatches without
|
|
47
|
+
// timing leak. Use whenever comparing user-supplied input against stored
|
|
48
|
+
// hashes / secrets.
|
|
49
|
+
const compare = ( a, b ) => {
|
|
50
|
+
|
|
51
|
+
if( typeof a !== 'string' || typeof b !== 'string' ) return false;
|
|
52
|
+
if( a.length !== b.length ) return false;
|
|
53
|
+
|
|
54
|
+
return crypto.timingSafeEqual(
|
|
55
|
+
Buffer.from( a ),
|
|
56
|
+
Buffer.from( b )
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { compare, generate, hash };
|
package/dist/token.js
CHANGED
package/package.json
CHANGED
|
@@ -48,6 +48,11 @@
|
|
|
48
48
|
"import": "./dist/nanoid.js",
|
|
49
49
|
"require": "./dist/nanoid.cjs"
|
|
50
50
|
},
|
|
51
|
+
"./oauth": {
|
|
52
|
+
"types": "./dist/oauth.d.ts",
|
|
53
|
+
"import": "./dist/oauth.js",
|
|
54
|
+
"require": "./dist/oauth.cjs"
|
|
55
|
+
},
|
|
51
56
|
"./orders": {
|
|
52
57
|
"types": "./dist/orders.d.ts",
|
|
53
58
|
"import": "./dist/orders.js",
|
|
@@ -109,5 +114,5 @@
|
|
|
109
114
|
"build": "tsup && npm publish"
|
|
110
115
|
},
|
|
111
116
|
"types": "dist/index.d.ts",
|
|
112
|
-
"version": "0.0.
|
|
117
|
+
"version": "0.0.41"
|
|
113
118
|
}
|
package/dist/chunk-HOA2JHXC.js
DELETED