javascript - Dove memorizzare il JWT Client Credentials Grant
Ho un'applicazione NodeJS Express che si autentica su un server Auth tramite il grant di credenziali client. Il token che ricevo viene utilizzato per caricare dati da un'API.
Qual è la migliore pratica per memorizzare il token in tutta l'applicazione?
Nota che il JWT non è specifico per l'utente, poiché la mia App Express è il Client.
Risposta n. 1
Lo conserverò in memoria. Normalmente scriverò un modulo singleton per gestirlo.
auth.js:
class Auth {
getToken() {
// check if we has token already and that token isn't expired
if (this.token && !isExpired(this.token)) {
return Promise.resolve(this.token);
}
// if not we call API for the new token then return the new token
return asyncCallApiForToken();
}
}
module.exports = new Auth();
main.js
const auth = require('./auth.js)
auth.getToken()
.then(token => {
// we got token here
}
Risposta n. 2
Cercherei di evitare di memorizzare permanentemente il token restituito e conservarlo solo in memoria, poiché il grant di credenziali client consente di ottenere un nuovo token relativamente facilmente e senza interazione dell'utente.
Ma se questo è problematico, allora direi: Accanto alle credenziali del client, dato che le credenziali del client sono almeno altrettanto sensibili quanto il token JWT.
