2013-05-30 14:18:51 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import * as Signals from './signals.js';
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import * as ObjectManager from './objectManager.js';
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2018-08-23 00:55:02 +00: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 14:18:51 +00:00
|
|
|
|
|
|
|
let _smartcardManager = null;
|
|
|
|
|
2023-07-30 12:56:59 +00:00
|
|
|
/**
|
|
|
|
* @returns {SmartcardManager}
|
|
|
|
*/
|
2023-07-10 09:53:00 +00:00
|
|
|
export function getSmartcardManager() {
|
2013-05-30 14:18:51 +00:00
|
|
|
if (_smartcardManager == null)
|
|
|
|
_smartcardManager = new SmartcardManager();
|
|
|
|
|
|
|
|
return _smartcardManager;
|
|
|
|
}
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class SmartcardManager extends Signals.EventEmitter {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor() {
|
2022-07-04 22:30:44 +00:00
|
|
|
super();
|
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._objectManager = new ObjectManager.ObjectManager({
|
|
|
|
connection: Gio.DBus.session,
|
|
|
|
name: 'org.gnome.SettingsDaemon.Smartcard',
|
|
|
|
objectPath: '/org/gnome/SettingsDaemon/Smartcard',
|
|
|
|
knownInterfaces: [SmartcardTokenIface],
|
|
|
|
onLoaded: this._onLoaded.bind(this),
|
|
|
|
});
|
2013-05-30 14:18:51 +00:00
|
|
|
this._insertedTokens = {};
|
|
|
|
this._loginToken = null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onLoaded() {
|
2013-05-30 14:18:51 +00: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-31 00:38:18 +00:00
|
|
|
this._objectManager.connect('interface-added', (objectManager, interfaceName, proxy) => {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (interfaceName === 'org.gnome.SettingsDaemon.Smartcard.Token')
|
2013-05-30 14:18:51 +00:00
|
|
|
this._addToken(proxy);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
this._objectManager.connect('interface-removed', (objectManager, interfaceName, proxy) => {
|
2023-08-07 00:51:19 +00:00
|
|
|
if (interfaceName === 'org.gnome.SettingsDaemon.Smartcard.Token')
|
2013-05-30 14:18:51 +00:00
|
|
|
this._removeToken(proxy);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateToken(token) {
|
2013-05-30 14:18:51 +00: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-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_addToken(token) {
|
2013-05-30 14:18:51 +00:00
|
|
|
this._updateToken(token);
|
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
token.connect('g-properties-changed', (proxy, properties) => {
|
2022-08-10 09:44:42 +00:00
|
|
|
const isInsertedChanged = !!properties.lookup_value('IsInserted', null);
|
|
|
|
if (isInsertedChanged) {
|
2017-10-31 00:38:18 +00:00
|
|
|
this._updateToken(token);
|
|
|
|
|
2019-08-20 00:51:42 +00:00
|
|
|
if (token.IsInserted)
|
2017-10-31 00:38:18 +00:00
|
|
|
this.emit('smartcard-inserted', token);
|
2019-08-20 00:51:42 +00:00
|
|
|
else
|
2017-10-31 00:38:18 +00:00
|
|
|
this.emit('smartcard-removed', token);
|
|
|
|
}
|
|
|
|
});
|
2013-05-30 14:18:51 +00:00
|
|
|
|
|
|
|
// Emit a smartcard-inserted at startup if it's already plugged in
|
|
|
|
if (token.IsInserted)
|
|
|
|
this.emit('smartcard-inserted', token);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_removeToken(token) {
|
2013-05-30 14:18:51 +00:00
|
|
|
let objectPath = token.get_object_path();
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._insertedTokens[objectPath] === token) {
|
2013-05-30 14:18:51 +00:00
|
|
|
delete this._insertedTokens[objectPath];
|
|
|
|
this.emit('smartcard-removed', token);
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:51:19 +00:00
|
|
|
if (this._loginToken === token)
|
2013-05-30 14:18:51 +00:00
|
|
|
this._loginToken = null;
|
|
|
|
|
|
|
|
token.disconnectAll();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
hasInsertedTokens() {
|
2013-05-30 14:18:51 +00:00
|
|
|
return Object.keys(this._insertedTokens).length > 0;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-30 14:18:51 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
hasInsertedLoginToken() {
|
2013-05-30 14:18:51 +00:00
|
|
|
if (!this._loginToken)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!this._loginToken.IsInserted)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|