2013-05-30 10:18:51 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported getSmartcardManager */
|
2013-05-30 10:18:51 -04:00
|
|
|
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Signals = imports.signals;
|
|
|
|
|
|
|
|
const ObjectManager = imports.misc.objectManager;
|
|
|
|
|
2018-08-22 20:55:02 -04:00
|
|
|
const SmartcardTokenIface = `
|
|
|
|
<node>
|
|
|
|
<interface name="org.gnome.SettingsDaemon.Smartcard.Token">
|
|
|
|
<property name="Name" type="s" access="read"/>
|
|
|
|
<property name="Driver" type="o" access="read"/>
|
|
|
|
<property name="IsInserted" type="b" access="read"/>
|
|
|
|
<property name="UsedToLogin" type="b" access="read"/>
|
|
|
|
</interface>
|
|
|
|
</node>`;
|
2013-05-30 10:18:51 -04:00
|
|
|
|
|
|
|
let _smartcardManager = null;
|
|
|
|
|
|
|
|
function getSmartcardManager() {
|
|
|
|
if (_smartcardManager == null)
|
|
|
|
_smartcardManager = new SmartcardManager();
|
|
|
|
|
|
|
|
return _smartcardManager;
|
|
|
|
}
|
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var SmartcardManager = class {
|
|
|
|
constructor() {
|
2013-05-30 10:18:51 -04:00
|
|
|
this._objectManager = new ObjectManager.ObjectManager({ connection: Gio.DBus.session,
|
|
|
|
name: "org.gnome.SettingsDaemon.Smartcard",
|
|
|
|
objectPath: '/org/gnome/SettingsDaemon/Smartcard',
|
2019-01-28 20:27:05 -05:00
|
|
|
knownInterfaces: [SmartcardTokenIface],
|
2017-12-01 19:27:35 -05:00
|
|
|
onLoaded: this._onLoaded.bind(this) });
|
2013-05-30 10:18:51 -04:00
|
|
|
this._insertedTokens = {};
|
|
|
|
this._loginToken = null;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onLoaded() {
|
2013-05-30 10:18:51 -04:00
|
|
|
let tokens = this._objectManager.getProxiesForInterface('org.gnome.SettingsDaemon.Smartcard.Token');
|
|
|
|
|
|
|
|
for (let i = 0; i < tokens.length; i++)
|
|
|
|
this._addToken(tokens[i]);
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._objectManager.connect('interface-added', (objectManager, interfaceName, proxy) => {
|
2013-05-30 10:18:51 -04:00
|
|
|
if (interfaceName == 'org.gnome.SettingsDaemon.Smartcard.Token')
|
|
|
|
this._addToken(proxy);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._objectManager.connect('interface-removed', (objectManager, interfaceName, proxy) => {
|
2013-05-30 10:18:51 -04:00
|
|
|
if (interfaceName == 'org.gnome.SettingsDaemon.Smartcard.Token')
|
|
|
|
this._removeToken(proxy);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateToken(token) {
|
2013-05-30 10:18:51 -04:00
|
|
|
let objectPath = token.get_object_path();
|
|
|
|
|
|
|
|
delete this._insertedTokens[objectPath];
|
|
|
|
|
|
|
|
if (token.IsInserted)
|
|
|
|
this._insertedTokens[objectPath] = token;
|
|
|
|
|
|
|
|
if (token.UsedToLogin)
|
|
|
|
this._loginToken = token;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_addToken(token) {
|
2013-05-30 10:18:51 -04:00
|
|
|
this._updateToken(token);
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
token.connect('g-properties-changed', (proxy, properties) => {
|
|
|
|
if ('IsInserted' in properties.deep_unpack()) {
|
|
|
|
this._updateToken(token);
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (token.IsInserted)
|
2017-10-30 20:38:18 -04:00
|
|
|
this.emit('smartcard-inserted', token);
|
2019-08-19 20:51:42 -04:00
|
|
|
else
|
2017-10-30 20:38:18 -04:00
|
|
|
this.emit('smartcard-removed', token);
|
|
|
|
}
|
|
|
|
});
|
2013-05-30 10:18:51 -04:00
|
|
|
|
|
|
|
// Emit a smartcard-inserted at startup if it's already plugged in
|
|
|
|
if (token.IsInserted)
|
|
|
|
this.emit('smartcard-inserted', token);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_removeToken(token) {
|
2013-05-30 10:18:51 -04:00
|
|
|
let objectPath = token.get_object_path();
|
|
|
|
|
|
|
|
if (this._insertedTokens[objectPath] == token) {
|
|
|
|
delete this._insertedTokens[objectPath];
|
|
|
|
this.emit('smartcard-removed', token);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._loginToken == token)
|
|
|
|
this._loginToken = null;
|
|
|
|
|
|
|
|
token.disconnectAll();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
hasInsertedTokens() {
|
2013-05-30 10:18:51 -04:00
|
|
|
return Object.keys(this._insertedTokens).length > 0;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-05-30 10:18:51 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
hasInsertedLoginToken() {
|
2013-05-30 10:18:51 -04:00
|
|
|
if (!this._loginToken)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!this._loginToken.IsInserted)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2013-05-30 10:18:51 -04:00
|
|
|
Signals.addSignalMethods(SmartcardManager.prototype);
|