Buffer
Digest the one-time passcode options.
String
Generate a counter-based one-time token.
Object
Verify a counter-based one-time token against the secret and return the delta.
Boolean
Verify a counter-based one-time token against the secret and return true if it verifies.
String
Generate a time-based one-time token.
Object
Verify a time-based one-time token against the secret and return the delta.
Boolean
Verify a time-based one-time token against the secret and return true if it verifies.
Object
| GeneratedSecret
Generates a random secret with the set A-Z a-z 0-9 and symbols, of any length (default 32).
String
Generates a key of a certain length (default 32) from A-Z, a-z, 0-9, and symbols (if requested).
String
Generate an URL for use with the Google Authenticator app.
Object
Buffer
Digest the one-time passcode options.
**Kind**: function
**Returns**: Buffer
- The one-time passcode as a buffer.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.counter | Integer
| | Counter value |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
| [options.key] | String
| | (DEPRECATED. Use `secret` instead.) Shared secret key |
### hotp(options) ⇒ String
Generate a counter-based one-time token. Specify the key and counter, and
receive the one-time password for that counter position as a string. You can
also specify a token length, as well as the encoding (ASCII, hexadecimal, or
base32) and the hashing algorithm to use (SHA1, SHA256, SHA512).
**Kind**: function
**Returns**: String
- The one-time passcode.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.counter | Integer
| | Counter value |
| [options.digest] | Buffer
| | Digest, automatically generated by default |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
| [options.key] | String
| | (DEPRECATED. Use `secret` instead.) Shared secret key |
| [options.length] | Integer
| 6
| (DEPRECATED. Use `digits` instead.) The number of digits for the one-time passcode. |
### hotp․verifyDelta(options) ⇒ Object
Verify a counter-based one-time token against the secret and return the delta.
By default, it verifies the token at the given counter value, with no leeway
(no look-ahead or look-behind). A token validated at the current counter value
will have a delta of 0.
You can specify a window to add more leeway to the verification process.
Setting the window param will check for the token at the given counter value
as well as `window` tokens ahead (one-sided window). See param for more info.
`verifyDelta()` will return the delta between the counter value of the token
and the given counter value. For example, if given a counter 5 and a window
10, `verifyDelta()` will look at tokens from 5 to 15, inclusive. If it finds
it at counter position 7, it will return `{ delta: 2 }`.
**Kind**: function
**Returns**: Object
- On success, returns an object with the counter
difference between the client and the server as the `delta` property (i.e.
`{ delta: 0 }`).
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.token | String
| | Passcode to validate |
| options.counter | Integer
| | Counter value. This should be stored by the application and must be incremented for each request. |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. |
| [options.window] | Integer
| 0
| The allowable margin for the counter. The function will check "W" codes in the future against the provided passcode, e.g. if W = 10, and C = 5, this function will check the passcode against all One Time Passcodes between 5 and 15, inclusive. |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
### hotp․verify(options) ⇒ Boolean
Verify a counter-based one-time token against the secret and return true if it
verifies. Helper function for `hotp.verifyDelta()`` that returns a boolean
instead of an object. For more on how to use a window with this, see
hotp.verifyDelta.
**Kind**: function
**Returns**: Boolean
- Returns true if the token matches within the given
window, false otherwise.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.token | String
| | Passcode to validate |
| options.counter | Integer
| | Counter value. This should be stored by the application and must be incremented for each request. |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. |
| [options.window] | Integer
| 0
| The allowable margin for the counter. The function will check "W" codes in the future against the provided passcode, e.g. if W = 10, and C = 5, this function will check the passcode against all One Time Passcodes between 5 and 15, inclusive. |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
### totp(options) ⇒ String
Generate a time-based one-time token. Specify the key, and receive the
one-time password for that time as a string. By default, it uses the current
time and a time step of 30 seconds, so there is a new token every 30 seconds.
You may override the time step and epoch for custom timing. You can also
specify a token length, as well as the encoding (ASCII, hexadecimal, or
base32) and the hashing algorithm to use (SHA1, SHA256, SHA512).
Under the hood, TOTP calculates the counter value by finding how many time
steps have passed since the epoch, and calls HOTP with that counter value.
**Kind**: function
**Returns**: String
- The one-time passcode.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| [options.time] | Integer
| | Time in seconds with which to calculate counter value. Defaults to `Date.now()`. |
| [options.step] | Integer
| 30
| Time step in seconds |
| [options.epoch] | Integer
| 0
| Initial time since the UNIX epoch from which to calculate the counter value. Defaults to 0 (no offset). |
| [options.counter] | Integer
| | Counter value, calculated by default. |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
| [options.key] | String
| | (DEPRECATED. Use `secret` instead.) Shared secret key |
| [options.initial_time] | Integer
| 0
| (DEPRECATED. Use `epoch` instead.) Initial time since the UNIX epoch from which to calculate the counter value. Defaults to 0 (no offset). |
| [options.length] | Integer
| 6
| (DEPRECATED. Use `digits` instead.) The number of digits for the one-time passcode. |
### totp․verifyDelta(options) ⇒ Object
Verify a time-based one-time token against the secret and return the delta.
By default, it verifies the token at the current time window, with no leeway
(no look-ahead or look-behind). A token validated at the current time window
will have a delta of 0.
You can specify a window to add more leeway to the verification process.
Setting the window param will check for the token at the given counter value
as well as `window` tokens ahead and `window` tokens behind (two-sided
window). See param for more info.
`verifyDelta()` will return the delta between the counter value of the token
and the given counter value. For example, if given a time at counter 1000 and
a window of 5, `verifyDelta()` will look at tokens from 995 to 1005,
inclusive. In other words, if the time-step is 30 seconds, it will look at
tokens from 2.5 minutes ago to 2.5 minutes in the future, inclusive.
If it finds it at counter position 1002, it will return `{ delta: 2 }`.
If it finds it at counter position 997, it will return `{ delta: -3 }`.
**Kind**: function
**Returns**: Object
- On success, returns an object with the time step
difference between the client and the server as the `delta` property (e.g.
`{ delta: 0 }`).
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.token | String
| | Passcode to validate |
| [options.time] | Integer
| | Time in seconds with which to calculate counter value. Defaults to `Date.now()`. |
| [options.step] | Integer
| 30
| Time step in seconds |
| [options.epoch] | Integer
| 0
| Initial time since the UNIX epoch from which to calculate the counter value. Defaults to 0 (no offset). |
| [options.counter] | Integer
| | Counter value, calculated by default. |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. |
| [options.window] | Integer
| 0
| The allowable margin for the counter. The function will check "W" codes in the future and the past against the provided passcode, e.g. if W = 5, and C = 1000, this function will check the passcode against all One Time Passcodes between 995 and 1005, inclusive. |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
### totp․verify(options) ⇒ Boolean
Verify a time-based one-time token against the secret and return true if it
verifies. Helper function for verifyDelta() that returns a boolean instead of
an object. For more on how to use a window with this, see totp.verifyDelta.
**Kind**: function
**Returns**: Boolean
- Returns true if the token matches within the given
window, false otherwise.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.token | String
| | Passcode to validate |
| [options.time] | Integer
| | Time in seconds with which to calculate counter value. Defaults to `Date.now()`. |
| [options.step] | Integer
| 30
| Time step in seconds |
| [options.epoch] | Integer
| 0
| Initial time since the UNIX epoch from which to calculate the counter value. Defaults to 0 (no offset). |
| [options.counter] | Integer
| | Counter value, calculated by default. |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. |
| [options.window] | Integer
| 0
| The allowable margin for the counter. The function will check "W" codes in the future and the past against the provided passcode, e.g. if W = 5, and C = 1000, this function will check the passcode against all One Time Passcodes between 995 and 1005, inclusive. |
| [options.encoding] | String
| "ascii"
| Key encoding (ascii, hex, base32, base64). |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
### generateSecret(options) ⇒ Object
| [GeneratedSecret](#GeneratedSecret)
Generates a random secret with the set A-Z a-z 0-9 and symbols, of any length
(default 32). Returns the secret key in ASCII, hexadecimal, and base32 format,
along with the URL used for the QR code for Google Authenticator (an otpauth
URL). Use a QR code library to generate a QR code based on the Google
Authenticator URL to obtain a QR code you can scan into the app.
**Kind**: function
**Returns**: A [`GeneratedSecret`](#GeneratedSecret) object
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| [options.length] | Integer
| 32
| Length of the secret |
| [options.symbols] | Boolean
| false
| Whether to include symbols |
| [options.otpauth_url] | Boolean
| true
| Whether to output a Google Authenticator-compatible otpauth:// URL (only returns otpauth:// URL, no QR code) |
| [options.name] | String
| | The name to use with Google Authenticator. |
| [options.qr_codes] | Boolean
| false
| (DEPRECATED. Do not use to prevent leaking of secret to a third party. Use your own QR code implementation.) Output QR code URLs for the token. |
| [options.google_auth_qr] | Boolean
| false
| (DEPRECATED. Do not use to prevent leaking of secret to a third party. Use your own QR code implementation.) Output a Google Authenticator otpauth:// QR code URL. |
### generateSecretASCII([length], [symbols]) ⇒ String
Generates a key of a certain length (default 32) from A-Z, a-z, 0-9, and
symbols (if requested).
**Kind**: function
**Returns**: String
- The generated key.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [length] | Integer
| 32
| The length of the key. |
| [symbols] | Boolean
| false
| Whether to include symbols in the key. |
### otpauthURL(options) ⇒ String
Generate a Google Authenticator-compatible otpauth:// URL for passing the
secret to a mobile device to install the secret.
Authenticator considers TOTP codes valid for 30 seconds. Additionally,
the app presents 6 digits codes to the user. According to the
documentation, the period and number of digits are currently ignored by
the app.
To generate a suitable QR Code, pass the generated URL to a QR Code
generator, such as the `qr-image` module.
**Kind**: function
**Throws**: Error if secret or label is missing, or if hotp is used and a
counter is missing, if the type is not one of `hotp` or `totp`, if the
number of digits is non-numeric, or an invalid period is used. Warns if
the number of digits is not either 6 or 8 (though 6 is the only one
supported by Google Authenticator), and if the hashihng algorithm is
not one of the supported SHA1, SHA256, or SHA512.
**Returns**: String
- A URL suitable for use with the Google Authenticator.
**See**: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | Object
| | |
| options.secret | String
| | Shared secret key |
| options.label | String
| | Used to identify the account with which the secret key is associated, e.g. the user's email address. |
| [options.type] | String
| "totp"
| Either "hotp" or "totp". |
| [options.counter] | Integer
| | The initial counter value, required for HOTP. |
| [options.issuer] | String
| | The provider or service with which the secret key is associated. |
| [options.algorithm] | String
| "sha1"
| Hash algorithm (sha1, sha256, sha512). |
| [options.digits] | Integer
| 6
| The number of digits for the one-time passcode. Currently ignored by Google Authenticator. |
| [options.period] | Integer
| 30
| The length of time for which a TOTP code will be valid, in seconds. Currently ignored by Google Authenticator. |
| [options.encoding] | String
| | Key encoding (ascii, hex, base32, base64). If the key is not encoded in Base-32, it will be reencoded. |
### GeneratedSecret : Object
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| ascii | String
| ASCII representation of the secret |
| hex | String
| Hex representation of the secret |
| base32 | String
| Base32 representation of the secret |
| qr_code_ascii | String
| URL for the QR code for the ASCII secret. |
| qr_code_hex | String
| URL for the QR code for the hex secret. |
| qr_code_base32 | String
| URL for the QR code for the base32 secret. |
| google_auth_qr | String
| URL for the Google Authenticator otpauth URL's QR code. |
| otpauth_url | String
| Google Authenticator-compatible otpauth URL. |
## Contributing
We're very happy to have your contributions in Speakeasy.
**Contributing code** — First, make sure you've added tests if adding new functionality. Then, run `npm test` to run all the tests to make sure they pass. Next, make a pull request to this repo. Thanks!
**Filing an issue** — Submit issues to the [GitHub Issues][issues] page.
**Maintainers** —
- Mark Bao ([markbao][markbao])
- Michael Phan-Ba ([mikepb][mikepb])
## License
This project incorporates code from [passcode][], which was originally a
fork of speakeasy, and [notp][], both of which are licensed under MIT.
Please see the [LICENSE](LICENSE) file for the full combined license.
Icons created by Gregor Črešnar, iconoci, and Danny Sturgess from the Noun
Project.
[issues]: https://github.com/speakeasyjs/speakeasy
[passcode]: http://github.com/mikepb/passcode
[notp]: https://github.com/guyht/notp
[oath]: http://www.openauthentication.org/
[rfc4226]: https://tools.ietf.org/html/rfc4226
[rfc6238]: https://tools.ietf.org/html/rfc6238
[markbao]: https://github.com/markbao
[mikepb]: https://github.com/mikepb