2018-04-23 15:18:46 +00:00
|
|
|
/* gpgme.js - Javascript integration for gpgme
|
|
|
|
* Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
|
|
|
|
*
|
|
|
|
* This file is part of GPGME.
|
|
|
|
*
|
|
|
|
* GPGME is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GPGME is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1+
|
|
|
|
*
|
2018-06-06 11:05:53 +00:00
|
|
|
* Author(s):
|
|
|
|
* Maximilian Krambach <mkrambach@intevation.de>
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-06 11:05:53 +00:00
|
|
|
import { isFingerprint, isLongId } from './Helpers';
|
|
|
|
import { gpgme_error } from './Errors';
|
2018-04-25 17:45:39 +00:00
|
|
|
import { createMessage } from './Message';
|
2018-05-03 12:12:10 +00:00
|
|
|
|
2018-05-25 17:02:18 +00:00
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Validates the given fingerprint and creates a new {@link GPGME_Key}
|
2018-05-25 17:02:18 +00:00
|
|
|
* @param {String} fingerprint
|
2018-07-10 12:32:26 +00:00
|
|
|
* @returns {GPGME_Key|GPGME_Error}
|
2018-05-25 17:02:18 +00:00
|
|
|
*/
|
2018-05-28 14:52:50 +00:00
|
|
|
export function createKey(fingerprint){
|
2018-05-03 12:12:10 +00:00
|
|
|
if (!isFingerprint(fingerprint)){
|
2018-05-04 10:56:59 +00:00
|
|
|
return gpgme_error('PARAM_WRONG');
|
2018-05-03 12:12:10 +00:00
|
|
|
}
|
2018-05-28 14:52:50 +00:00
|
|
|
else return new GPGME_Key(fingerprint);
|
2018-05-03 12:12:10 +00:00
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
|
2018-05-25 17:02:18 +00:00
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Represents the Keys as stored in the gnupg backend
|
2018-06-06 11:05:53 +00:00
|
|
|
* It allows to query almost all information defined in gpgme Key Objects
|
2018-07-10 12:32:26 +00:00
|
|
|
* Refer to {@link validKeyProperties} for available information, and the gpgme
|
2018-06-06 11:05:53 +00:00
|
|
|
* documentation on their meaning
|
|
|
|
* (https://www.gnupg.org/documentation/manuals/gpgme/Key-objects.html)
|
|
|
|
*
|
2018-07-10 12:32:26 +00:00
|
|
|
* @class
|
2018-05-25 17:02:18 +00:00
|
|
|
*/
|
2018-04-23 15:18:46 +00:00
|
|
|
export class GPGME_Key {
|
|
|
|
|
2018-05-28 14:52:50 +00:00
|
|
|
constructor(fingerprint){
|
2018-04-24 17:29:32 +00:00
|
|
|
this.fingerprint = fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
set fingerprint(fpr){
|
2018-05-25 17:02:18 +00:00
|
|
|
if (isFingerprint(fpr) === true) {
|
|
|
|
if (this._data === undefined) {
|
|
|
|
this._data = {fingerprint: fpr};
|
|
|
|
} else {
|
|
|
|
if (this._data.fingerprint === undefined){
|
|
|
|
this._data.fingerprint = fpr;
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* @returns {String} The fingerprint defining this Key
|
|
|
|
*/
|
2018-04-23 15:18:46 +00:00
|
|
|
get fingerprint(){
|
2018-05-25 17:02:18 +00:00
|
|
|
if (!this._data || !this._data.fingerprint){
|
2018-05-03 16:03:22 +00:00
|
|
|
return gpgme_error('KEY_INVALID');
|
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
return this._data.fingerprint;
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* @param {Object} data Bulk set the data for this key, with an Object sent
|
2018-05-25 17:02:18 +00:00
|
|
|
* by gpgme-json.
|
2018-07-10 12:32:26 +00:00
|
|
|
* @returns {GPGME_Key|GPGME_Error} Itself after values have been set, an
|
|
|
|
* error if something went wrong
|
|
|
|
* @private
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-05-28 15:26:56 +00:00
|
|
|
setKeyData(data){
|
2018-05-25 17:02:18 +00:00
|
|
|
if (this._data === undefined) {
|
|
|
|
this._data = {};
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
typeof(data) !== 'object') {
|
|
|
|
return gpgme_error('KEY_INVALID');
|
|
|
|
}
|
|
|
|
if (!this._data.fingerprint && isFingerprint(data.fingerprint)){
|
|
|
|
if (data.fingerprint !== this.fingerprint){
|
|
|
|
return gpgme_error('KEY_INVALID');
|
|
|
|
}
|
|
|
|
this._data.fingerprint = data.fingerprint;
|
|
|
|
} else if (this._data.fingerprint !== data.fingerprint){
|
|
|
|
return gpgme_error('KEY_INVALID');
|
|
|
|
}
|
2018-05-30 15:05:54 +00:00
|
|
|
let dataKeys = Object.keys(data);
|
|
|
|
for (let i=0; i< dataKeys.length; i++){
|
|
|
|
if (!validKeyProperties.hasOwnProperty(dataKeys[i])){
|
2018-05-25 17:02:18 +00:00
|
|
|
return gpgme_error('KEY_INVALID');
|
|
|
|
}
|
2018-05-30 15:05:54 +00:00
|
|
|
if (validKeyProperties[dataKeys[i]](data[dataKeys[i]]) !== true ){
|
2018-05-25 17:02:18 +00:00
|
|
|
return gpgme_error('KEY_INVALID');
|
|
|
|
}
|
2018-05-30 15:05:54 +00:00
|
|
|
switch (dataKeys[i]){
|
2018-06-06 11:05:53 +00:00
|
|
|
case 'subkeys':
|
|
|
|
this._data.subkeys = [];
|
|
|
|
for (let i=0; i< data.subkeys.length; i++) {
|
|
|
|
this._data.subkeys.push(
|
|
|
|
new GPGME_Subkey(data.subkeys[i]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'userids':
|
|
|
|
this._data.userids = [];
|
|
|
|
for (let i=0; i< data.userids.length; i++) {
|
|
|
|
this._data.userids.push(
|
|
|
|
new GPGME_UserId(data.userids[i]));
|
|
|
|
}
|
|
|
|
break;
|
2018-06-06 13:29:21 +00:00
|
|
|
case 'last_update':
|
|
|
|
this._data[dataKeys[i]] = new Date( data[dataKeys[i]] * 1000 );
|
|
|
|
break;
|
2018-06-06 11:05:53 +00:00
|
|
|
default:
|
|
|
|
this._data[dataKeys[i]] = data[dataKeys[i]];
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
2018-05-03 12:12:10 +00:00
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
return this;
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Query any property of the Key listed in {@link validKeyProperties}
|
|
|
|
* @param {String} property property to be retreived
|
|
|
|
* @param {Boolean} cached (optional) if false, the data will be directly
|
|
|
|
* queried from gnupg, and the operation will be asynchronous. Else, the
|
|
|
|
* data will be fetched from the state of the initialization of the Key.
|
|
|
|
* The cached mode may contain outdated information, but can be used as
|
|
|
|
* synchronous operation, where the backend is not expected to change Keys
|
|
|
|
* during a session. The key still can be reloaded by invoking
|
|
|
|
* {@link refreshKey}.
|
|
|
|
* @returns {*|Promise<*>} the value (Boolean, String, Array, Object).
|
2018-07-24 12:56:33 +00:00
|
|
|
* If 'cached' is false, the value will be resolved as a Promise.
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
get(property, cached=true) {
|
|
|
|
if (cached === false) {
|
|
|
|
let me = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
2018-05-30 15:05:54 +00:00
|
|
|
if (!validKeyProperties.hasOwnProperty(property)){
|
|
|
|
reject('PARAM_WRONG');
|
|
|
|
} else if (property === 'armored'){
|
2018-05-28 15:26:56 +00:00
|
|
|
resolve(me.getArmor());
|
|
|
|
} else if (property === 'hasSecret'){
|
|
|
|
resolve(me.getHasSecret());
|
|
|
|
} else {
|
|
|
|
me.refreshKey().then(function(key){
|
|
|
|
resolve(key.get(property, true));
|
|
|
|
}, function(error){
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
});
|
2018-05-30 15:05:54 +00:00
|
|
|
} else {
|
|
|
|
if (!validKeyProperties.hasOwnProperty(property)){
|
2018-05-25 17:02:18 +00:00
|
|
|
return gpgme_error('PARAM_WRONG');
|
2018-05-30 15:05:54 +00:00
|
|
|
}
|
|
|
|
if (!this._data.hasOwnProperty(property)){
|
|
|
|
return gpgme_error('KEY_NO_INIT');
|
2018-05-25 17:02:18 +00:00
|
|
|
} else {
|
|
|
|
return (this._data[property]);
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Reloads the Key information from gnupg. This is only useful if you use
|
|
|
|
* the GPGME_Keys cached. Note that this is a performance hungry operation.
|
|
|
|
* If you desire more than a few refreshs, it may be advisable to run
|
|
|
|
* {@link Keyring.getKeys} instead.
|
|
|
|
* @returns {Promise<GPGME_Key|GPGME_Error>}
|
|
|
|
* @async
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
refreshKey() {
|
|
|
|
let me = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
if (!me._data.fingerprint){
|
|
|
|
reject(gpgme_error('KEY_INVALID'));
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
let msg = createMessage('keylist');
|
|
|
|
msg.setParameter('sigs', true);
|
|
|
|
msg.setParameter('keys', me._data.fingerprint);
|
2018-05-28 14:52:50 +00:00
|
|
|
msg.post().then(function(result){
|
2018-05-25 17:02:18 +00:00
|
|
|
if (result.keys.length === 1){
|
2018-05-28 15:26:56 +00:00
|
|
|
me.setKeyData(result.keys[0]);
|
2018-07-10 12:32:26 +00:00
|
|
|
me.getHasSecret().then(function(){
|
2018-07-24 12:56:33 +00:00
|
|
|
me.getArmor().then(function(){
|
|
|
|
resolve(me);
|
|
|
|
}, function(error){
|
|
|
|
reject(error);
|
|
|
|
});
|
2018-07-10 12:32:26 +00:00
|
|
|
}, function(error){
|
|
|
|
reject(error);
|
|
|
|
});
|
2018-05-25 17:02:18 +00:00
|
|
|
} else {
|
|
|
|
reject(gpgme_error('KEY_NOKEY'));
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
}, function (error) {
|
|
|
|
reject(gpgme_error('GNUPG_ERROR'), error);
|
2018-06-06 11:05:53 +00:00
|
|
|
});
|
2018-04-23 15:18:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Query the armored block of the Key directly from gnupg. Please note that
|
|
|
|
* this will not get you any export of the secret/private parts of a Key
|
|
|
|
* @returns {Promise<String|GPGME_Error>}
|
2018-06-13 13:22:03 +00:00
|
|
|
* @async
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-06-06 11:05:53 +00:00
|
|
|
getArmor(){
|
2018-05-28 15:26:56 +00:00
|
|
|
let me = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
if (!me._data.fingerprint){
|
|
|
|
reject(gpgme_error('KEY_INVALID'));
|
|
|
|
}
|
|
|
|
let msg = createMessage('export');
|
|
|
|
msg.setParameter('armor', true);
|
|
|
|
msg.setParameter('keys', me._data.fingerprint);
|
|
|
|
msg.post().then(function(result){
|
2018-06-06 09:57:41 +00:00
|
|
|
me._data.armored = result.data;
|
2018-05-28 15:26:56 +00:00
|
|
|
resolve(result.data);
|
|
|
|
}, function(error){
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
|
2018-06-13 13:22:03 +00:00
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Find out if the Key includes a secret part. Note that this is a rather
|
|
|
|
* nonperformant operation, as it needs to query gnupg twice. If you want
|
|
|
|
* this inforrmation about more than a few Keys, it may be advisable to run
|
|
|
|
* {@link Keyring.getKeys} instead.
|
|
|
|
* @returns {Promise<Boolean|GPGME_Error>} True if a secret/private Key is
|
|
|
|
* available in the gnupg Keyring
|
2018-06-13 13:22:03 +00:00
|
|
|
* @async
|
|
|
|
*/
|
2018-05-28 15:26:56 +00:00
|
|
|
getHasSecret(){
|
|
|
|
let me = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
if (!me._data.fingerprint){
|
|
|
|
reject(gpgme_error('KEY_INVALID'));
|
|
|
|
}
|
|
|
|
let msg = createMessage('keylist');
|
|
|
|
msg.setParameter('keys', me._data.fingerprint);
|
|
|
|
msg.setParameter('secret', true);
|
|
|
|
msg.post().then(function(result){
|
|
|
|
me._data.hasSecret = null;
|
2018-06-19 07:26:01 +00:00
|
|
|
if (
|
|
|
|
result.keys &&
|
|
|
|
result.keys.length === 1 &&
|
|
|
|
result.keys[0].secret === true
|
|
|
|
) {
|
|
|
|
me._data.hasSecret = true;
|
|
|
|
resolve(true);
|
|
|
|
} else {
|
2018-05-28 15:26:56 +00:00
|
|
|
me._data.hasSecret = false;
|
|
|
|
resolve(false);
|
|
|
|
}
|
|
|
|
}, function(error){
|
2018-06-06 11:05:53 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
2018-05-28 15:26:56 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-30 15:05:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-06 11:05:53 +00:00
|
|
|
* Convenience functions to be directly used as properties of the Key
|
2018-05-30 15:05:54 +00:00
|
|
|
* Notice that these rely on cached info and may be outdated. Use the async
|
|
|
|
* get(property, false) if you need the most current info
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Property for the export of armored Key. If the armored Key is not
|
|
|
|
* cached, it returns an {@link GPGME_Error} with code 'KEY_NO_INIT'.
|
|
|
|
* Running {@link refreshKey} may help in this case.
|
|
|
|
* @returns {String|GPGME_Error} The armored public Key block.
|
2018-05-30 15:05:54 +00:00
|
|
|
*/
|
|
|
|
get armored(){
|
|
|
|
return this.get('armored', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Property indicating if the Key possesses a private/secret part. If this
|
|
|
|
* information is not yet cached, it returns an {@link GPGME_Error} with
|
|
|
|
* code 'KEY_NO_INIT'. Running {@link refreshKey} may help in this case.
|
|
|
|
* @returns {Boolean} If the Key has a secret subkey.
|
2018-05-30 15:05:54 +00:00
|
|
|
*/
|
|
|
|
get hasSecret(){
|
|
|
|
return this.get('hasSecret', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Deletes the (public) Key from the GPG Keyring. Note that a deletion of a
|
2018-05-30 15:05:54 +00:00
|
|
|
* secret key is not supported by the native backend.
|
2018-07-10 12:32:26 +00:00
|
|
|
* @returns {Promise<Boolean|GPGME_Error>} Success if key was deleted,
|
|
|
|
* rejects with a GPG error otherwise.
|
2018-05-30 15:05:54 +00:00
|
|
|
*/
|
|
|
|
delete(){
|
|
|
|
let me = this;
|
|
|
|
return new Promise(function(resolve, reject){
|
|
|
|
if (!me._data.fingerprint){
|
|
|
|
reject(gpgme_error('KEY_INVALID'));
|
|
|
|
}
|
|
|
|
let msg = createMessage('delete');
|
|
|
|
msg.setParameter('key', me._data.fingerprint);
|
|
|
|
msg.post().then(function(result){
|
|
|
|
resolve(result.success);
|
|
|
|
}, function(error){
|
|
|
|
reject(error);
|
2018-06-06 11:05:53 +00:00
|
|
|
});
|
2018-05-30 15:05:54 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Representing a subkey of a Key.
|
|
|
|
* @class
|
|
|
|
* @protected
|
2018-05-25 17:02:18 +00:00
|
|
|
*/
|
|
|
|
class GPGME_Subkey {
|
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Initializes with the json data sent by gpgme-json
|
|
|
|
* @param {Object} data
|
|
|
|
* @private
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
constructor(data){
|
|
|
|
let keys = Object.keys(data);
|
|
|
|
for (let i=0; i< keys.length; i++) {
|
|
|
|
this.setProperty(keys[i], data[keys[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Validates a subkey property against {@link validSubKeyProperties} and
|
|
|
|
* sets it if validation is successful
|
|
|
|
* @param {String} property
|
|
|
|
* @param {*} value
|
|
|
|
* @param private
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
setProperty(property, value){
|
|
|
|
if (!this._data){
|
|
|
|
this._data = {};
|
|
|
|
}
|
|
|
|
if (validSubKeyProperties.hasOwnProperty(property)){
|
|
|
|
if (validSubKeyProperties[property](value) === true) {
|
2018-06-06 13:29:21 +00:00
|
|
|
if (property === 'timestamp' || property === 'expires'){
|
|
|
|
this._data[property] = new Date(value * 1000);
|
|
|
|
} else {
|
|
|
|
this._data[property] = value;
|
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Fetches any information about this subkey
|
2018-05-25 17:02:18 +00:00
|
|
|
* @param {String} property Information to request
|
2018-07-10 12:32:26 +00:00
|
|
|
* @returns {String | Number | Date}
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
get(property) {
|
|
|
|
if (this._data.hasOwnProperty(property)){
|
|
|
|
return (this._data[property]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Representing user attributes associated with a Key or subkey
|
|
|
|
* @class
|
|
|
|
* @protected
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
class GPGME_UserId {
|
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Initializes with the json data sent by gpgme-json
|
|
|
|
* @param {Object} data
|
|
|
|
* @private
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
constructor(data){
|
|
|
|
let keys = Object.keys(data);
|
|
|
|
for (let i=0; i< keys.length; i++) {
|
|
|
|
this.setProperty(keys[i], data[keys[i]]);
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Validates a subkey property against {@link validUserIdProperties} and
|
|
|
|
* sets it if validation is successful
|
|
|
|
* @param {String} property
|
|
|
|
* @param {*} value
|
|
|
|
* @param private
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
setProperty(property, value){
|
|
|
|
if (!this._data){
|
|
|
|
this._data = {};
|
|
|
|
}
|
|
|
|
if (validUserIdProperties.hasOwnProperty(property)){
|
|
|
|
if (validUserIdProperties[property](value) === true) {
|
2018-06-06 13:29:21 +00:00
|
|
|
if (property === 'last_update'){
|
|
|
|
this._data[property] = new Date(value*1000);
|
|
|
|
} else {
|
|
|
|
this._data[property] = value;
|
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-06 13:29:21 +00:00
|
|
|
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Fetches information about the user
|
2018-05-25 17:02:18 +00:00
|
|
|
* @param {String} property Information to request
|
|
|
|
* @returns {String | Number}
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
get(property) {
|
|
|
|
if (this._data.hasOwnProperty(property)){
|
|
|
|
return (this._data[property]);
|
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Validation definition for userIds. Each valid userId property is represented
|
|
|
|
* as a key- Value pair, with their value being a validation function to check
|
|
|
|
* against
|
|
|
|
* @protected
|
|
|
|
* @const
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
const validUserIdProperties = {
|
|
|
|
'revoked': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'invalid': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'uid': function(value){
|
|
|
|
if (typeof(value) === 'string' || value === ''){
|
2018-06-06 11:05:53 +00:00
|
|
|
return true;
|
2018-05-03 16:03:22 +00:00
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
'validity': function(value){
|
|
|
|
if (typeof(value) === 'string'){
|
2018-06-06 11:05:53 +00:00
|
|
|
return true;
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
'name': function(value){
|
|
|
|
if (typeof(value) === 'string' || value === ''){
|
2018-06-06 11:05:53 +00:00
|
|
|
return true;
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
'email': function(value){
|
|
|
|
if (typeof(value) === 'string' || value === ''){
|
2018-06-06 11:05:53 +00:00
|
|
|
return true;
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
'address': function(value){
|
|
|
|
if (typeof(value) === 'string' || value === ''){
|
2018-06-06 11:05:53 +00:00
|
|
|
return true;
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
'comment': function(value){
|
|
|
|
if (typeof(value) === 'string' || value === ''){
|
2018-06-06 11:05:53 +00:00
|
|
|
return true;
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
'origin': function(value){
|
|
|
|
return Number.isInteger(value);
|
|
|
|
},
|
|
|
|
'last_update': function(value){
|
|
|
|
return Number.isInteger(value);
|
2018-05-03 12:12:10 +00:00
|
|
|
}
|
2018-05-25 17:02:18 +00:00
|
|
|
};
|
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Validation definition for subKeys. Each valid userId property is represented
|
|
|
|
* as a key-value pair, with the value being a validation function
|
|
|
|
* @protected
|
|
|
|
* @const
|
|
|
|
*/
|
2018-05-25 17:02:18 +00:00
|
|
|
const validSubKeyProperties = {
|
|
|
|
'invalid': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_encrypt': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_sign': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_certify': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_authenticate': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'secret': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'is_qualified': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'is_cardkey': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'is_de_vs': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'pubkey_algo_name': function(value){
|
2018-06-06 11:05:53 +00:00
|
|
|
return typeof(value) === 'string';
|
|
|
|
// TODO: check against list of known?['']
|
2018-05-25 17:02:18 +00:00
|
|
|
},
|
|
|
|
'pubkey_algo_string': function(value){
|
|
|
|
return typeof(value) === 'string';
|
|
|
|
// TODO: check against list of known?['']
|
|
|
|
},
|
|
|
|
'keyid': function(value){
|
|
|
|
return isLongId(value);
|
|
|
|
},
|
|
|
|
'pubkey_algo': function(value) {
|
|
|
|
return (Number.isInteger(value) && value >= 0);
|
|
|
|
},
|
|
|
|
'length': function(value){
|
|
|
|
return (Number.isInteger(value) && value > 0);
|
|
|
|
},
|
|
|
|
'timestamp': function(value){
|
|
|
|
return (Number.isInteger(value) && value > 0);
|
|
|
|
},
|
|
|
|
'expires': function(value){
|
|
|
|
return (Number.isInteger(value) && value > 0);
|
|
|
|
}
|
2018-06-06 11:05:53 +00:00
|
|
|
};
|
2018-07-10 12:32:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validation definition for Keys. Each valid Key property is represented
|
|
|
|
* as a key-value pair, with their value being a validation function
|
|
|
|
* @protected
|
|
|
|
* @const
|
|
|
|
*/
|
2018-05-30 15:05:54 +00:00
|
|
|
const validKeyProperties = {
|
|
|
|
'fingerprint': function(value){
|
|
|
|
return isFingerprint(value);
|
|
|
|
},
|
|
|
|
'armored': function(value){
|
|
|
|
return typeof(value === 'string');
|
|
|
|
},
|
|
|
|
'revoked': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'expired': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'disabled': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'invalid': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_encrypt': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_sign': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_certify': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'can_authenticate': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'secret': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'is_qualified': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
},
|
|
|
|
'protocol': function(value){
|
|
|
|
return typeof(value) === 'string';
|
|
|
|
//TODO check for implemented ones
|
|
|
|
},
|
|
|
|
'issuer_serial': function(value){
|
|
|
|
return typeof(value) === 'string';
|
|
|
|
},
|
|
|
|
'issuer_name': function(value){
|
|
|
|
return typeof(value) === 'string';
|
|
|
|
},
|
|
|
|
'chain_id': function(value){
|
|
|
|
return typeof(value) === 'string';
|
|
|
|
},
|
|
|
|
'owner_trust': function(value){
|
|
|
|
return typeof(value) === 'string';
|
|
|
|
},
|
|
|
|
'last_update': function(value){
|
|
|
|
return (Number.isInteger(value));
|
|
|
|
//TODO undefined/null possible?
|
|
|
|
},
|
|
|
|
'origin': function(value){
|
|
|
|
return (Number.isInteger(value));
|
|
|
|
},
|
|
|
|
'subkeys': function(value){
|
|
|
|
return (Array.isArray(value));
|
|
|
|
},
|
|
|
|
'userids': function(value){
|
|
|
|
return (Array.isArray(value));
|
|
|
|
},
|
|
|
|
'tofu': function(value){
|
|
|
|
return (Array.isArray(value));
|
|
|
|
},
|
|
|
|
'hasSecret': function(value){
|
|
|
|
return typeof(value) === 'boolean';
|
|
|
|
}
|
|
|
|
|
2018-06-06 11:05:53 +00:00
|
|
|
};
|