git-credential-node
Thin wrapper around
git credential
command Allow you to use Git credential API from node.
Installation
npm install --save git-credential-node
How it works
This is a thin wrapper around the git credential
command.
The library use the GIT_TERMINAL_PROMPT environment variable to avoid asking the user credential on stdin.
The option is avalable starting with git 2.3, so you must have this version of git intalled on your system (or a newer one).
Usage
The library provides the following functions that can be used to interact with a locally installed Git credential tool.
All functions with names ending in sync
are synchronous, and either return the result of operation or throws.
All other functions are asynchronous and could be used in one of two ways:
- a callback could be provided as last argument which is called with the error or the results of the operation as arguments, following the standard node semantic.
- a promise is returned that is either resolved with results, or rejected with errors.
fill(url, callback)
Retrieves any stored credentials for the provided target server.
url
: AString
parameter indicating the URL of the target server, e.g. https://github.comcallback
: Called when the call to the Git credential tool finishes.
The provided callback
is called with two parameters:
err
: An error object in case the call failed.result
: { username, password }, containing the stored credentials. If there aren't any stored credentials for the requested target server, result will be null.
When there aren't any stored credentials for the requested target server, the Git credential helper will not ask for credentials, it will simply provide an empty result object to the callback.
Example:
;;
approve(options, callback)
Stores the provided credentials for the provided target server. The following parameters are expected:
options
: { username, password, url } An object containingusername
andpassword
properties with the credentials to be stored, andurl
indicating the URL of the target server.callback
: Called when the call to the Git credential tool finishes.
The provided callback
is called with one parameters:
err
: An error object in case the call failed. If it is null, it means the call has succedeed.
Example:
;const opts = username: 'user' password: 'pass' url: 'http://foo/bar.git'; ;
reject(url, callback)
Removes any stored credentials for the provided target server. The following parameters are expected:
url
: AString
parameter indicating the URL of the target server, e.g. https://github.comcallback
: Called when the call to the Git credential tool finishes.
The provided callback
is called with one parameters:
err
: An error object in case the call failed. If it is null, it means the call has succedeed.
Example:
;;
Promises usage
Example of use of the async functions with promise.
All functions resolve or reject the returned promise with the same arguments passed to the callback. We use es2016 proposed async function in these example for clarity, but this is obviously not required. Any other comment specified in the callback section apply also to the promise usage.
fill(url)
Example:
; { try const data = await ; if !data console; return data; catch err console; }
approve(options)
Example:
; { const opts = username: 'user' password: 'pass' url: 'http://foo/bar.git' ; try await ; catch err return console; console;});
reject(url, callback)
Example:
; { try await ; catch err return console; console;});
Sync usage
Example of use of the sync functions.
All functions hasve a synchronous counterpart, with name ending in Sync
.
These can be particulary useful to use within bin scripts.
Any other comment specified in the callback section apply also to the sync functions.
fillSync(url)
Example:
; { try const data = ; if !data console; return data; catch err console; }
Sync(options)
Example:
; { const opts = username: 'user' password: 'pass' url: 'http://foo/bar.git' ; try ; catch err return console; console;});
rejectSync(url, callback)
Example:
; { try ; catch err return console; console;});
Credits
I took inspiration and documentation from git-credential-helper by nwinkler
License
MIT - © 2015 Andrea Parodi