sails.js
Autenticazione del token Web JSON con Sails
Ricerca…
Configurazione
Primo passo
Dobbiamo creare un servizio chiamato jwToken . Vai alla directory api/services
e crea jwToken.js
.
'use strict';
const jwt = require('jsonwebtoken'),
tokenSecret = "secretissecret";
module.exports = {
// Generates a token from supplied payload
issue(payload) {
return jwt.sign(
payload,
tokenSecret, // Token Secret that we sign it with
{
expiresIn: "30 days" // Token Expire time
});
},
// Verifies token on a request
verify(token, callback) {
return jwt.verify(
token, // The token to be verified
tokenSecret, // Same token we used to sign
{}, // No Option, for more see https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback
callback //Pass errors or decoded token to callback
);
}
};
Passo due
Cripta la nostra password usando bcrypt
. Vai a api/models/User.js
'use strict';
const bcrypt = require('bcrypt');
module.exports = {
attributes: {
// your code...
},
// Here we encrypt password before creating a User
beforeCreate(values, next) {
bcrypt.genSalt(10, (err, salt) => {
if (err) {
sails.log.error(err);
return next();
}
bcrypt.hash(values.password, salt, (err, hash) => {
if (err) {
sails.log.error(err);
return next();
}
values.encryptedPassword = hash; // Here is our encrypted password
return next();
});
});
},
comparePassword(password, encryptedPassword) {
return new Promise(function(resolve, reject) {
bcrypt.compare(password, encryptedPassword, (err, match) => {
if (err) {
sails.log.error(err);
return reject("Something went wrong!");
}
if (match) return resolve();
else return reject("Mismatch passwords");
});
});
}
};
Fase tre
Crea criterio di autorizzazione per verificare se un utente ha un token valido nell'intestazione della richiesta. Vai a api/policies
e crea isAuthorized.js
.
'use strict';
module.exports = (req, res, next) => {
let token;
if (req.headers && req.headers.token) {
token = req.headers.token;
if (token.length <= 0) return res.json(401, {err: 'Format is Authorization: Bearer [token]'});
} else if (req.param('token')) {
token = req.param('token');
// We delete the token from param to not mess with blueprints
delete req.query.token;
} else {
return res.json(401, {err: 'No Authorization header was found'});
}
jwToken.verify(token, function (err, token) {
if (err) return res.json(401, {err: 'Invalid Token!'});
req.token = token; // This is the decrypted token or the payload you provided
next();
});
};
Fase quattro
Utilizziamo config / policies.js per proteggere i nostri controllori
module.exports.policies = {
'*': ['isAuthorized'], // Everything resctricted here
'UserController': { // Name of your controller
'create': true // We dont need authorization here, allowing public access
}
};
Passo cinque
Mettiamo alla prova la nostra implementazione. Vai a api/controllers
e crea UserController.js
'use strict';
module.exports = {
create(req, res) {
const data = req.body;
if (data.password !== data.confirmPassword) return res.badRequest("Password not the same");
User.create({
email: data.email,
password: data.password,
name: data.name
//etc...
})
.then((user) => {
res.send({ token: jwToken.issue({ id: user.id }) }); // payload is { id: user.id}
})
.catch((err) => {
sails.log.error(err);
return res.serverError("Something went wrong");
});
},
login(req, res) {
const data = req.body;
if (!data.email || !data.password) return res.badRequest('Email and password required');
User.findOne({ email: email })
.then((user) => {
if (!user) return res.notFound();
User.comparePassword(password, user.password)
.then(() => {
return res.send({ token: jwToken.issue({ id: user.id }) })
})
.catch((err) => {
return res.forbidden();
});
})
.catch((err) => {
sails.log.error(err);
return res.serverError();
});
}
};
Installazione
Abbiamo bisogno di due dipendenze:
- bcrypt per la crittografia
npm install bcrypt --save
- Token Web JSON
npm install jsonwebtoken --save
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow