Install keychain
masterInstall the keychain module via npm to enable basic macOS Keychain access in your Node.js applications.
npm install keychainrepository·master·Indexed 40 days ago
https://github.com/drudge/node-keychainA Node.js module providing basic access to the macOS Keychain by wrapping the system's security command-line tool. It supports storing, retrieving, and deleting generic and internet passwords, as well as managing keychains via createKeychain, deleteKeychain, and setDefaultKeychain.
Install the keychain module via npm to enable basic macOS Keychain access in your Node.js applications.
npm install keychainUse setPassword and getPassword with the default type: 'generic' to manage standard service credentials. Both methods require an account and a service name.
var keychain = require('keychain');
keychain.setPassword({ account: 'foo', service: 'FooBar', password: 'baz' }, function(err) {
keychain.getPassword({ account: 'foo', service: 'FooBar' }, function(err, pass) {
console.log('Password is', pass);
// Prints: Password is baz
});
});To manage internet-specific credentials (e.g., for websites), set the type option to 'internet'. This requires an account, service (the domain), and password.
var keychain = require('keychain');
keychain.setPassword({ account: 'foo', service: 'FooBar.com', type: 'internet', password: 'baz' }, function(err) {
keychain.getPassword({ account: 'foo', service: 'FooBar.com', type: 'internet' }, function(err, pass) {
console.log('Password is', pass);
// Prints: Password is baz
});
});The keychain module provides the following asynchronous methods. All methods accept an options object and an optional callback function.
keychain.setPassword(options[, callback]);
keychain.getPassword(options, callback);
keychain.deletePassword(options[, callback]);
keychain.createKeychain(options[, callback]);
keychain.deleteKeychain(options[, callback]);
keychain.setDefaultKeychain(options[, callback]);The following parameters can be passed within the options object to the keychain methods:
| Param | Description |
| ----- | ----------- |
| `account` | Specify account name (required) |
| `service` | Specify service name (required) |
| `password` | Specify password to be added (required for `setPassword`) |
| `type` | The type of password to get or save. Supported values are `generic` and `internet`. See [docs](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/security.1.html). Default: `generic` |
| `keychainName` | Specify the name of the keychain (required for 'createKeychain' and 'deleteKeychain') |Use deletePassword() to remove a specific password entry from the macOS keychain.
Parameters:
opts.account (String): The account name.opts.service (String): The service name.opts.type (String, optional): The password type. Defaults to 'generic'.Callback Signature: fn(err)
const keychain = require('./keychain');
keychain.deletePassword({ account: 'myUser', service: 'myService' }, (err) => {
if (err) {
console.error('Error deleting password:', err.message);
return;
}
console.log('Password deleted');
});These methods allow for high-level keychain management on macOS.
createKeychain(opts, fn)
opts.keychainName (String): The name of the new keychain.opts.password (String): The password for the new keychain.fn(err, keychainName)deleteKeychain(opts, fn)
opts.keychainName (String): The name of the keychain to delete.fn(err, keychainName)setDefaultKeychain(opts, fn)
opts.keychainName (String): The name of the keychain to set as default.fn(err, keychainName)const keychain = require('./keychain');
// Create
keychain.createKeychain({ keychainName: 'MyKey', password: 'keyPassword' }, (err, name) => {
// ...
});
// Set Default
keychain.setDefaultKeychain({ keychainName: 'MyKey' }, (err, name) => {
// ...
});
// Delete
keychain.deleteKeychain({ keychainName: 'MyKey' }, (err, name) => {
// ...
});Use setPassword() to store a new password or update an existing one in the macOS keychain.
Parameters:
opts.account (String): The account name.opts.service (String): The service name.opts.password (String): The password to store.opts.type (String, optional): The password type. Defaults to 'generic'.Callback Signature: fn(err, password)
const keychain = require('./keychain');
keychain.setPassword({
account: 'myUser',
service: 'myService',
password: 'secretPassword'
}, (err, password) => {
if (err) {
console.error('Error setting password:', err.message);
return;
}
console.log('Password set successfully');
});Use getPassword() to fetch a stored password from the macOS keychain. You must provide an account and a service. You can optionally specify the type of password (e.g., 'generic' or 'internet').
Parameters:
opts.account (String): The account name.opts.service (String): The service name.opts.type (String, optional): The password type. Defaults to 'generic'.Callback Signature: fn(err, password)
const keychain = require('./keychain');
keychain.getPassword({ account: 'myUser', service: 'myService' }, (err, password) => {
if (err) {
console.error('Error retrieving password:', err.message);
return;
}
console.log('Retrieved password:', password);
});The library provides specific error classes to help identify why a keychain operation failed. All errors follow the standard Node.js error pattern (err, result) in callbacks.
Available Error Types:
UnsupportedPlatformError: The current platform is not darwin (macOS).NoAccountProvidedError: The account option was missing.NoServiceProvidedError: The service option was missing.NoPasswordProvidedError: The password option was missing.NoKeychainNameProvidedError: The keychainName option was missing.PasswordNotFoundError: The requested password could not be found.ServiceFailureError: The underlying security command failed to execute or returned a non-zero exit code.