-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth.js
More file actions
31 lines (24 loc) · 713 Bytes
/
auth.js
File metadata and controls
31 lines (24 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
const crypto = require('crypto');
const KEYS = require('./lib/redisKeys');
const TOKEN_PATTERN = /^[a-f0-9]{64}$/i;
function createAuthToken() {
return crypto.randomBytes(32).toString('hex');
}
async function validateAuthToken(redisClient, token) {
if (typeof token !== 'string') {
return null;
}
const normalizedToken = token.trim();
if (!normalizedToken || !TOKEN_PATTERN.test(normalizedToken)) {
return null;
}
try {
const clientId = await redisClient.get(KEYS.token(normalizedToken));
return clientId || null;
} catch (err) {
console.error('validateAuthToken error', err);
return null;
}
}
module.exports = { createAuthToken, validateAuthToken };