Introduction to Keytar
Keytar is a native Node.js module that allows you to securely store and retrieve credentials, such as passwords and tokens, using the operating system’s credential storage facilities. This helps in managing sensitive information safely, without hardcoding them in your applications.
Get Started with Keytar
First, you need to install Keytar in your Node.js project:
npm install keytar
Keytar API Examples
Explore the various Keytar API methods with example snippets:
1. Storing a Password
The setPassword
method stores a password in the credential store:
const keytar = require('keytar');
async function storePassword(service, account, password) {
await keytar.setPassword(service, account, password);
console.log(`Password stored for ${account}@${service}`);
}
storePassword('my-service', 'my-account', 'my-secret-password');
2. Retrieving a Password
The getPassword
method retrieves a stored password:
async function retrievePassword(service, account) {
const password = await keytar.getPassword(service, account);
console.log(`Retrieved password for ${account}@${service}: ${password}`);
}
retrievePassword('my-service', 'my-account');
3. Deleting a Password
The deletePassword
method removes a stored password:
async function deletePassword(service, account) {
const success = await keytar.deletePassword(service, account);
console.log(`Password deleted for ${account}@${service}: ${success}`);
}
deletePassword('my-service', 'my-account');
4. Finding Credentials
The findCredentials
method returns all stored credentials for a given service:
async function findAllCredentials(service) {
const credentials = await keytar.findCredentials(service);
credentials.forEach(cred => {
console.log(`Found credential - Account: ${cred.account}, Password: ${cred.password}`);
});
}
findAllCredentials('my-service');
5. Finding Password
The findPassword
method returns the password for the first found account based on the given service:
async function findPassword(service) {
const password = await keytar.findPassword(service);
console.log(`Found password for ${service}: ${password}`);
}
findPassword('my-service');
Sample App Using Keytar
Here is a simple example where all the above APIs are integrated into a Node.js application:
const keytar = require('keytar'); const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function main() {
rl.question('Enter service name: ', async (service) => {
rl.question('Enter account name: ', async (account) => {
rl.question('Enter password: ', async (password) => {
await keytar.setPassword(service, account, password);
console.log('Password stored successfully.');
const storedPassword = await keytar.getPassword(service, account);
console.log(`Retrieved password: ${storedPassword}`);
await keytar.deletePassword(service, account);
console.log('Password deleted successfully.');
rl.close();
});
});
});
}
main();
By using Keytar, you ensure that sensitive credentials are managed securely and easily within your Node.js applications.
Hash: ca7f357f842c49aeae75282d1117181496704e4b2009d69b1a6faf4f6078acca