Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional SCard Scope #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ pcsc.on('reader', (reader) => {

});

}
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {

console.log("card inserted");

Expand Down
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"sources": [
"src/addon.cpp",
"src/pcsclite.cpp",
"src/cardreader.cpp"
"src/cardreader.cpp",
"src/constants.cpp"
],
"cflags": [
"-Wall",
Expand Down
12 changes: 9 additions & 3 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"use strict";

const pcsclite = require('../lib/pcsclite');
const {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
} = require('../lib/constants');


const pcsc = pcsclite();
// const pcsc = pcsclite(); // without options (scope defaults to SCARD_SCOPE_SYSTEM)
const pcsc = pcsclite({ scope: SCARD_SCOPE_USER }); // overwriting default scope

pcsc.on('reader', (reader) => {

Expand Down Expand Up @@ -39,8 +46,7 @@ pcsc.on('reader', (reader) => {

});

}
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {

console.log("card inserted");

Expand Down
89 changes: 89 additions & 0 deletions examples/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"use strict";

const pcsclite = require('@pokusew/pcsclite');
const {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
} = require('@pokusew/pcsclite/lib/constants');


// const pcsc = pcsclite(); // without options (scope defaults to SCARD_SCOPE_SYSTEM)
const pcsc = pcsclite({ scope: SCARD_SCOPE_USER }); // overwriting default scope

pcsc.on('reader', (reader) => {

console.log('New reader detected', reader.name);

reader.on('error', err => {
console.log('Error(', reader.name, '):', err.message);
});

reader.on('status', (status) => {

console.log('Status(', reader.name, '):', status);

// check what has changed
const changes = reader.state ^ status.state;

if (!changes) {
return;
}

if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {

console.log("card removed");

reader.disconnect(reader.SCARD_LEAVE_CARD, err => {

if (err) {
console.log(err);
return;
}

console.log('Disconnected');

});

} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {

console.log("card inserted");

reader.connect({ share_mode: reader.SCARD_SHARE_SHARED }, (err, protocol) => {

if (err) {
console.log(err);
return;
}

console.log('Protocol(', reader.name, '):', protocol);

reader.transmit(Buffer.from([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, (err, data) => {

if (err) {
console.log(err);
return;
}

console.log('Data received', data);
reader.close();
pcsc.close();

});

});

}

});

reader.on('end', () => {
console.log('Reader', reader.name, 'removed');
});

});

pcsc.on('error', err => {
console.log('PCSC error', err.message);
});
15 changes: 15 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

const {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
} = require('../build/Release/pcsclite.node');

module.exports = {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
};
13 changes: 9 additions & 4 deletions lib/pcsclite.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const EventEmitter = require('events');
// see https://github.com/nodejs/node-gyp/issues/263, https://github.com/nodejs/node-gyp/issues/631
const pcsclite = require('../build/Release/pcsclite.node');

const { PCSCLite, CardReader } = pcsclite;
const { PCSCLite, CardReader, SCARD_SCOPE_SYSTEM } = pcsclite;


inherits(PCSCLite, EventEmitter);
Expand Down Expand Up @@ -45,11 +45,16 @@ function diff(a, b) {

}

module.exports = function () {
module.exports = function (options = {}) {

const readers = {};

const p = new PCSCLite();
// const scope = options.scope ?? SCARD_SCOPE_SYSTEM;
// ^ this syntax above (null coalescing operator) would need transpiler as it is not available in the Node.js yet
const scope = options.scope !== undefined && options.scope !== null ? options.scope : SCARD_SCOPE_SYSTEM;

// TODO: consider alternative approach > allow call with undefined and the C++ side will supply the default value
const p = new PCSCLite(scope);

p.readers = readers;

Expand All @@ -69,7 +74,7 @@ module.exports = function () {

newNames.forEach(function (name) {

const r = new CardReader(name);
const r = new CardReader(name, scope);

r.on('_end', function () {
r.removeAllListeners('status');
Expand Down
2 changes: 2 additions & 0 deletions src/addon.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "pcsclite.h"
#include "cardreader.h"
#include "constants.h"

void init_all(v8::Local<v8::Object> target) {
PCSCLite::init(target);
CardReader::init(target);
initConstants(target);
}

NODE_MODULE(pcsclite, init_all)
12 changes: 7 additions & 5 deletions src/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ void CardReader::init(Local<Object> target) {
Nan::Set(target, Nan::New("CardReader").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}

CardReader::CardReader(const std::string &reader_name): m_card_context(0),
CardReader::CardReader(const std::string &reader_name, DWORD scope): m_card_context(0),
m_card_handle(0),
m_name(reader_name),
m_state(0) {
m_state(0),
m_scope(scope) {
assert(uv_mutex_init(&m_mutex) == 0);
assert(uv_cond_init(&m_cond) == 0);
}
Expand All @@ -91,8 +92,9 @@ NAN_METHOD(CardReader::New) {
Nan::HandleScope scope;

Nan::Utf8String reader_name(Nan::To<String>(info[0]).ToLocalChecked());
DWORD pcsc_scope = Nan::To<uint32_t>(info[1]).FromJust();

CardReader* obj = new CardReader(*reader_name);
CardReader* obj = new CardReader(*reader_name, pcsc_scope);
obj->Wrap(info.Holder());
Nan::Set(obj->handle(), Nan::New(name_symbol), Nan::To<String>(info[0]).ToLocalChecked());
Nan::Set(obj->handle(), Nan::New(connected_symbol), Nan::False());
Expand Down Expand Up @@ -389,7 +391,7 @@ void CardReader::HandlerFunction(void* arg) {
async_baton->async_result = new AsyncResult();
async_baton->async_result->do_exit = false;

LONG result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &reader->m_status_card_context);
LONG result = SCardEstablishContext(reader->m_scope, NULL, NULL, &reader->m_status_card_context);

SCARD_READERSTATE card_reader_state = SCARD_READERSTATE();
card_reader_state.szReader = reader->m_name.c_str();
Expand Down Expand Up @@ -440,7 +442,7 @@ void CardReader::DoConnect(uv_work_t* req) {
uv_mutex_lock(&obj->m_mutex);
/* Is context established */
if (!obj->m_card_context) {
result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &obj->m_card_context);
result = SCardEstablishContext(obj->m_scope, NULL, NULL, &obj->m_card_context);
}

/* Connect */
Expand Down
3 changes: 2 additions & 1 deletion src/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CardReader: public Nan::ObjectWrap {

private:

CardReader(const std::string &reader_name);
CardReader(const std::string &reader_name, DWORD scope);

~CardReader();

Expand Down Expand Up @@ -132,6 +132,7 @@ class CardReader: public Nan::ObjectWrap {
uv_mutex_t m_mutex;
uv_cond_t m_cond;
int m_state;
DWORD m_scope;
static Nan::AsyncResource *async_resource;
};

Expand Down
30 changes: 30 additions & 0 deletions src/constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "constants.h"

#ifdef __APPLE__
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#else
#include <winscard.h>
#endif

void initConstants(v8::Local<v8::Object> target) {
// TERMINAL and GLOBAL are not defined on Windows as per
// https://docs.microsoft.com/windows/win32/api/winscard/nf-winscard-scardestablishcontext

Nan::Set(target, Nan::New("SCARD_SCOPE_USER").ToLocalChecked(), Nan::New(SCARD_SCOPE_USER));

#ifdef WIN32
Nan::Set(target, Nan::New("SCARD_SCOPE_TERMINAL").ToLocalChecked(), Nan::New(SCARD_SCOPE_USER));
#else
Nan::Set(target, Nan::New("SCARD_SCOPE_TERMINAL").ToLocalChecked(), Nan::New(SCARD_SCOPE_TERMINAL));
#endif /* WIN32 */

Nan::Set(target, Nan::New("SCARD_SCOPE_SYSTEM").ToLocalChecked(), Nan::New(SCARD_SCOPE_SYSTEM));

#ifdef WIN32
Nan::Set(target, Nan::New("SCARD_SCOPE_GLOBAL").ToLocalChecked(), Nan::New(SCARD_SCOPE_SYSTEM));
#else
Nan::Set(target, Nan::New("SCARD_SCOPE_GLOBAL").ToLocalChecked(), Nan::New(SCARD_SCOPE_GLOBAL));
#endif /* WIN32 */

}
7 changes: 7 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H
#include <nan.h>

void initConstants(v8::Local<v8::Object> target);

#endif /* CONSTANTS_H */
16 changes: 10 additions & 6 deletions src/pcsclite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ void PCSCLite::init(Local<Object> target) {
Nan::Set(target, Nan::New("PCSCLite").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}

PCSCLite::PCSCLite(): m_card_context(0),
PCSCLite::PCSCLite(DWORD scope): m_card_context(0),
m_card_reader_state(),
m_status_thread(0),
m_state(0) {
m_state(0),
m_scope(scope) {

assert(uv_mutex_init(&m_mutex) == 0);
assert(uv_cond_init(&m_cond) == 0);
Expand Down Expand Up @@ -70,8 +71,7 @@ PCSCLite::PCSCLite(): m_card_context(0),
LONG result;
// TODO: consider removing this do-while Windows workaround that should not be needed anymore
do {
// TODO: make dwScope (now hard-coded to SCARD_SCOPE_SYSTEM) customisable
result = SCardEstablishContext(SCARD_SCOPE_SYSTEM,
result = SCardEstablishContext(m_scope,
NULL,
NULL,
&m_card_context);
Expand Down Expand Up @@ -111,7 +111,11 @@ PCSCLite::~PCSCLite() {

NAN_METHOD(PCSCLite::New) {
Nan::HandleScope scope;
PCSCLite* obj = new PCSCLite();

DWORD pcsc_scope = Nan::To<uint32_t>(info[0]).FromJust();

PCSCLite* obj = new PCSCLite(pcsc_scope);

obj->Wrap(info.Holder());
info.GetReturnValue().Set(info.Holder());
}
Expand Down Expand Up @@ -340,7 +344,7 @@ LONG PCSCLite::get_card_readers(PCSCLite* pcsclite, AsyncResult* async_result) {
#endif
if (result == SCARD_E_NO_SERVICE || result == SCARD_E_SERVICE_STOPPED) {
SCardReleaseContext(pcsclite->m_card_context);
SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &pcsclite->m_card_context);
SCardEstablishContext(pcsclite->m_scope, NULL, NULL, &pcsclite->m_card_context);
result = get_card_readers(pcsclite, async_result);
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/pcsclite.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PCSCLite: public Nan::ObjectWrap {

private:

PCSCLite();
PCSCLite(DWORD scope);

~PCSCLite();

Expand All @@ -56,6 +56,7 @@ class PCSCLite: public Nan::ObjectWrap {
uv_cond_t m_cond;
bool m_pnp;
int m_state;
DWORD m_scope;
static Nan::AsyncResource *async_resource;
};

Expand Down