hwcrypto / hwcrypto.js

Browser JavaScript library for working with hardware tokens
https://hwcrypto.github.io
MIT License
157 stars 47 forks source link

Added hasBackend() method to check if functioning backend is available, returns backend info on success #7

Open martinrebane opened 9 years ago

martinpaljak commented 9 years ago

You can use debug() for the same purpose, but keep in mind that the presence of a backend implementation does not mean that there is a possibility to get a signature: for example, a plugin with PKCS#11 implementation (Linux, OSX) can fail if there is no PKCS#11 component installed and would currently fail while asking for a certificate. You can check if debug() does not contain "No implementation" as a workaround, I don't think that the API needs to grow a method of checking for implementation.

martinpaljak commented 9 years ago

Would looking for a needle in the debug() string be sufficient for your use case?

martinrebane commented 9 years ago

Yes, checking for the needle would work, this was my first idea:

hwcrypto.debug().then(
            function (result) {
                        if (result.indexOf("No implementation") > -1) {
                                   console.log("No real backend..");
                        }
            },
            function (err) {
                        console.log(err);
            }
);

But this code looks like (and is) an ugly hack because hwcrypto provides backend of type NoBackend if there is no real backend. I would have to search for error condition in the happy path (success scenario).

Other thing is that if hwcrypto debug() method should change some day, this code will break and whoever debugs it, will loose lots of hair as it would fail silently. This behavior is not reflected in the API.

The reason why we would like to see such probing function is customer service. If we can decently detect whether a real backend is available or not, then half of the problems could be solved by clients themselves (we can give them exact error message and HOWTO) and 95% of signing related client support requests could be solved by customer support. Cases where they have browser backend, but PKCS#11 component is not installed, would be automatically separated from cases where there is no backend.

Technically yes, it would not make much sense to add such method, but from user's perspective exact and timely error message is as important as signing itself.

martinpaljak commented 9 years ago

Okay. "Having timely and exact error messages" is a requirement that makes sense.

From technical perspective: debug() is supposed to always resolve to a string. What about adding a "documented / recommended way of detecting something"? A "check" method for the publicly stable API is not what I would like to add, it could be an "internal method" (like debug()) but then again, it could be a documented way of using an existing API to get the same result. Adding a "check() -> boolean" internal method that does the same could also be a possibility, to save integrators from typing.

martinrebane commented 9 years ago

Yes, documented internal method would be fine. Code would also be much cleaner than just using string search.