Skip to content

Commit 3403593

Browse files
committed
Improve token authentication error handling
1 parent ec220a5 commit 3403593

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

lib/errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const ERR_POOL_HAS_BUSY_CONNECTIONS = 104;
109109
const ERR_NAN_VALUE = 105;
110110
const ERR_INVALID_REF_CURSOR = 107;
111111
const ERR_LOB_CLOSED = 108;
112+
const ERR_INVALID_PRIVATE_KEY = 117;
112113

113114
// define mapping for ODPI-C errors that need to be wrapped with NJS errors
114115
const adjustErrorXref = new Map();
@@ -284,6 +285,8 @@ messages.set(ERR_INVALID_REF_CURSOR,
284285
'invalid cursor');
285286
messages.set(ERR_LOB_CLOSED,
286287
'LOB was already closed');
288+
messages.set(ERR_INVALID_PRIVATE_KEY,
289+
'invalid private key. Headers and footers are not allowed');
287290

288291
//-----------------------------------------------------------------------------
289292
// assert()
@@ -544,6 +547,7 @@ module.exports = {
544547
ERR_INVALID_BIND_NAME,
545548
ERR_INVALID_REF_CURSOR,
546549
ERR_LOB_CLOSED,
550+
ERR_INVALID_PRIVATE_KEY,
547551
assert,
548552
assertArgCount,
549553
assertParamPropBool,

lib/oracledb.js

+4
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ async function _verifyOptions(options, inCreatePool) {
366366
}
367367
errors.assert(nodbUtil.isTokenValid(accessToken),
368368
errors.ERR_TOKEN_HAS_EXPIRED);
369+
if (accessToken.privateKey !== undefined) {
370+
errors.assert(nodbUtil.isPrivateKeyValid(accessToken),
371+
errors.ERR_INVALID_PRIVATE_KEY);
372+
}
369373

370374
// store token and privatekey
371375
if (typeof accessToken === 'string') {

lib/util.js

+13
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ function isTokenValid(accessToken) {
291291
}
292292
}
293293

294+
function isPrivateKeyValid(accessToken) {
295+
errors.assert(typeof accessToken.privateKey === 'string',
296+
errors.ERR_TOKEN_BASED_AUTH);
297+
298+
if (accessToken.privateKey.startsWith("-----BEGIN PRIVATE KEY-----") ||
299+
accessToken.privateKey.endsWith("-----END PRIVATE KEY-----")) {
300+
return false;
301+
}
302+
303+
return true;
304+
}
305+
294306
// define exports
295307
module.exports = {
296308
BINARY_FILE,
@@ -303,6 +315,7 @@ module.exports = {
303315
isArrayOfStrings,
304316
isObject,
305317
isObjectOrArray,
318+
isPrivateKeyValid,
306319
isShardingKey,
307320
isSodaDocument,
308321
isTokenExpired,

0 commit comments

Comments
 (0)