npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yubikey-chalresp

v0.3.0

Published

YubiKey OTP implementation for node.js

Downloads

13

Readme

YubiKey challenge-response for node.js

This is an implementation of YubiKey challenge-response OTP for node.js

Features

  • fast native implementation using yubico-c and ykpers
  • non-blocking API, I/O is performed in a separate thread
  • thread-safe library, locking is done inside
  • no additional JavaScript, all you need is the .node file
  • no runtime dependencies
  • possibility to cancel pending challenge-response

Installation

If you're on Linux, install libusb-1.0 dependency first:

apt-get install libusb-1.0-0-dev

Install the package from npm:

npm i yubikey-chalresp

Usage

const ykchalresp = require('yubikey-chalresp');

// Get a list of connected YubiKeys
ykchalresp.getYubiKeys((err, yubiKeys) => {
    // Select one of them and pass it to the challenge-response function
    const yubiKey = yubiKeys[0];

    const challenge = Buffer.from('aa', 'hex');
    const slot = 2;

    ykchalresp.challengeResponse(yubiKey, challenge, slot, (err, response) => {
        if (err) {
            if (err.touchRequested) {
                // Present the touch UI or show a message in the terminal
                // NOTE: the same callback will be called once again after it
                console.log('Please touch your YubiKey');
            } else {
                // There was some error
            }
        } else {
            // Response calculated successfully
            console.log('Response', response.toString('hex'));
        }
    });
});

Reference

version

Returns the version of the library

> ykchalresp.version
'0.0.1'

getYubiKeys

Gets the list of connected YubiKeys

Arguments:

  • options: object with possible parameters:
    • getCapabilities: read capabilities from YubiKeys
    • testSlots: perform a test challenge instead of relying on the config
  • callback: callback that will be called once the list is retrieved
const options = { getCapabilities: true, testSlots: true };
> ykchalresp.getYubiKeys(options, (err, yubiKeys) => console.log(yubiKeys))
[
  {
    serial: 12345678,
    vid: 4176,
    pid: 1031,
    version: '5.2.4',
    slots: [
      { number: 1, valid: true, touch: true },
      { number: 2, valid: true, touch: false }
    ]
    capabilities: <Buffer ...>
  }
]

capabilities are not always there, some YubiKeys don't provide them.

challengeResponse

Calculates the challenge-response, assuming it's programmed into the given slot

Arguments:

  • yubiKey: object representing the YubiKey with these properties:
    • vid
    • pid
    • serial
  • challenge: node.js Buffer with the desired challenge
  • slot: one of two slots: 1 or 2
  • callback: result callback that will be called:
    • in case of error, with (error)
    • in case of success, with (undefined, Buffer)
    • when a touch gesture is requested, with ({ touchRequested: true }). In this case the same callback will be called once again in the end!
> ykchalresp.challengeResponse({ vid: 4176, pid: 1031, serial: 12345678 },
    Buffer.from('aa', 'hex'), 2, (err, result) => console.log(err, result))
[Error: Touch requested] { touchRequested: true } undefined
undefined <Buffer ...>

cancelChallengeResponse

Cancels pending challenge-response operation and makes the YubiKey stop blinking. Since we're always taking an exclusive lock, there can be only one challenge-response in progress, so there are no extra parameters here.

The function always succeeds and doesn't reutrn any value, the result is handled in async way. Please note that due to race conditions you may still get a successful response if a YubiKey was pressed at the same moment. If the operation was successfully canceled, you will get an error with YK_ETIMEOUT error code.

Error codes

If an error was triggered by the YubiKey or Yubico API, it will contain the error code reported by the library. The full list of error codes can be found here. Errors can be checked like this:

ykchalresp.challengeResponse(yk, challenge, slot, (err, result) => {
    if (err && err.code === ykchalresp.YK_ENOKEY) {
        console.log('Please insert your YubiKey');
    }
});

Development

Clone the repo with submodules:

git clone [email protected]:antelle/node-yubikey-chalresp.git --recursive

Build the addon:

npm run rebuild

Insert your YubiKey and check if it's working:

npm run test

Iterative one-liner, convenient when you're testing something:

npm run build-test

License

MIT