Мой сервер nodejs получает запросы от клиентов. Этот запрос либо включает параметр запроса cpuId
, в этом случае клиент идентифицирует себя, либо не предоставляет такой параметр (который предполагает, что он является новичком):
let parseRequestData = async req => { /* ... */ };
let lookupUserByCpuId = cpuId => { /* ... */ };
let initNewUserWithRandomCpuId = () => { /* ... */ };
let removeExistingUser = user => { /* ... */ };
let respondToUserRequest = (user, path, query, body) => { /* ... */ };
let server = require('http').createServer(async (req, res) => {
// `path` could look like "/people"
// `query` could look like { cpuId: '183270232873', filter: 'age<=30', sort: 'ascending:firstName' }
// `body` is a json body in case a body is relevant to the request
let { path, query, body } = await parseRequestData(req);
let user = null;
let claimsToHaveUser = query.hasOwnProperty('cpuId');
if (claimsToHaveUser) {
user = lookupUserByCpuId(query.cpuId);
if (!user) { res.writeHead(400); res.end('Invalid cpuId'); return; }
} else {
user = initNewUserWithRandomCpuId();
user.confirmationTimeout = setTimeout(() => {
removeExistingUser(user);
}, 60 * 60 * 1000);
// When the user confirms email, cell #, etc, we'll clear this timeout
}
// If we get here, `user` is instantiated!
try {
let jsonResponse = respondToUserRequest(user, path, query, body);
} catch(err) {
res.writeHead(500); res.end('An error occurred'); return;
}
if (claimsToHaveUser) jsonResponse.cpuId = user.cpuId;
res.writeHead(200);
res.end(JSON.stringify(jsonResponse));
});
Клиенты могут сделать начальный запросзапрос без значения cpuId
. Ответ на это всегда будет включать только что созданный cpuId
. Затем клиент может включить это значение cpuId
в каждый запрос, идущий вперед - это позволит клиенту привязать несколько запросов к одной и той же учетной записи на сервере.
Используемые значения cpuId
остаются конфиденциальными иникогда не отправляется сервером, за исключением самого первого запроса, который инициализирует cpuId
.
Это безопасная модель аутентификации? Какие здесь проблемы безопасности?