
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
hathor-cookie-auth
Advanced tools
npm install --save hathor-cookie-auth
auth: {
static: true, // it's better to not protect your static assets by default
key: String, // Key/password for authentication
// What module to utilize for authentication
module: `hathor-cookie-auth`,
// Value in milliseconds
// for 1 year: 365 * 24 * 60 * 60 * 1000
ttl: 1000 * 60 * 3,
// If you need to allow access to a specific asset because you set static to false
whitelist: [ // Always allow access to the login folder
'login'
],
blacklist: [ // If you have files that you don't want access to without auth, then blacklist them
'login/private.html'
],
// One option to provide username's and passwords
users: [
{
username: 'test',
password: 'person' // This value can be bcrypt'd so plain text isn't ever shown
}
],
// The "more appropriate" way
userHandler(username, password, callback){},
plugin: {} // Any values you want to override or push down into the hapi-auth-cookie module
}
By providing a validateFunc you can setup your own token validation that can be used for things like expiring tokens when a certain criteria are met, users are edited/removed from the application and other common things that may change the state associated with the token.
The callback takes three parameters; error, isValid, and (optional) credentials. A simple example below:
const validateFunc = (decoded, request, callback){
myUserProvider.byId(decoded.id, (err, user)=>{
if(err){
return callback(err);
}
return callback(null, !!user);
});
}
If you wanted to change the credentials associated with a token (overwrite the decoded value), as an example to change the expires time, you would return a new credentials object.
The users array is provided for development mode testing, it should never be used in a live environment. Adding, editing, deleting users requires that you restart the application.
If you setup and configure the userHandler method within the configuration it will take precedence over the users array. This means that none of the user accounts specified within the users array will work if there is a userHandler defined. If, for example you want to utilize the users array for local development and userHandler when running within a specified environment, it is recommended that you setup the userHandler in code when you load your configuration. An example of this is given below.
The callback takes three parameters; error, isValid, and credentials
const userHandler = (username, password, callback){
myUserProvider.get(username, password, (err, user)=>{
if(err){
return callback(err);
}
return callback(null, !!user, user?{id: user.id}:false);
});
}
Whatever you return in the credentials object will be encoded and sent back in the token. If you need to change the encoded values later on you should use the validateFunc property to provide the new data.
Once your application has a JSON Web Token it can pass this back to the server in either the Authorization header or in the query parameters of each call in the token parameter.
Returns the login index page from your application.
Accepts username and password, returns a set cookie response that will be passed back in all subsequent requests to the application.
Clears the token and "logs" the user out. Unless this case is handled by your application code the token could still be valid.
FAQs
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.