lemonldap-ng-handler 0.9.6 → 0.10.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/CHANGELOG.md +29 -0
- package/lib/cda.d.mts +64 -0
- package/lib/handlerAuthBasic.d.mts +53 -0
- package/lib/handlerCDA.d.mts +58 -0
- package/lib/handlerDevOps.d.mts +21 -0
- package/lib/handlerDevOpsST.d.mts +9 -0
- package/lib/handlerMain.d.mts +37 -0
- package/lib/handlerMain.d.ts +3 -3
- package/lib/handlerOAuth2.d.mts +140 -0
- package/lib/handlerOAuth2.d.ts +121 -10
- package/lib/handlerServiceToken.d.mts +7 -0
- package/lib/index.d.mts +20 -0
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +431 -91
- package/lib/index.mjs.map +1 -1
- package/lib/init.d.mts +79 -0
- package/lib/msgActions.d.mts +34 -0
- package/lib/tsv.d.mts +84 -0
- package/lib/tsv.d.ts +2 -0
- package/package.json +19 -11
package/lib/index.mjs
CHANGED
|
@@ -227,10 +227,11 @@ class HandlerInit {
|
|
|
227
227
|
/**
|
|
228
228
|
* Sessions storage initialization
|
|
229
229
|
*/
|
|
230
|
-
//['global','oidc'].forEach
|
|
231
230
|
if (!conf["globalStorage"] || !conf["globalStorageOptions"])
|
|
232
231
|
// istanbul ignore next
|
|
233
232
|
throw new Error("Missing session storage configuration");
|
|
233
|
+
this.tsv.sessionStorageModule = conf["globalStorage"];
|
|
234
|
+
this.tsv.sessionStorageOptions = conf["globalStorageOptions"];
|
|
234
235
|
try {
|
|
235
236
|
this.sessionAcc = new Session({
|
|
236
237
|
storageModule: conf["globalStorage"],
|
|
@@ -243,6 +244,29 @@ class HandlerInit {
|
|
|
243
244
|
cause: e,
|
|
244
245
|
});
|
|
245
246
|
}
|
|
247
|
+
/**
|
|
248
|
+
* OIDC/OAuth2 initialization (used by the OAuth2 handler type)
|
|
249
|
+
*/
|
|
250
|
+
// Access token sessions are stored apart only when oidcStorage is
|
|
251
|
+
// set, else they share the global storage
|
|
252
|
+
if (conf.oidcStorage) {
|
|
253
|
+
this.tsv.oidcStorageModule = conf.oidcStorage;
|
|
254
|
+
this.tsv.oidcStorageOptions = conf.oidcStorageOptions || {};
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
this.tsv.oidcStorageModule = conf["globalStorage"];
|
|
258
|
+
this.tsv.oidcStorageOptions = conf["globalStorageOptions"];
|
|
259
|
+
}
|
|
260
|
+
// Keep the client id of each relying party, exposed to rules and
|
|
261
|
+
// headers as $_clientId
|
|
262
|
+
this.tsv.oauth2Options = {};
|
|
263
|
+
Object.keys(conf.oidcRPMetaDataOptions || {}).forEach((rp) => {
|
|
264
|
+
var _a;
|
|
265
|
+
const clientId = (_a = conf.oidcRPMetaDataOptions[rp]) === null || _a === void 0 ? void 0 : _a.oidcRPMetaDataOptionsClientID;
|
|
266
|
+
// @ts-ignore: oauth2Options has just been initialized
|
|
267
|
+
if (clientId)
|
|
268
|
+
this.tsv.oauth2Options[rp] = { clientId };
|
|
269
|
+
});
|
|
246
270
|
/**
|
|
247
271
|
* Location rules initialization
|
|
248
272
|
*/
|
|
@@ -604,10 +628,13 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
604
628
|
this.hideCookie(req);
|
|
605
629
|
return next();
|
|
606
630
|
}
|
|
607
|
-
/* 4 - search for LLNG cookie */
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
631
|
+
/* 4 - search for LLNG cookie (or any other credential, see subclasses) */
|
|
632
|
+
Promise.resolve(this.fetchId(req))
|
|
633
|
+
.then((id) => {
|
|
634
|
+
if (id === "") {
|
|
635
|
+
return this.goToPortal(res, this.selfUri(vhost, uri));
|
|
636
|
+
}
|
|
637
|
+
this.retrieveSession(id, req)
|
|
611
638
|
.then((session) => {
|
|
612
639
|
const user = session[this.tsv.whatToTrace];
|
|
613
640
|
this.userLogger.debug(`User ${user} identified`);
|
|
@@ -633,10 +660,12 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
633
660
|
/* Expired session */
|
|
634
661
|
this.goToPortal(res, this.selfUri(vhost, uri));
|
|
635
662
|
});
|
|
636
|
-
}
|
|
637
|
-
|
|
663
|
+
})
|
|
664
|
+
// istanbul ignore next
|
|
665
|
+
.catch((e) => {
|
|
666
|
+
this.userLogger.error(`Unable to fetch session id: ${e}`);
|
|
638
667
|
this.goToPortal(res, this.selfUri(vhost, uri));
|
|
639
|
-
}
|
|
668
|
+
});
|
|
640
669
|
}
|
|
641
670
|
selfUri(vhost, uri) {
|
|
642
671
|
return ((this.tsv.https && this.tsv.https[vhost] ? "https" : "http") +
|
|
@@ -706,7 +735,7 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
706
735
|
}
|
|
707
736
|
return "";
|
|
708
737
|
}
|
|
709
|
-
retrieveSession(id) {
|
|
738
|
+
retrieveSession(id, _req) {
|
|
710
739
|
return new Promise((resolve, reject) => {
|
|
711
740
|
if (this.sessionAcc === undefined)
|
|
712
741
|
// istanbul ignore next
|
|
@@ -1039,109 +1068,420 @@ class LemonldapNGHandlerDevOpsST extends LemonldapNGHandlerDevOps {
|
|
|
1039
1068
|
|
|
1040
1069
|
/**
|
|
1041
1070
|
* OAuth2/OIDC Handler for LemonLDAP::NG
|
|
1042
|
-
* Port of Lemonldap::NG::Handler::OAuth2
|
|
1071
|
+
* Port of Lemonldap::NG::Handler::Lib::OAuth2
|
|
1072
|
+
*
|
|
1073
|
+
* A Bearer access token is an *opaque credential*: the token itself (or the
|
|
1074
|
+
* `jti` claim of its JWT serialization) is the identifier of an access token
|
|
1075
|
+
* session created by the Portal. This handler therefore:
|
|
1043
1076
|
*
|
|
1044
|
-
*
|
|
1045
|
-
* the
|
|
1077
|
+
* 1. resolves the access token session in the OIDC session storage,
|
|
1078
|
+
* 2. follows it to the user session (or offline session) it was issued for,
|
|
1079
|
+
* 3. exposes the token attributes (scope, client, audiences, grant type) as
|
|
1080
|
+
* session attributes so that rules and headers can use them.
|
|
1081
|
+
*
|
|
1082
|
+
* Nothing but the session identifier is ever read from the token: the trust
|
|
1083
|
+
* anchor is the session store, not the token payload.
|
|
1046
1084
|
*/
|
|
1085
|
+
/* Prefix used by fetchId() to mark offline session ids */
|
|
1086
|
+
const OFFLINE_PREFIX = "O-";
|
|
1087
|
+
/**
|
|
1088
|
+
* Shape of a session identifier.
|
|
1089
|
+
*
|
|
1090
|
+
* The value carried by a Bearer token is attacker controlled and is handed
|
|
1091
|
+
* over as-is to the session storage, where some backends interpolate it into
|
|
1092
|
+
* a LDAP DN (@lemonldap-ng/session-ldap) or into an URL
|
|
1093
|
+
* (@lemonldap-ng/session-rest). Only accept what the Portal may have
|
|
1094
|
+
* generated, so that a crafted `jti` can not escape the lookup.
|
|
1095
|
+
*/
|
|
1096
|
+
const SESSION_ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
1047
1097
|
class LemonldapNGHandlerOAuth2 extends LemonldapNGHandler {
|
|
1098
|
+
constructor() {
|
|
1099
|
+
super(...arguments);
|
|
1100
|
+
this.ownOidcSessionAcc = false;
|
|
1101
|
+
this.state = new WeakMap();
|
|
1102
|
+
}
|
|
1048
1103
|
/**
|
|
1049
|
-
* Override init to
|
|
1104
|
+
* Override reload() - and not init() - to set up the OIDC session storage.
|
|
1105
|
+
*
|
|
1106
|
+
* init() runs once, while reload() runs again on every configuration change
|
|
1107
|
+
* announced by the message broker, and it rebuilds the main session
|
|
1108
|
+
* accessor. The OIDC accessor must follow: else the handler would keep
|
|
1109
|
+
* querying the previous storage (with its own cache), localUnlog() would
|
|
1110
|
+
* only purge the new one, and a modified oidcStorage would never be applied.
|
|
1050
1111
|
*/
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
super
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1112
|
+
reload() {
|
|
1113
|
+
const _super = Object.create(null, {
|
|
1114
|
+
reload: { get: () => super.reload }
|
|
1115
|
+
});
|
|
1116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1117
|
+
const res = yield _super.reload.call(this);
|
|
1118
|
+
yield this.initOidcSessionStorage();
|
|
1119
|
+
return res;
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* (Re)build the accessor used for access token and offline sessions
|
|
1124
|
+
*
|
|
1125
|
+
* The new accessor is only installed once it is known to be usable: a
|
|
1126
|
+
* reload announcing a broken oidcStorage must leave the handler running on
|
|
1127
|
+
* its previous storage rather than answering 401 to every token, and must
|
|
1128
|
+
* not leak the accessor it failed to replace (its node-persist timers would
|
|
1129
|
+
* keep the process alive).
|
|
1130
|
+
*/
|
|
1131
|
+
initOidcSessionStorage() {
|
|
1132
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1133
|
+
const storageModule = this.tsv.oidcStorageModule;
|
|
1134
|
+
if (!storageModule) {
|
|
1135
|
+
// istanbul ignore next
|
|
1136
|
+
throw new Error("OAuth2 handler requires a session storage");
|
|
1137
|
+
}
|
|
1138
|
+
let candidate;
|
|
1139
|
+
let owned;
|
|
1140
|
+
// Reuse the main accessor when both storages are identical, as it is the
|
|
1141
|
+
// case when no dedicated oidcStorage is configured
|
|
1142
|
+
if (storageModule === this.tsv.sessionStorageModule &&
|
|
1143
|
+
JSON.stringify(this.tsv.oidcStorageOptions || {}) ===
|
|
1144
|
+
JSON.stringify(this.tsv.sessionStorageOptions || {})) {
|
|
1145
|
+
this.userLogger.debug("OAuth2 handler shares the main session storage");
|
|
1146
|
+
candidate = this.sessionAcc;
|
|
1147
|
+
owned = false;
|
|
1148
|
+
}
|
|
1149
|
+
else {
|
|
1150
|
+
try {
|
|
1151
|
+
candidate = new Session({
|
|
1152
|
+
storageModule,
|
|
1153
|
+
storageModuleOptions: this.tsv.oidcStorageOptions || {},
|
|
1154
|
+
});
|
|
1155
|
+
yield candidate.ready;
|
|
1077
1156
|
}
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1157
|
+
catch (e) {
|
|
1158
|
+
throw new Error(`Unable to load OIDC session module: ${e}`, {
|
|
1159
|
+
cause: e,
|
|
1160
|
+
});
|
|
1082
1161
|
}
|
|
1083
|
-
|
|
1084
|
-
.
|
|
1162
|
+
owned = true;
|
|
1163
|
+
this.userLogger.debug("OAuth2 OIDC session storage initialized");
|
|
1164
|
+
}
|
|
1165
|
+
// Accessor built by a previous reload(), to be closed once replaced
|
|
1166
|
+
const previous = this.ownOidcSessionAcc ? this.oidcSessionAcc : undefined;
|
|
1167
|
+
this.oidcSessionAcc = candidate;
|
|
1168
|
+
this.ownOidcSessionAcc = owned;
|
|
1169
|
+
if (previous && previous !== candidate)
|
|
1170
|
+
yield previous.close();
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Override localUnlog to purge the OIDC caches too
|
|
1175
|
+
*
|
|
1176
|
+
* When the Portal revokes an access token, it broadcasts an unlog/delSession
|
|
1177
|
+
* message holding the id of the deleted session. With a dedicated
|
|
1178
|
+
* oidcStorage, that session lives in the OIDC accessor, whose caches
|
|
1179
|
+
* Main::localUnlog() does not know about: the revoked token would keep
|
|
1180
|
+
* authenticating until the cache entry expires.
|
|
1181
|
+
* @param sessionId - Session ID to clear (uses last cached if not provided)
|
|
1182
|
+
*/
|
|
1183
|
+
localUnlog(sessionId) {
|
|
1184
|
+
const _super = Object.create(null, {
|
|
1185
|
+
localUnlog: { get: () => super.localUnlog }
|
|
1186
|
+
});
|
|
1187
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1188
|
+
// super.localUnlog() resets lastSessionId, read it first
|
|
1189
|
+
const id = sessionId || this.lastSessionId;
|
|
1190
|
+
yield _super.localUnlog.call(this, sessionId);
|
|
1191
|
+
// In the shared storage case both accessors are the same object, which
|
|
1192
|
+
// super.localUnlog() has already purged
|
|
1193
|
+
if (id && this.ownOidcSessionAcc && this.oidcSessionAcc) {
|
|
1194
|
+
yield this.oidcSessionAcc.clearAllCaches(id);
|
|
1195
|
+
}
|
|
1085
1196
|
});
|
|
1086
1197
|
}
|
|
1087
1198
|
/**
|
|
1088
|
-
* Override
|
|
1199
|
+
* Override run to bind the per-request state to both req and res: fetchId()
|
|
1200
|
+
* and retrieveSession() only see the request, goToPortal() only sees the
|
|
1201
|
+
* response.
|
|
1202
|
+
*/
|
|
1203
|
+
run(req, res, next) {
|
|
1204
|
+
const state = {};
|
|
1205
|
+
this.state.set(req, state);
|
|
1206
|
+
this.state.set(res, state);
|
|
1207
|
+
return super.run(req, res, next);
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Override fetchId to resolve an OAuth2 Bearer token into a session id
|
|
1211
|
+
*
|
|
1212
|
+
* Contrary to the cookie case, the value carried by the request is *not* the
|
|
1213
|
+
* user session id: it identifies an access token session, which in turn
|
|
1214
|
+
* references the user session (or an offline session).
|
|
1089
1215
|
* @param req - HTTP request
|
|
1090
1216
|
*/
|
|
1091
1217
|
fetchId(req) {
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1218
|
+
const _super = Object.create(null, {
|
|
1219
|
+
fetchId: { get: () => super.fetchId }
|
|
1220
|
+
});
|
|
1221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1222
|
+
const authHeader = req.headers.authorization;
|
|
1223
|
+
if (!authHeader || !/^bearer\s/i.test(authHeader)) {
|
|
1224
|
+
// Fall back to cookie-based authentication
|
|
1225
|
+
return _super.fetchId.call(this, req);
|
|
1226
|
+
}
|
|
1227
|
+
const accessToken = authHeader.replace(/^bearer\s+/i, "").trim();
|
|
1228
|
+
// An empty Bearer token carries no credential, but the request may still
|
|
1229
|
+
// hold a valid cookie: the Perl handler reaches Main::fetchId here
|
|
1230
|
+
if (!accessToken) {
|
|
1231
|
+
this.userLogger.info("OAuth2: empty Bearer token");
|
|
1232
|
+
return this.fallbackToCookie(req);
|
|
1233
|
+
}
|
|
1234
|
+
// The token is either the access token session id, or a JWT whose `jti`
|
|
1235
|
+
// claim holds it
|
|
1236
|
+
const accessTokenId = getAccessTokenSessionId(accessToken);
|
|
1237
|
+
if (!accessTokenId) {
|
|
1238
|
+
return this.invalidToken(req, "No session id found in access token");
|
|
1239
|
+
}
|
|
1240
|
+
// The token is not signed, and even a signed one is not trusted here: the
|
|
1241
|
+
// id is about to be given to the session storage, so refuse anything that
|
|
1242
|
+
// does not look like a session id
|
|
1243
|
+
if (!this.isSessionId(accessTokenId)) {
|
|
1244
|
+
return this.invalidToken(req, "Malformed access token session id");
|
|
1245
|
+
}
|
|
1246
|
+
const lookup = yield this.getOIDCInfos(accessTokenId);
|
|
1247
|
+
// An expired or corrupted access token session is a hard failure
|
|
1248
|
+
if (lookup.status === "invalid") {
|
|
1249
|
+
return this.invalidToken(req, "Access token session is not usable");
|
|
1250
|
+
}
|
|
1251
|
+
// An unknown access token is not necessarily an attack: the request may
|
|
1252
|
+
// also carry a valid cookie. Behave as the Perl handler and fall back.
|
|
1253
|
+
if (lookup.status === "notFound") {
|
|
1254
|
+
this.userLogger.info(`Unknown access token session ${accessTokenId}`);
|
|
1255
|
+
return this.fallbackToCookie(req);
|
|
1256
|
+
}
|
|
1257
|
+
const infos = lookup.data;
|
|
1258
|
+
// Scope, client and audiences, to be exposed as session attributes. They
|
|
1259
|
+
// are only published once the token is known to grant a session: a token
|
|
1260
|
+
// that ends up on the cookie fallback below must not lend its scope to
|
|
1261
|
+
// the session the cookie points to. This diverges from the Perl handler,
|
|
1262
|
+
// which sets $req->data->{_scope} before falling back and therefore lets
|
|
1263
|
+
// a bare client credentials token satisfy a $_scope based rule.
|
|
1264
|
+
const attrs = infos.rp
|
|
1265
|
+
? this.tokenAttributes(accessToken, infos)
|
|
1266
|
+
: undefined;
|
|
1267
|
+
// Token tied to a regular session
|
|
1268
|
+
if (infos.user_session_id) {
|
|
1269
|
+
if (!this.isSessionId(infos.user_session_id)) {
|
|
1270
|
+
return this.invalidToken(req, `Malformed user_session_id in access token session ${accessTokenId}`);
|
|
1102
1271
|
}
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1272
|
+
const state = this.stateFor(req);
|
|
1273
|
+
state.viaToken = true;
|
|
1274
|
+
state.attrs = attrs;
|
|
1275
|
+
this.userLogger.debug(`OAuth2: get user session ${infos.user_session_id}`);
|
|
1276
|
+
return infos.user_session_id;
|
|
1277
|
+
}
|
|
1278
|
+
// Token tied to an offline session
|
|
1279
|
+
if (infos.offline_session_id) {
|
|
1280
|
+
if (!this.isSessionId(infos.offline_session_id)) {
|
|
1281
|
+
return this.invalidToken(req, `Malformed offline_session_id in access token session ${accessTokenId}`);
|
|
1106
1282
|
}
|
|
1283
|
+
const state = this.stateFor(req);
|
|
1284
|
+
state.viaToken = true;
|
|
1285
|
+
state.attrs = attrs;
|
|
1286
|
+
this.userLogger.debug(`OAuth2: get offline session ${infos.offline_session_id}`);
|
|
1287
|
+
return `${OFFLINE_PREFIX}${infos.offline_session_id}`;
|
|
1107
1288
|
}
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1289
|
+
this.userLogger.warn(`Access token session ${accessTokenId} references no session`);
|
|
1290
|
+
return this.fallbackToCookie(req);
|
|
1291
|
+
});
|
|
1111
1292
|
}
|
|
1112
1293
|
/**
|
|
1113
|
-
* Override retrieveSession to
|
|
1114
|
-
*
|
|
1294
|
+
* Override retrieveSession to handle offline sessions and to expose the
|
|
1295
|
+
* access token attributes to rules and headers
|
|
1296
|
+
* @param id - Session ID, prefixed by "O-" for offline sessions
|
|
1297
|
+
* @param req - HTTP request, when available
|
|
1115
1298
|
*/
|
|
1116
|
-
retrieveSession(id) {
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
const now = (Date.now() / 1000) | 0;
|
|
1127
|
-
if (now - session._utime > this.tsv.timeout ||
|
|
1128
|
-
(this.tsv.timeoutActivity &&
|
|
1129
|
-
session._lastSeen &&
|
|
1130
|
-
// @ts-ignore: session._lastSeen is defined
|
|
1131
|
-
now - session._lastSeen > this.tsv.timeoutActivity)) {
|
|
1132
|
-
reject(`OAuth2 session ${id} expired`);
|
|
1133
|
-
}
|
|
1134
|
-
else {
|
|
1135
|
-
resolve(session);
|
|
1136
|
-
}
|
|
1137
|
-
})
|
|
1138
|
-
.catch((_e) => {
|
|
1139
|
-
// If not found in OIDC storage, try main storage
|
|
1140
|
-
super.retrieveSession(id).then(resolve).catch(reject);
|
|
1141
|
-
});
|
|
1299
|
+
retrieveSession(id, req) {
|
|
1300
|
+
var _a;
|
|
1301
|
+
const attrs = req ? (_a = this.state.get(req)) === null || _a === void 0 ? void 0 : _a.attrs : undefined;
|
|
1302
|
+
if (!id.startsWith(OFFLINE_PREFIX)) {
|
|
1303
|
+
return super.retrieveSession(id, req).then((session) => this.withTokenAttributes(session, attrs), (e) => {
|
|
1304
|
+
var _a;
|
|
1305
|
+
if (req && ((_a = this.state.get(req)) === null || _a === void 0 ? void 0 : _a.viaToken)) {
|
|
1306
|
+
this.stateFor(req).error = "invalid_token";
|
|
1307
|
+
}
|
|
1308
|
+
throw e;
|
|
1142
1309
|
});
|
|
1143
1310
|
}
|
|
1144
|
-
|
|
1311
|
+
// Offline sessions live in the OIDC storage
|
|
1312
|
+
const offlineId = id.substring(OFFLINE_PREFIX.length);
|
|
1313
|
+
return this.getOIDCInfos(offlineId).then((lookup) => {
|
|
1314
|
+
if (lookup.status !== "ok") {
|
|
1315
|
+
if (req)
|
|
1316
|
+
this.stateFor(req).error = "invalid_token";
|
|
1317
|
+
throw `Offline session ${offlineId} can not be retrieved`;
|
|
1318
|
+
}
|
|
1319
|
+
return this.withTokenAttributes(lookup.data, attrs);
|
|
1320
|
+
},
|
|
1321
|
+
// istanbul ignore next
|
|
1322
|
+
(e) => {
|
|
1323
|
+
if (req)
|
|
1324
|
+
this.stateFor(req).error = "invalid_token";
|
|
1325
|
+
throw e;
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Hide the access token from the protected application
|
|
1330
|
+
* @param req - HTTP request
|
|
1331
|
+
*/
|
|
1332
|
+
hideCookie(req) {
|
|
1333
|
+
if (req.headers.authorization) {
|
|
1334
|
+
this.userLogger.debug("OAuth2: removing Authorization header");
|
|
1335
|
+
delete req.headers.authorization;
|
|
1336
|
+
}
|
|
1337
|
+
super.hideCookie(req);
|
|
1338
|
+
}
|
|
1339
|
+
/**
|
|
1340
|
+
* A resource server never redirects: answer with a 401 and the challenge
|
|
1341
|
+
* described in RFC 6750 section 3
|
|
1342
|
+
* @param res - HTTP response
|
|
1343
|
+
* @param _uri - Original URI (unused)
|
|
1344
|
+
* @param _args - Additional arguments (unused)
|
|
1345
|
+
*/
|
|
1346
|
+
goToPortal(res, _uri, _args = "") {
|
|
1347
|
+
var _a;
|
|
1348
|
+
const error = (_a = this.state.get(res)) === null || _a === void 0 ? void 0 : _a.error;
|
|
1349
|
+
res.setHeader("WWW-Authenticate", "Bearer" + (error ? ` error="${error}"` : ""));
|
|
1350
|
+
// res.status is defined when running under express. If not we are running
|
|
1351
|
+
// as FastCGI server
|
|
1352
|
+
// @ts-ignore: res.status may exist
|
|
1353
|
+
if (res.status) {
|
|
1354
|
+
// @ts-ignore: res.status may exist
|
|
1355
|
+
res.status(401).send("Unauthorized");
|
|
1356
|
+
}
|
|
1357
|
+
else {
|
|
1358
|
+
res.writeHead(401, "Unauthorized");
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Close the dedicated OIDC session storage, if any
|
|
1363
|
+
*/
|
|
1364
|
+
stopEventLoop() {
|
|
1365
|
+
const _super = Object.create(null, {
|
|
1366
|
+
stopEventLoop: { get: () => super.stopEventLoop }
|
|
1367
|
+
});
|
|
1368
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1369
|
+
yield _super.stopEventLoop.call(this);
|
|
1370
|
+
if (this.ownOidcSessionAcc && this.oidcSessionAcc) {
|
|
1371
|
+
yield this.oidcSessionAcc.close();
|
|
1372
|
+
this.userLogger.debug("OIDC session storage closed");
|
|
1373
|
+
}
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
/**
|
|
1377
|
+
* Get a session from the OIDC storage and check that it is not expired
|
|
1378
|
+
*
|
|
1379
|
+
* Note: the session backends do not tell an unknown session apart from an
|
|
1380
|
+
* unreachable storage, so both end up as "notFound" here, exactly as in the
|
|
1381
|
+
* Perl handler and as in the cookie path of Handler::Main. A storage outage
|
|
1382
|
+
* is therefore reported to the client as an invalid token. Fixing this
|
|
1383
|
+
* requires a "not found" signal in every @lemonldap-ng/session-* backend.
|
|
1384
|
+
* @param id - Access token, offline or OIDC session id
|
|
1385
|
+
*/
|
|
1386
|
+
getOIDCInfos(id) {
|
|
1387
|
+
if (this.oidcSessionAcc === undefined) {
|
|
1388
|
+
// istanbul ignore next
|
|
1389
|
+
return Promise.resolve({ status: "invalid" });
|
|
1390
|
+
}
|
|
1391
|
+
return this.oidcSessionAcc.get(id).then((data) => {
|
|
1392
|
+
// A non numeric _utime would turn the comparison below into
|
|
1393
|
+
// `NaN > timeout`, i.e. false, and grant an unbounded lifetime
|
|
1394
|
+
const utime = Number(data._utime);
|
|
1395
|
+
if (!data._utime || !Number.isFinite(utime)) {
|
|
1396
|
+
this.userLogger.error(`_utime missing from OIDC session ${id}`);
|
|
1397
|
+
return { status: "invalid" };
|
|
1398
|
+
}
|
|
1399
|
+
const now = (Date.now() / 1000) | 0;
|
|
1400
|
+
if (now - utime > this.tsv.timeout) {
|
|
1401
|
+
this.userLogger.info(`OIDC session ${id} expired`);
|
|
1402
|
+
return { status: "invalid" };
|
|
1403
|
+
}
|
|
1404
|
+
this.userLogger.debug(`Get OIDC session ${id}`);
|
|
1405
|
+
return { status: "ok", data };
|
|
1406
|
+
}, (e) => {
|
|
1407
|
+
this.userLogger.info(`OIDC session ${id} can not be retrieved: ${e}`);
|
|
1408
|
+
return { status: "notFound" };
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1412
|
+
* Collect the access token session fields exposed to rules and headers
|
|
1413
|
+
* @param accessToken - Bearer token, as received
|
|
1414
|
+
* @param infos - Access token session
|
|
1415
|
+
*/
|
|
1416
|
+
tokenAttributes(accessToken, infos) {
|
|
1417
|
+
var _a, _b;
|
|
1418
|
+
const attrs = {
|
|
1419
|
+
_accessToken: accessToken,
|
|
1420
|
+
_clientConfKey: infos.rp,
|
|
1421
|
+
};
|
|
1422
|
+
if (infos.scope !== undefined)
|
|
1423
|
+
attrs._scope = infos.scope;
|
|
1424
|
+
if (infos.grant_type !== undefined) {
|
|
1425
|
+
attrs._oidc_grant_type = infos.grant_type;
|
|
1426
|
+
}
|
|
1427
|
+
if (infos.aud !== undefined)
|
|
1428
|
+
attrs._audiences = infos.aud;
|
|
1429
|
+
const clientId = (_b = (_a = this.tsv.oauth2Options) === null || _a === void 0 ? void 0 : _a[infos.rp]) === null || _b === void 0 ? void 0 : _b.clientId;
|
|
1430
|
+
if (clientId)
|
|
1431
|
+
attrs._clientId = clientId;
|
|
1432
|
+
return attrs;
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* Build a copy of the session enriched with the access token attributes.
|
|
1436
|
+
* A copy is mandatory: the session object may be shared with the session
|
|
1437
|
+
* caches.
|
|
1438
|
+
*/
|
|
1439
|
+
withTokenAttributes(session, attrs) {
|
|
1440
|
+
return attrs ? Object.assign(Object.assign({}, session), attrs) : session;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Check that a value may be handed over to the session storage
|
|
1444
|
+
*
|
|
1445
|
+
* Applied both to the id carried by the token and to the ids read from the
|
|
1446
|
+
* access token session: the latter are written by the Portal, but they end
|
|
1447
|
+
* up in a LDAP DN or in an URL just the same.
|
|
1448
|
+
* @param id - Candidate session id
|
|
1449
|
+
*/
|
|
1450
|
+
isSessionId(id) {
|
|
1451
|
+
return typeof id === "string" && SESSION_ID_RE.test(id);
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* Get (or create) the state attached to a request or a response
|
|
1455
|
+
*/
|
|
1456
|
+
stateFor(o) {
|
|
1457
|
+
let state = this.state.get(o);
|
|
1458
|
+
if (!state) {
|
|
1459
|
+
state = {};
|
|
1460
|
+
this.state.set(o, state);
|
|
1461
|
+
}
|
|
1462
|
+
return state;
|
|
1463
|
+
}
|
|
1464
|
+
/**
|
|
1465
|
+
* Reject the request with an "invalid_token" OAuth2 error
|
|
1466
|
+
*/
|
|
1467
|
+
invalidToken(req, msg) {
|
|
1468
|
+
this.userLogger.warn(`OAuth2: ${msg}`);
|
|
1469
|
+
this.stateFor(req).error = "invalid_token";
|
|
1470
|
+
return "";
|
|
1471
|
+
}
|
|
1472
|
+
/**
|
|
1473
|
+
* Use the LLNG cookie when the Bearer token can not be resolved
|
|
1474
|
+
*/
|
|
1475
|
+
fallbackToCookie(req) {
|
|
1476
|
+
const _super = Object.create(null, {
|
|
1477
|
+
fetchId: { get: () => super.fetchId }
|
|
1478
|
+
});
|
|
1479
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1480
|
+
const id = yield _super.fetchId.call(this, req);
|
|
1481
|
+
if (id === "")
|
|
1482
|
+
this.stateFor(req).error = "invalid_token";
|
|
1483
|
+
return id;
|
|
1484
|
+
});
|
|
1145
1485
|
}
|
|
1146
1486
|
}
|
|
1147
1487
|
|