emailengine-app 2.77.0 → 2.79.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/.github/workflows/test.yml +80 -0
- package/.ncurc.js +23 -13
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +60 -0
- package/bin/emailengine.js +1 -1
- package/config/e2e.toml +2 -1
- package/config/test.toml +2 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +1 -1
- package/lib/account/expected-identity.js +88 -0
- package/lib/account.js +145 -61
- package/lib/api-reference/format.js +105 -4
- package/lib/api-reference/index.js +24 -7
- package/lib/api-reference/model.js +148 -21
- package/lib/api-reference/schema-tree.js +282 -28
- package/lib/api-routes/account-routes.js +99 -36
- package/lib/api-routes/behavior-notes.js +3 -0
- package/lib/api-routes/blocklist-routes.js +21 -47
- package/lib/api-routes/chat-routes.js +10 -12
- package/lib/api-routes/delivery-test-routes.js +6 -26
- package/lib/api-routes/export-routes.js +2 -0
- package/lib/api-routes/gateway-routes.js +52 -11
- package/lib/api-routes/license-routes.js +2 -0
- package/lib/api-routes/mailbox-routes.js +29 -26
- package/lib/api-routes/message-routes.js +27 -21
- package/lib/api-routes/oauth2-app-routes.js +11 -29
- package/lib/api-routes/operation-impact.js +80 -0
- package/lib/api-routes/outbox-routes.js +15 -5
- package/lib/api-routes/permission-map.js +397 -0
- package/lib/api-routes/route-helpers.js +223 -9
- package/lib/api-routes/route-metadata.js +28 -0
- package/lib/api-routes/settings-routes.js +30 -25
- package/lib/api-routes/stats-routes.js +2 -2
- package/lib/api-routes/submit-routes.js +53 -31
- package/lib/api-routes/template-routes.js +3 -0
- package/lib/api-routes/token-routes.js +319 -33
- package/lib/api-routes/webhook-route-routes.js +14 -4
- package/lib/append-list.js +3 -14
- package/lib/arf-detect.js +23 -1
- package/lib/auth-token.js +80 -8
- package/lib/autodetect-imap-settings.js +40 -16
- package/lib/base32.js +30 -0
- package/lib/bounce-detect.js +182 -62
- package/lib/consts.js +10 -0
- package/lib/db.js +20 -2
- package/lib/delivery-error.js +30 -1
- package/lib/egress-filter.js +89 -8
- package/lib/email-client/base-client.js +35 -113
- package/lib/email-client/gmail-client.js +22 -13
- package/lib/email-client/imap/listing-diff.js +3 -3
- package/lib/email-client/imap/mailbox.js +28 -93
- package/lib/email-client/imap/subconnection.js +10 -7
- package/lib/email-client/imap/sync-operations.js +46 -46
- package/lib/email-client/imap-client.js +39 -28
- package/lib/email-client/message-builder.js +15 -15
- package/lib/email-client/notification-handler.js +1 -1
- package/lib/email-client/outlook/graph-api.js +10 -4
- package/lib/email-client/outlook-client.js +54 -55
- package/lib/email-client/smtp-pool-manager.js +1 -1
- package/lib/enum-descriptions.js +92 -2
- package/lib/export.js +2 -2
- package/lib/field-usage.js +133 -0
- package/lib/gateway.js +2 -2
- package/lib/get-raw-email.js +1 -1
- package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +3 -3
- package/lib/imapproxy/imap-core/lib/commands/id.js +2 -13
- package/lib/imapproxy/imap-core/lib/commands/starttls.js +1 -1
- package/lib/imapproxy/imap-core/lib/imap-command.js +21 -5
- package/lib/imapproxy/imap-core/lib/imap-connection.js +17 -7
- package/lib/imapproxy/imap-core/lib/imap-server.js +2 -2
- package/lib/imapproxy/imap-server.js +40 -10
- package/lib/license-beacon.js +5 -10
- package/lib/lists.js +49 -1
- package/lib/llm-pre-process.js +6 -6
- package/lib/logger.js +5 -1
- package/lib/metrics-collector.js +2 -2
- package/lib/msgpack.js +62 -0
- package/lib/oauth/decode-jwt-payload.js +39 -0
- package/lib/oauth/gmail.js +9 -12
- package/lib/oauth/mail-ru.js +5 -7
- package/lib/oauth/outlook-identity.js +61 -0
- package/lib/oauth/outlook.js +9 -9
- package/lib/oauth/pubsub/google.js +25 -10
- package/lib/oauth/retry-setup.js +38 -0
- package/lib/oauth/scope-checker.js +1 -1
- package/lib/oauth2-apps.js +224 -295
- package/lib/openapi/build-document.js +15 -7
- package/lib/openapi/joi-schema.js +20 -0
- package/lib/outbox.js +145 -14
- package/lib/pre-process.js +9 -9
- package/lib/queue-stats.js +49 -0
- package/lib/read-env-value.js +37 -0
- package/lib/reconnection-manager.js +2 -2
- package/lib/rewrite-text-nodes.js +4 -3
- package/lib/routes-ui.js +4 -0
- package/lib/schemas.js +223 -155
- package/lib/settings.js +84 -5
- package/lib/sub-script.js +2 -1
- package/lib/swagger-options.js +2 -1
- package/lib/templates.js +5 -5
- package/lib/threads.js +2 -2
- package/lib/token-audit-log.js +208 -0
- package/lib/token-permission-view.js +219 -0
- package/lib/token-permissions.js +172 -0
- package/lib/tokens.js +100 -16
- package/lib/tools.js +259 -85
- package/lib/totp.js +67 -0
- package/lib/ui-routes/account-routes.js +122 -26
- package/lib/ui-routes/admin-config-routes.js +20 -17
- package/lib/ui-routes/admin-entities-routes.js +66 -22
- package/lib/ui-routes/auth-routes.js +36 -48
- package/lib/ui-routes/export-routes.js +10 -4
- package/lib/ui-routes/oauth-config-routes.js +5 -5
- package/lib/ui-routes/reference-routes.js +11 -8
- package/lib/ui-routes/route-helpers.js +79 -2
- package/lib/ui-routes/settings-page.js +1 -1
- package/lib/ui-routes/smtp-test-routes.js +1 -1
- package/lib/ui-routes/suppression-list-routes.js +294 -0
- package/lib/ui-routes/unsubscribe-routes.js +19 -29
- package/lib/utils/is-email.js +34 -0
- package/lib/utils/network.js +2 -2
- package/lib/utils/to-camel-case.js +17 -0
- package/lib/web-safe-html.js +1 -1
- package/lib/webhook-egress.js +37 -0
- package/lib/webhook-routing.js +80 -0
- package/lib/webhooks.js +21 -15
- package/package.json +26 -27
- package/sbom.json +1 -0
- package/server.js +81 -42
- package/static/css/flyonui.css +1 -1
- package/static/css/public.css +3 -0
- package/static/css/src/app.tailwind.css +159 -9
- package/static/js/app.js +69 -0
- package/static/js/reference.js +741 -10
- package/static/js/ui.js +43 -0
- package/static/licenses.html +698 -1590
- package/translations/README.md +64 -0
- package/translations/de.mo +0 -0
- package/translations/de.po +164 -120
- package/translations/en.po +200 -161
- package/translations/et.mo +0 -0
- package/translations/et.po +163 -120
- package/translations/fr.mo +0 -0
- package/translations/fr.po +163 -120
- package/translations/ja.mo +0 -0
- package/translations/ja.po +163 -120
- package/translations/messages.pot +94 -64
- package/translations/nl.mo +0 -0
- package/translations/nl.po +164 -120
- package/translations/pl.mo +0 -0
- package/translations/pl.po +163 -120
- package/views/account-identity-error.hbs +34 -0
- package/views/accounts/account.hbs +69 -7
- package/views/accounts/browse.hbs +18 -35
- package/views/accounts/edit.hbs +1 -0
- package/views/accounts/register/imap.hbs +12 -2
- package/views/config/logging.hbs +6 -0
- package/views/config/oauth/app.hbs +0 -2
- package/views/config/security.hbs +13 -0
- package/views/layout/app.hbs +29 -5
- package/views/oauth-scope-error.hbs +1 -1
- package/views/partials/oauth_form.hbs +0 -15
- package/views/partials/reference/disclosure.hbs +8 -2
- package/views/partials/reference/nav.hbs +36 -9
- package/views/partials/reference/operation.hbs +48 -14
- package/views/partials/reference/params.hbs +17 -1
- package/views/partials/reference/schema-node.hbs +112 -54
- package/views/partials/reference/schema-tools.hbs +25 -0
- package/views/partials/reference/token-alert.hbs +35 -10
- package/views/partials/reference/try-it.hbs +84 -11
- package/views/partials/side_menu.hbs +1 -0
- package/views/partials/ui/checkbox.hbs +2 -2
- package/views/partials/ui/tabs.hbs +7 -1
- package/views/reference/index.hbs +1 -1
- package/views/reference/token.hbs +4 -0
- package/views/suppression-lists/index.hbs +85 -0
- package/views/suppression-lists/list.hbs +87 -0
- package/views/templates/index.hbs +8 -2
- package/views/tokens/index.hbs +31 -4
- package/views/tokens/new.hbs +324 -2
- package/workers/api.js +299 -140
- package/workers/documents.js +46 -26
- package/workers/export.js +20 -8
- package/workers/imap-proxy.js +2 -2
- package/workers/imap.js +13 -7
- package/workers/smtp.js +13 -7
- package/workers/submit.js +41 -31
- package/workers/webhooks.js +60 -37
- package/lib/feature-flags.js +0 -48
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Narrowing check for an access token that carries a `permissions` record.
|
|
4
|
+
//
|
|
5
|
+
// Two axes, both subtractive: `actions` says what the token may do, `groups` says what it may
|
|
6
|
+
// touch, and the vocabulary for both lives in lib/api-routes/permission-map.js. This function only
|
|
7
|
+
// ever SUBTRACTS from what `scopes` and the account binding already allow - there is no value of
|
|
8
|
+
// `permissions` that grants anything, so it is safe to run after those checks rather than instead
|
|
9
|
+
// of them.
|
|
10
|
+
//
|
|
11
|
+
// Shared by both enforcement points on the same precedent that put lib/auth-token.js in one place:
|
|
12
|
+
// the api-token strategy in workers/api.js for HTTP, and lib/auth-token.js for the SMTP submission
|
|
13
|
+
// and IMAP proxy surfaces. A policy change that landed on one and missed the other would be a hole
|
|
14
|
+
// in whichever surface was forgotten.
|
|
15
|
+
|
|
16
|
+
const { NEVER_GRANTABLE, ACTION_VALUES, GROUP_VALUES } = require('./api-routes/permission-map');
|
|
17
|
+
|
|
18
|
+
// The axes a record may carry, and the vocabulary each accepts. Anything else is a record this
|
|
19
|
+
// version does not understand.
|
|
20
|
+
const AXIS_VALUES = {
|
|
21
|
+
actions: ACTION_VALUES,
|
|
22
|
+
groups: GROUP_VALUES
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const AXES = Object.keys(AXIS_VALUES);
|
|
26
|
+
|
|
27
|
+
// Why a request was refused. Reported to the caller rather than rendered here, because HTTP, SMTP
|
|
28
|
+
// and IMAP each say "no" differently.
|
|
29
|
+
const REASON = {
|
|
30
|
+
// The route resolved to no action or no group, so this vocabulary does not describe it. Means
|
|
31
|
+
// the route table and permission-map.js have diverged, which test/api-routes-table-test.js
|
|
32
|
+
// exists to prevent.
|
|
33
|
+
UNCLASSIFIED: 'unclassified',
|
|
34
|
+
|
|
35
|
+
// `permissions` is present but not a shape this version can read. Deliberately NOT treated as
|
|
36
|
+
// absent: see the comment on parsePermissions().
|
|
37
|
+
MALFORMED: 'malformed',
|
|
38
|
+
|
|
39
|
+
// A group no `permissions` record may ever name, whatever it says.
|
|
40
|
+
RESTRICTED: 'restricted',
|
|
41
|
+
|
|
42
|
+
ACTION: 'action',
|
|
43
|
+
GROUP: 'group'
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Reads a `permissions` record, or reports that it cannot be read.
|
|
48
|
+
*
|
|
49
|
+
* The distinction between "absent" and "unreadable" is the whole safety of this module, because the
|
|
50
|
+
* two mean opposite things: absent is every token issued before permissions existed and must keep
|
|
51
|
+
* working unnarrowed, while unreadable must grant nothing. Collapsing them would turn a malformed
|
|
52
|
+
* record into a full-privilege token.
|
|
53
|
+
*
|
|
54
|
+
* That is not hypothetical. tokens.setRawData() writes whatever fields its input carries with no
|
|
55
|
+
* joi schema at all, and it is the import path for EENGINE_PREPARED_TOKEN, so an arbitrary
|
|
56
|
+
* `permissions` value can reach a record without ever passing route validation.
|
|
57
|
+
*
|
|
58
|
+
* @param {*} permissions - the record's `permissions` field
|
|
59
|
+
* @returns {{narrowed: Boolean, axes: (Object|null), malformed: Boolean}}
|
|
60
|
+
*/
|
|
61
|
+
function parsePermissions(permissions) {
|
|
62
|
+
if (permissions === null || typeof permissions === 'undefined') {
|
|
63
|
+
return { narrowed: false, axes: null, malformed: false };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (typeof permissions !== 'object' || Array.isArray(permissions)) {
|
|
67
|
+
return { narrowed: true, axes: null, malformed: true };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const keys = Object.keys(permissions);
|
|
71
|
+
|
|
72
|
+
// An unknown key is refused rather than ignored. That is what lets a later version add a third
|
|
73
|
+
// axis without this one silently disregarding it - an older release denies the request instead
|
|
74
|
+
// of honouring a narrowing it cannot apply.
|
|
75
|
+
if (keys.some(key => !AXES.includes(key))) {
|
|
76
|
+
return { narrowed: true, axes: null, malformed: true };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// `{}` is ambiguous between "no narrowing" and "grant nothing". The joi schema refuses it so it
|
|
80
|
+
// never reaches a record through the API, and anything that gets in another way is denied.
|
|
81
|
+
if (!keys.length) {
|
|
82
|
+
return { narrowed: true, axes: null, malformed: true };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const axes = {};
|
|
86
|
+
for (const axis of AXES) {
|
|
87
|
+
// Object.hasOwn rather than `in`: this record was msgpack-decoded from Redis by a path with
|
|
88
|
+
// no schema, and `in` walks the prototype chain, so a payload that got something onto the
|
|
89
|
+
// decoded object's prototype could otherwise supply an axis the record does not own.
|
|
90
|
+
if (!Object.hasOwn(permissions, axis)) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const value = permissions[axis];
|
|
95
|
+
if (!Array.isArray(value) || value.some(entry => !AXIS_VALUES[axis].has(entry))) {
|
|
96
|
+
return { narrowed: true, axes: null, malformed: true };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// An empty array is well-formed and grants nothing: an allowlist that lists nothing allows
|
|
100
|
+
// nothing. That is the opposite of the axis being absent, and it matches how `scopes: []`
|
|
101
|
+
// already behaves.
|
|
102
|
+
axes[axis] = value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { narrowed: true, axes, malformed: false };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Is this operation allowed to the token?
|
|
110
|
+
*
|
|
111
|
+
* @param {Object} opts
|
|
112
|
+
* @param {Object} opts.tokenData - the record from tokens.get()
|
|
113
|
+
* @param {Object} opts.operation - { action, group }, resolved by the caller from the route
|
|
114
|
+
* @returns {{allowed: Boolean, reason: (String|null), required: (Object|null)}}
|
|
115
|
+
*/
|
|
116
|
+
function check({ tokenData, operation }) {
|
|
117
|
+
const { action = null, group = null } = operation || {};
|
|
118
|
+
const parsed = parsePermissions(tokenData && tokenData.permissions);
|
|
119
|
+
|
|
120
|
+
if (!parsed.narrowed) {
|
|
121
|
+
// No narrowing on this token, so nothing to subtract. Every token issued before this
|
|
122
|
+
// feature shipped takes this path, which is what makes the change additive.
|
|
123
|
+
return { allowed: true, reason: null, required: null };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const required = { action, group };
|
|
127
|
+
|
|
128
|
+
if (parsed.malformed) {
|
|
129
|
+
return { allowed: false, reason: REASON.MALFORMED, required };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!action || !group) {
|
|
133
|
+
return { allowed: false, reason: REASON.UNCLASSIFIED, required };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Checked before the axes, and independently of them, so no value of `groups` can reach these.
|
|
137
|
+
// This is the rule the rest of the model rests on: while it holds, a narrowed token cannot read
|
|
138
|
+
// the settings blob, cannot read a stored OAuth2 credential, and cannot mint or revoke a token,
|
|
139
|
+
// so it cannot widen itself. Encoded as a deny set rather than as an absence from the grantable
|
|
140
|
+
// list so that widening the grantable list can never quietly reopen it.
|
|
141
|
+
if (NEVER_GRANTABLE.has(group)) {
|
|
142
|
+
return { allowed: false, reason: REASON.RESTRICTED, required };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (parsed.axes.actions && !parsed.axes.actions.includes(action)) {
|
|
146
|
+
return { allowed: false, reason: REASON.ACTION, required };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (parsed.axes.groups && !parsed.axes.groups.includes(group)) {
|
|
150
|
+
return { allowed: false, reason: REASON.GROUP, required };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return { allowed: true, reason: null, required };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* What this module makes of a token's `permissions` field, without an operation to check against.
|
|
158
|
+
*
|
|
159
|
+
* Exists so the admin UI can label a record the same way the enforcement reads it. Deriving the
|
|
160
|
+
* shape a second time in the view diverged immediately: `{}`, an unknown axis and an out-of-vocabulary
|
|
161
|
+
* slug are all refusals here, but a view that only looked for a non-object rendered them as working
|
|
162
|
+
* narrow credentials.
|
|
163
|
+
*
|
|
164
|
+
* @param {Object} tokenData - the record from tokens.get()
|
|
165
|
+
* @returns {{narrowed: Boolean, malformed: Boolean}}
|
|
166
|
+
*/
|
|
167
|
+
function inspect(tokenData) {
|
|
168
|
+
const { narrowed, malformed } = parsePermissions(tokenData && tokenData.permissions);
|
|
169
|
+
return { narrowed, malformed };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
module.exports = { check, inspect, REASON };
|
package/lib/tokens.js
CHANGED
|
@@ -2,13 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
const { redis } = require('./db');
|
|
4
4
|
const crypto = require('crypto');
|
|
5
|
-
const msgpack = require('
|
|
5
|
+
const msgpack = require('./msgpack');
|
|
6
6
|
const logger = require('./logger');
|
|
7
7
|
const { REDIS_PREFIX } = require('./consts');
|
|
8
8
|
const { constantTimeEqual, filterListPage } = require('./tools');
|
|
9
|
+
const tokenAuditLog = require('./token-audit-log');
|
|
9
10
|
|
|
10
11
|
const SESS_PREFIX = 'sess_';
|
|
11
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The id a token is listed and stored under: the SHA-256 of its raw bytes.
|
|
15
|
+
*
|
|
16
|
+
* Derivable from the token value, which is why provision() does not have to return it - but only if
|
|
17
|
+
* you know the hex string is decoded to bytes before hashing rather than hashed as text. Exported so
|
|
18
|
+
* callers get that right by construction instead of reimplementing it.
|
|
19
|
+
*
|
|
20
|
+
* @param {String} token - the 64-char hex token value
|
|
21
|
+
* @returns {String} SHA-256 hex id
|
|
22
|
+
*/
|
|
23
|
+
function tokenId(token) {
|
|
24
|
+
return crypto.createHash('sha256').update(Buffer.from(token, 'hex')).digest('hex');
|
|
25
|
+
}
|
|
26
|
+
|
|
12
27
|
// Best-effort removal of an expired token record. Fired from both of the places that can
|
|
13
28
|
// notice the expiry - get(), when someone presents the token, and list(), when someone views
|
|
14
29
|
// the token list. Both are needed: a token that is minted and then never used is never
|
|
@@ -27,7 +42,29 @@ function reapExpiredToken(hashedToken, account) {
|
|
|
27
42
|
.catch(err => logger.error({ msg: 'Failed to clean up an expired access token', hash: hashedToken, err }));
|
|
28
43
|
}
|
|
29
44
|
|
|
45
|
+
// Reads one `{prefix}tokens:access` value into the shape both readers publish. Shared because the
|
|
46
|
+
// listing and the single-token getter feed the same joi schema and had already drifted: the listing
|
|
47
|
+
// assigned the decoded object as-is, so a token that had never authenticated came back as `{time:
|
|
48
|
+
// null}` with no `ip` key at all, and the admin view carries a `|| {}` guard for the case where the
|
|
49
|
+
// field is missing entirely. One shape means the getter's promise to match the listing is true.
|
|
50
|
+
function decodeAccessData(encoded, hashedToken) {
|
|
51
|
+
let accessData = {};
|
|
52
|
+
|
|
53
|
+
if (encoded) {
|
|
54
|
+
try {
|
|
55
|
+
accessData = msgpack.decode(encoded);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
// An unreadable last-use entry must not fail the read of the token it belongs to
|
|
58
|
+
logger.error({ msg: 'Failed to process token access data', hash: hashedToken, err });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { time: accessData.time ? new Date(accessData.time) : null, ip: accessData.ip || null };
|
|
63
|
+
}
|
|
64
|
+
|
|
30
65
|
module.exports = {
|
|
66
|
+
tokenId,
|
|
67
|
+
|
|
31
68
|
async getSessionToken(sessionId, account, ttl) {
|
|
32
69
|
const token = crypto.randomBytes(32);
|
|
33
70
|
const hashedToken = crypto.createHash('sha256').update(token).digest('hex');
|
|
@@ -107,7 +144,7 @@ module.exports = {
|
|
|
107
144
|
* password is a supported configuration), and the CLI needs shell access anyway.
|
|
108
145
|
*
|
|
109
146
|
* Both HTTP callers gate themselves: POST /admin/tokens/new unconditionally, since
|
|
110
|
-
* `server.auth.default('session')` leaves it open while authData is unset; POST /v1/
|
|
147
|
+
* `server.auth.default('session')` leaves it open while authData is unset; POST /v1/tokens only
|
|
111
148
|
* for the `preauth` caller that the `disableTokens` setting lets through without a credential.
|
|
112
149
|
*
|
|
113
150
|
* @param {Object} opts - Token options. May originate from a request payload.
|
|
@@ -115,7 +152,7 @@ module.exports = {
|
|
|
115
152
|
*/
|
|
116
153
|
async provision(opts) {
|
|
117
154
|
opts = opts || {};
|
|
118
|
-
const { account, restrictions, ip, remoteAddress, description, metadata, scopes, nolog, expires } = opts;
|
|
155
|
+
const { account, restrictions, permissions, ip, remoteAddress, description, metadata, scopes, nolog, expires } = opts;
|
|
119
156
|
|
|
120
157
|
const token = crypto.randomBytes(32);
|
|
121
158
|
const hashedToken = crypto.createHash('sha256').update(token).digest('hex');
|
|
@@ -166,6 +203,13 @@ module.exports = {
|
|
|
166
203
|
tokenData.restrictions = restrictions;
|
|
167
204
|
}
|
|
168
205
|
|
|
206
|
+
// Narrowing below the scope. Absent means "not narrowed", which is every token issued
|
|
207
|
+
// before this existed, so the field is only written when it was asked for - see the absent
|
|
208
|
+
// vs empty vs malformed distinction in lib/token-permissions.js.
|
|
209
|
+
if (permissions) {
|
|
210
|
+
tokenData.permissions = permissions;
|
|
211
|
+
}
|
|
212
|
+
|
|
169
213
|
if (metadata) {
|
|
170
214
|
tokenData.metadata = metadata;
|
|
171
215
|
}
|
|
@@ -206,7 +250,7 @@ module.exports = {
|
|
|
206
250
|
throw err;
|
|
207
251
|
}
|
|
208
252
|
|
|
209
|
-
const hashedToken = hashed ? token :
|
|
253
|
+
const hashedToken = hashed ? token : tokenId(token);
|
|
210
254
|
|
|
211
255
|
let tokenDataEncoded = await redis.hgetBuffer(`${REDIS_PREFIX}tokens`, hashedToken);
|
|
212
256
|
if (!tokenDataEncoded) {
|
|
@@ -242,6 +286,20 @@ module.exports = {
|
|
|
242
286
|
return Object.assign({ id: hashedToken }, tokenData);
|
|
243
287
|
},
|
|
244
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Last-use record for a token, in the shape the listings report it.
|
|
291
|
+
*
|
|
292
|
+
* Kept apart from get(), which every authenticated request runs: the last-use record lives in a
|
|
293
|
+
* second hash and nothing on the auth path reads it, so folding it in would put an extra Redis
|
|
294
|
+
* round trip on every API call to serve one page.
|
|
295
|
+
*
|
|
296
|
+
* @param {String} tokenId - SHA-256 hash of the token, the id the listings report
|
|
297
|
+
* @returns {Promise<{time: (Date|null), ip: (String|null)}>}
|
|
298
|
+
*/
|
|
299
|
+
async getAccess(tokenId) {
|
|
300
|
+
return decodeAccessData(await redis.hgetBuffer(`${REDIS_PREFIX}tokens:access`, tokenId), tokenId);
|
|
301
|
+
},
|
|
302
|
+
|
|
245
303
|
async delete(token, opts) {
|
|
246
304
|
if (!/^[0-9a-f]{64}$/i.test(token)) {
|
|
247
305
|
let err = new Error('Invalid token format');
|
|
@@ -264,7 +322,14 @@ module.exports = {
|
|
|
264
322
|
}
|
|
265
323
|
}
|
|
266
324
|
|
|
267
|
-
|
|
325
|
+
// The audit log goes with the token. It describes a credential that no longer exists, so
|
|
326
|
+
// keeping it would outlive the thing it is a record of - and the key is named after the
|
|
327
|
+
// token id, so nothing would ever collect it.
|
|
328
|
+
let req = redis
|
|
329
|
+
.multi()
|
|
330
|
+
.hdel(`${REDIS_PREFIX}tokens`, tokenData.id)
|
|
331
|
+
.hdel(`${REDIS_PREFIX}tokens:access`, tokenData.id)
|
|
332
|
+
.del(tokenAuditLog.logKey(tokenData.id));
|
|
268
333
|
if (tokenData.account) {
|
|
269
334
|
req = req.srem(`${REDIS_PREFIX}iat:${tokenData.account}`, tokenData.id);
|
|
270
335
|
} else {
|
|
@@ -352,14 +417,28 @@ module.exports = {
|
|
|
352
417
|
return tokenData;
|
|
353
418
|
},
|
|
354
419
|
|
|
355
|
-
|
|
420
|
+
/**
|
|
421
|
+
* Lists tokens.
|
|
422
|
+
*
|
|
423
|
+
* @param {String} [account] - list tokens bound to this account
|
|
424
|
+
* @param {Number} [page] - zero-based page
|
|
425
|
+
* @param {Number} [pageSize] - entries per page
|
|
426
|
+
* @param {String} [query] - filter on id, description or account
|
|
427
|
+
* @param {Object} [opts]
|
|
428
|
+
* @param {Boolean} [opts.all] - every token on the instance, bound or not. Reads the token hash
|
|
429
|
+
* directly rather than walking one index per account: the account is stored on each record, so
|
|
430
|
+
* the union is already there, and enumerating accounts would be a round trip each.
|
|
431
|
+
*/
|
|
432
|
+
async list(account, page, pageSize, query, opts) {
|
|
356
433
|
page = Math.max(Number(page) || 0, 0);
|
|
357
434
|
pageSize = Math.max(Number(pageSize) || 20, 1);
|
|
358
435
|
|
|
359
436
|
let startPos = page * pageSize;
|
|
360
437
|
|
|
361
438
|
let list;
|
|
362
|
-
if (
|
|
439
|
+
if (opts && opts.all) {
|
|
440
|
+
list = await redis.hkeys(`${REDIS_PREFIX}tokens`);
|
|
441
|
+
} else if (account) {
|
|
363
442
|
list = await redis.smembers(`${REDIS_PREFIX}iat:${account}`);
|
|
364
443
|
} else {
|
|
365
444
|
list = await redis.smembers(`${REDIS_PREFIX}iat`);
|
|
@@ -417,20 +496,18 @@ module.exports = {
|
|
|
417
496
|
if (tokenData.expires) {
|
|
418
497
|
tokenData.expires = new Date(tokenData.expires);
|
|
419
498
|
}
|
|
420
|
-
|
|
499
|
+
// `account` is absent on the record for an unbound token, so it is filled in
|
|
500
|
+
// explicitly: a listing that mixes bound and unbound tokens should report one
|
|
501
|
+
// shape, not two, and `null` is the answer to "which account" for a token
|
|
502
|
+
// that is not bound to one.
|
|
503
|
+
lastEntry = Object.assign({ id: tokenHash, account: null }, tokenData);
|
|
421
504
|
response.tokens.push(lastEntry);
|
|
422
505
|
} catch (err) {
|
|
423
506
|
logger.error({ msg: 'Failed to process token data', hash: tokenHash, err });
|
|
424
507
|
}
|
|
425
508
|
}
|
|
426
509
|
} else if (lastEntry && entry[1]) {
|
|
427
|
-
|
|
428
|
-
let accessData = msgpack.decode(entry[1]);
|
|
429
|
-
accessData.time = accessData.time ? new Date(accessData.time) : null;
|
|
430
|
-
lastEntry.access = accessData;
|
|
431
|
-
} catch (err) {
|
|
432
|
-
logger.error({ msg: 'Failed to process token data', hash: tokenHash, err });
|
|
433
|
-
}
|
|
510
|
+
lastEntry.access = decodeAccessData(entry[1], tokenHash);
|
|
434
511
|
}
|
|
435
512
|
}
|
|
436
513
|
|
|
@@ -438,9 +515,16 @@ module.exports = {
|
|
|
438
515
|
let paged = filterListPage(response.tokens, ['id', 'description', 'account'], query, startPos, pageSize);
|
|
439
516
|
response.tokens = paged.entries;
|
|
440
517
|
response.total = paged.total;
|
|
441
|
-
response.pages = paged.pages;
|
|
442
518
|
}
|
|
443
519
|
|
|
520
|
+
// Derived last, from whichever total survived. The loop above reaps expired tokens and
|
|
521
|
+
// decrements `total` as it goes, so a count taken before it describes a listing that no
|
|
522
|
+
// longer exists - three tokens with the first expired reported `{total: 2, pages: 3}`. Only
|
|
523
|
+
// the visible page is inspected, so an expired token further along is still counted until a
|
|
524
|
+
// later call reaches it; what this guarantees is that `pages` and `total` in one response
|
|
525
|
+
// are two views of one number. Same formula filterListPage() applies to the filtered set.
|
|
526
|
+
response.pages = Math.ceil(response.total / pageSize);
|
|
527
|
+
|
|
444
528
|
return response;
|
|
445
529
|
}
|
|
446
530
|
};
|