From 2713f6c5a2b49f11a61dc3e48d4efbfeb31112c9 Mon Sep 17 00:00:00 2001 From: cipres Date: Sat, 31 Oct 2020 00:08:22 +0100 Subject: [PATCH] Fix bugs in the peers UI and didauth service --- galacteek/__init__.py | 2 +- galacteek/core/profile.py | 3 +++ galacteek/core/schemes.py | 14 +++++++++----- galacteek/did/ipid.py | 4 +++- galacteek/ipfs/encrypt.py | 18 ++++++++++-------- galacteek/ipfs/p2pservices/didauth.py | 14 ++++++++++---- galacteek/ui/dialogs.py | 1 + galacteek/ui/peers.py | 4 ++-- 8 files changed, 39 insertions(+), 21 deletions(-) diff --git a/galacteek/__init__.py b/galacteek/__init__.py index 674c0d0f..56cdb76a 100644 --- a/galacteek/__init__.py +++ b/galacteek/__init__.py @@ -46,7 +46,7 @@ # .............................................................................. # -__version__ = '0.4.37' +__version__ = '0.4.38' from galacteek.core.asynclib import * # noqa from galacteek.core import glogger diff --git a/galacteek/core/profile.py b/galacteek/core/profile.py index ca8735a0..e8522437 100644 --- a/galacteek/core/profile.py +++ b/galacteek/core/profile.py @@ -1079,6 +1079,9 @@ async def createIpIdentifier(self, ipfsop, self.userInfo.curIdentity['personDid'] ) + if passphrase: + await ipid.unlock(rsaPassphrase=passphrase) + return ipid @ipfsOp diff --git a/galacteek/core/schemes.py b/galacteek/core/schemes.py index 3e2343ee..fcf5ce45 100644 --- a/galacteek/core/schemes.py +++ b/galacteek/core/schemes.py @@ -66,6 +66,8 @@ QWebEngineUrlScheme.ViewSourceAllowed defaultLocalSchemeFlags = defaultSchemeFlags | QWebEngineUrlScheme.LocalScheme +serviceWorkersFlags = \ + defaultSchemeFlags | QWebEngineUrlScheme.ServiceWorkersAllowed # Registered URL schemes @@ -132,35 +134,37 @@ def initializeSchemes(): declareUrlScheme( SCHEME_DWEB, syntax=QWebEngineUrlScheme.Syntax.Path, + flags=serviceWorkersFlags ) declareUrlScheme( SCHEME_DWEBGW, syntax=QWebEngineUrlScheme.Syntax.Path, - flags=defaultSchemeFlags + flags=serviceWorkersFlags ) declareUrlScheme( SCHEME_FS, syntax=QWebEngineUrlScheme.Syntax.Path, - flags=defaultSchemeFlags + flags=serviceWorkersFlags ) declareUrlScheme( SCHEME_IPFS, syntax=QWebEngineUrlScheme.Syntax.Host, - flags=defaultSchemeFlags + flags=serviceWorkersFlags ) declareUrlScheme( SCHEME_IPNS, syntax=QWebEngineUrlScheme.Syntax.Host, - flags=defaultSchemeFlags + flags=serviceWorkersFlags ) declareUrlScheme( SCHEME_ENS, - syntax=QWebEngineUrlScheme.Syntax.Host + syntax=QWebEngineUrlScheme.Syntax.Host, + flags=serviceWorkersFlags ) declareUrlScheme( diff --git a/galacteek/did/ipid.py b/galacteek/did/ipid.py index 49868850..b5959d10 100644 --- a/galacteek/did/ipid.py +++ b/galacteek/did/ipid.py @@ -1039,7 +1039,9 @@ async def didAuthPerform(self, ipfsop, streamCtx, ipid, token=None): json=req) as resp: if resp.status != HTTPOk.status_code: - raise Exception('DID Auth error') + payload = await resp.json() + log.debug('Error payload: {payload}') + raise Exception(f'DID Auth error: code {resp.status}') payload = await resp.json() diff --git a/galacteek/ipfs/encrypt.py b/galacteek/ipfs/encrypt.py index b3db1d1d..2ac07ace 100644 --- a/galacteek/ipfs/encrypt.py +++ b/galacteek/ipfs/encrypt.py @@ -9,7 +9,6 @@ from jwcrypto import jwt from jwcrypto.common import json_encode -from galacteek.core.asynccache import selfcachedcoromethod from galacteek.ipfs.wrappers import ipfsOp from galacteek.ipfs.cidhelpers import cidValid from galacteek.core.asynclib import asyncReadFile @@ -56,7 +55,7 @@ async def pubKeyCid(self, ipfsop): async def privJwk(self): try: - privKey = await self.__privateKey() + privKey = await self._privateKey() pem = privKey.export_key(pkcs=8) key = jwk.JWK() key.import_from_pem(pem) @@ -100,7 +99,7 @@ async def encrypt(self, data, pubKey, sessionKey=None, cacheKey=False): async def decrypt(self, data): return await self.rsaExec.decryptData(BytesIO(data), - await self.__privateKey()) + await self._privateKey()) @ipfsOp async def storeSelf(self, op, data, offline=False, wrap=False): @@ -152,7 +151,7 @@ async def encryptJsonToMfs(self, op, obj, mfsPath): @ipfsOp async def decryptIpfsObject(self, op, data): - privKey = await self.__privateKey() + privKey = await self._privateKey() try: decrypted = await self.rsaExec.decryptData(BytesIO(data), privKey) if decrypted: @@ -189,12 +188,12 @@ async def decryptMfsJson(self, op, path): @ipfsOp async def pssSign(self, op, message): return await self.rsaExec.pssSign( - message, await self.__privateKey()) + message, await self._privateKey()) @ipfsOp async def pssSignImport(self, op, message, pin=False): signed = await self.rsaExec.pssSign( - message, await self.__privateKey()) + message, await self._privateKey()) if signed: try: @@ -233,8 +232,11 @@ async def privKeyUnlock(self, passphrase=None): self.debug(f'Key cache size: {len(self._privKeyCache)}') return key - @selfcachedcoromethod('_privKeyCache') - async def __privateKey(self, key=0): + async def _privateKey(self, key=0): + pKey = self._privKeyCache.get(key) + if pKey: + return pKey + return await self.rsaExec.importKey( await asyncReadFile(self.privKeyPath) ) diff --git a/galacteek/ipfs/p2pservices/didauth.py b/galacteek/ipfs/p2pservices/didauth.py index fd61de3d..5e0a449b 100644 --- a/galacteek/ipfs/p2pservices/didauth.py +++ b/galacteek/ipfs/p2pservices/didauth.py @@ -137,11 +137,11 @@ async def authPss(self, ipfsop, request): raise Exception(f'Invalid DIDAuth token {token}') except Exception as err: self.message(f'authPss error: {err}') - return await self.msgError() + return await self.msgError(error='Invalid token or schema') did = js.get('did') if not didIdentRe.match(did): - return await self.msgError() + return await self.msgError(error=f'Invalid DID: {did}') self.message( f'Received DID auth challenge request for DID: {did}') @@ -153,10 +153,16 @@ async def authPss(self, ipfsop, request): # one we currently use return await self.msgError(error='Invalid DID') - privKey = curProfile._didKeyStore._privateKeyForDid(did) - if not privKey: + rsaAgent = await currentIpid.rsaAgentGet(ipfsop) + if not rsaAgent: return await self.msgError() + privKey = await rsaAgent._privateKey() + + if not privKey: + return await self.msgError( + error=f'Invalid key for DID: {did}') + try: signed = await self.rsaExecutor.pssSign( js['challenge'].encode(), diff --git a/galacteek/ui/dialogs.py b/galacteek/ui/dialogs.py index 62f6295b..cc107ab1 100644 --- a/galacteek/ui/dialogs.py +++ b/galacteek/ui/dialogs.py @@ -1502,6 +1502,7 @@ def __init__(self, showCancel=False, parent=None): self.ui.useIpidPassphrase.stateChanged.connect(self.onUsePassphrase) self.ui.useIpidPassphrase.setCheckState(Qt.Checked) + self.ui.useIpidPassphrase.setCheckState(Qt.Unchecked) self.ui.ipidRsaPassphrase.setEchoMode(QLineEdit.Password) self.ui.ipidRsaPassphraseVerif.setEchoMode(QLineEdit.Password) diff --git a/galacteek/ui/peers.py b/galacteek/ui/peers.py index fe81c089..55f79e3c 100644 --- a/galacteek/ui/peers.py +++ b/galacteek/ui/peers.py @@ -331,7 +331,7 @@ def mimeData(self, indexes): for idx in indexes: idxPeer = self.index(idx.row(), 1, idx.parent()) if idxPeer.isValid(): - peer = self.data(idxPeer) + peer = self.data(idxPeer, Qt.DisplayRole) mimedata.setUrls([QUrl('galacteekpeer:{}'.format(peer))]) break @@ -388,7 +388,7 @@ def flags(self, index): def headerData(self, section, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: - return self.rootItem.data(section) + return self.rootItem.data(section, role) return None