mirror of https://github.com/moparisthebest/mail
commit
fef264248d
@ -0,0 +1,78 @@
|
||||
'use strict';
|
||||
|
||||
//
|
||||
// Controller
|
||||
//
|
||||
|
||||
var PublickeyImportCtrl = function($scope, $q, keychain, pgp, hkp, dialog, appConfig) {
|
||||
|
||||
//
|
||||
// scope state
|
||||
//
|
||||
|
||||
$scope.state.publickeyImport = {
|
||||
toggle: function(to) {
|
||||
$scope.state.lightbox = (to) ? 'publickey-import' : undefined;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// scope variables
|
||||
//
|
||||
|
||||
$scope.hkpUrl = appConfig.config.hkpUrl.replace('https://', '');
|
||||
|
||||
//
|
||||
// scope functions
|
||||
//
|
||||
|
||||
$scope.importKey = function(publicKeyArmored) {
|
||||
var keyParams, pubkey;
|
||||
|
||||
// verifiy public key string
|
||||
if (publicKeyArmored.indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') < 0) {
|
||||
dialog.error({
|
||||
showBugReporter: false,
|
||||
message: 'Invalid public key!'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
keyParams = pgp.getKeyParams(publicKeyArmored);
|
||||
} catch (e) {
|
||||
dialog.error(new Error('Error reading public key params!'));
|
||||
return;
|
||||
}
|
||||
|
||||
pubkey = {
|
||||
_id: keyParams._id,
|
||||
userId: keyParams.userId,
|
||||
userIds: keyParams.userIds,
|
||||
publicKey: publicKeyArmored,
|
||||
imported: true // mark manually imported keys
|
||||
};
|
||||
|
||||
return keychain.saveLocalPublicKey(pubkey).then(function() {
|
||||
$scope.pastedKey = '';
|
||||
// display success message
|
||||
return dialog.info({
|
||||
title: 'Success',
|
||||
message: 'Public key ' + keyParams._id + ' for ' + keyParams.userId + ' imported successfully!'
|
||||
});
|
||||
}).catch(dialog.error);
|
||||
};
|
||||
|
||||
$scope.lookupKey = function(query) {
|
||||
var keyUrl = hkp.getIndexUrl(query);
|
||||
|
||||
return dialog.info({
|
||||
title: 'Link',
|
||||
message: 'Follow this link and paste the PGP key block above...',
|
||||
faqLink: keyUrl,
|
||||
faqLinkTitle: keyUrl
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = PublickeyImportCtrl;
|
@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
var ngModule = angular.module('woServices');
|
||||
ngModule.service('hkp', HKP);
|
||||
module.exports = HKP;
|
||||
|
||||
function HKP(appConfig) {
|
||||
this._appConfig = appConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a url of the link to be opened in a new window
|
||||
* @param {String} query Either the email address or name
|
||||
* @return {String} The url of the hkp query
|
||||
*/
|
||||
HKP.prototype.getIndexUrl = function(query) {
|
||||
var baseUrl = this._appConfig.config.hkpUrl + '/pks/lookup?op=index&search=';
|
||||
return baseUrl + encodeURIComponent(query);
|
||||
};
|
@ -0,0 +1,27 @@
|
||||
<div class="lightbox__body" ng-controller="PublicKeyImportCtrl">
|
||||
<header class="lightbox__header">
|
||||
<h2>Import PGP keys</h2>
|
||||
<button class="lightbox__close" wo-touch="state.publickeyImport.toggle(false)" data-action="lightbox-close">
|
||||
<svg><use xlink:href="#icon-close" /><title>Close</title></svg>
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div class="lightbox__content">
|
||||
<div class="u-gap-bottom">
|
||||
<p class="typo-paragraph">You can import public keys via copy/paste or as .asc files. Use the lookup function to search by name or email address on the key server. <i>Most keys are fetched automatically while reading and writing messages so most users won't need to import keys manually.</i></p>
|
||||
</div>
|
||||
|
||||
<div class="u-gap-bottom">
|
||||
<textarea class="textarea" placeholder="Paste PUBLIC PGP KEY BLOCK here..." ng-model="pastedKey" ng-change="importKey(pastedKey)"></textarea>
|
||||
</div>
|
||||
|
||||
<form class="form-input-with-button u-gap-bottom">
|
||||
<input class="input-text" type="text" placeholder="Lookup on {{hkpUrl}} by name or email address" ng-model="searchText">
|
||||
<button class="btn" type="submit" ng-click="lookupKey(searchText)">Lookup</button>
|
||||
</form>
|
||||
|
||||
<div class="form__row">
|
||||
<input class="input-file" type="file" multiple keyfile-input>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -0,0 +1,91 @@
|
||||
'use strict';
|
||||
|
||||
var PublicKeyImportCtrl = require('../../../../src/js/controller/app/publickey-import'),
|
||||
Keychain = require('../../../../src/js/service/keychain'),
|
||||
PGP = require('../../../../src/js/crypto/pgp'),
|
||||
Dialog = require('../../../../src/js/util/dialog');
|
||||
|
||||
describe('Public Key Import Controller unit test', function() {
|
||||
var scope, ctrl, keychainStub, pgpStub, dialogStub;
|
||||
|
||||
beforeEach(function() {
|
||||
pgpStub = sinon.createStubInstance(PGP);
|
||||
keychainStub = sinon.createStubInstance(Keychain);
|
||||
dialogStub = sinon.createStubInstance(Dialog);
|
||||
|
||||
angular.module('publickey-import', ['woServices']);
|
||||
angular.mock.module('publickey-import');
|
||||
angular.mock.inject(function($rootScope, $controller) {
|
||||
scope = $rootScope.$new();
|
||||
scope.state = {};
|
||||
ctrl = $controller(PublicKeyImportCtrl, {
|
||||
$scope: scope,
|
||||
$q: window.qMock,
|
||||
keychain: keychainStub,
|
||||
pgp: pgpStub,
|
||||
dialog: dialogStub
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function() {});
|
||||
|
||||
describe('importKey', function() {
|
||||
it('should work', function(done) {
|
||||
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
|
||||
|
||||
pgpStub.getKeyParams.returns({
|
||||
_id: '12345',
|
||||
userId: 'max@example.com',
|
||||
userIds: []
|
||||
});
|
||||
|
||||
keychainStub.saveLocalPublicKey.withArgs({
|
||||
_id: '12345',
|
||||
userId: 'max@example.com',
|
||||
userIds: [],
|
||||
publicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----',
|
||||
imported: true
|
||||
}).returns(resolves());
|
||||
|
||||
scope.listKeys = function() {};
|
||||
|
||||
scope.importKey(keyArmored).then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail due to invalid armored key', function() {
|
||||
var keyArmored = '-----BEGIN PGP PRIVATE KEY BLOCK-----';
|
||||
|
||||
scope.importKey(keyArmored);
|
||||
|
||||
expect(dialogStub.error.calledOnce).to.be.true;
|
||||
});
|
||||
|
||||
it('should fail due to error in pgp.getKeyParams', function() {
|
||||
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
|
||||
pgpStub.getKeyParams.throws(new Error('WAT'));
|
||||
|
||||
scope.importKey(keyArmored);
|
||||
|
||||
expect(dialogStub.error.calledOnce).to.be.true;
|
||||
});
|
||||
|
||||
it('should fail due to error in keychain.saveLocalPublicKey', function(done) {
|
||||
var keyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
|
||||
|
||||
pgpStub.getKeyParams.returns({
|
||||
_id: '12345',
|
||||
userId: 'max@example.com'
|
||||
});
|
||||
|
||||
keychainStub.saveLocalPublicKey.returns(rejects(42));
|
||||
|
||||
scope.importKey(keyArmored).then(function() {
|
||||
expect(dialogStub.error.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue