Update saveIdentity
Add support new blockingApproval and nonblockingApproval arguments Populate the firstUse property on identity key records Return whether an existing record was overwritten. References https://github.com/WhisperSystems/Signal-Android/commit/39d4a7#diff-69ede72c549da6bcbcd959935995b7e9R45 // FREEBIE
This commit is contained in:
		
					parent
					
						
							
								4d4dd3341f
							
						
					
				
			
			
				commit
				
					
						82469713d2
					
				
			
		
					 2 changed files with 38 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -318,7 +318,7 @@
 | 
			
		|||
                });
 | 
			
		||||
            });
 | 
			
		||||
        },
 | 
			
		||||
        saveIdentity: function(identifier, publicKey) {
 | 
			
		||||
        saveIdentity: function(identifier, publicKey, blockingApproval, nonblockingApproval) {
 | 
			
		||||
            if (identifier === null || identifier === undefined) {
 | 
			
		||||
                throw new Error("Tried to put identity key for undefined/null key");
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -332,17 +332,41 @@
 | 
			
		|||
                    var oldpublicKey = identityKey.get('publicKey');
 | 
			
		||||
                    if (!oldpublicKey) {
 | 
			
		||||
                        // Lookup failed, or the current key was removed, so save this one.
 | 
			
		||||
                        identityKey.save({publicKey: publicKey}).then(resolve);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        // Key exists, if it matches do nothing, else throw
 | 
			
		||||
                        if (equalArrayBuffers(oldpublicKey, publicKey)) {
 | 
			
		||||
                            resolve();
 | 
			
		||||
                        } else {
 | 
			
		||||
                            reject(new Error("Attempted to overwrite a different identity key"));
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                        console.log("Saving new identity...");
 | 
			
		||||
                        identityKey.save({
 | 
			
		||||
                            publicKey           : publicKey,
 | 
			
		||||
                            firstUse            : true,
 | 
			
		||||
                            timestamp           : Date.now(),
 | 
			
		||||
                            blockingApproval    : blockingApproval,
 | 
			
		||||
                            nonblockingApproval : nonblockingApproval,
 | 
			
		||||
                        }).then(function() {
 | 
			
		||||
                            resolve(false);
 | 
			
		||||
                        });
 | 
			
		||||
                    } else if (!equalArrayBuffers(oldpublicKey, publicKey)) {
 | 
			
		||||
                        console.log("Replacing existing identity...");
 | 
			
		||||
                        identityKey.save({
 | 
			
		||||
                            publicKey           : publicKey,
 | 
			
		||||
                            firstUse            : false,
 | 
			
		||||
                            timestamp           : Date.now(),
 | 
			
		||||
                            blockingApproval    : blockingApproval,
 | 
			
		||||
                            nonblockingApproval : nonblockingApproval,
 | 
			
		||||
                        }).then(function() {
 | 
			
		||||
                            this.trigger('keychange', identifier);
 | 
			
		||||
                            resolve(true);
 | 
			
		||||
                        }.bind(this));
 | 
			
		||||
                    } else if (this.isBlockingApprovalRequired(identityKey) || this.isNonBlockingApprovalRequired(identityKey)) {
 | 
			
		||||
                        console.log("Setting approval status...");
 | 
			
		||||
                        identityKey.save({
 | 
			
		||||
                            blockingApproval    : blockingApproval,
 | 
			
		||||
                            nonblockingApproval : nonblockingApproval,
 | 
			
		||||
                        }).then(function() {
 | 
			
		||||
                            resolve(false);
 | 
			
		||||
                        });
 | 
			
		||||
                    } else {
 | 
			
		||||
                        resolve(false);
 | 
			
		||||
                    }
 | 
			
		||||
                }.bind(this));
 | 
			
		||||
            }.bind(this));
 | 
			
		||||
        },
 | 
			
		||||
        isBlockingApprovalRequired: function(identityKey) {
 | 
			
		||||
            return (!identityKey.get('firstUse')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,16 +43,14 @@ describe("SignalProtocolStore", function() {
 | 
			
		|||
                });
 | 
			
		||||
            }).then(done,done);
 | 
			
		||||
        });
 | 
			
		||||
        it('rejects on key change', function(done) {
 | 
			
		||||
        it('returns true on key change', function(done) {
 | 
			
		||||
            var newIdentity = libsignal.crypto.getRandomBytes(33);
 | 
			
		||||
            store.saveIdentity(identifier, testKey.pubKey).then(function() {
 | 
			
		||||
                store.saveIdentity(identifier, newIdentity).then(function() {
 | 
			
		||||
                    done(new Error('Allowed to overwrite identity key'));
 | 
			
		||||
                }).catch(function(e) {
 | 
			
		||||
                    assert(e instanceof Error);
 | 
			
		||||
                store.saveIdentity(identifier, newIdentity).then(function(changed) {
 | 
			
		||||
                    assert.isTrue(changed);
 | 
			
		||||
                    done();
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
            }).catch(done);
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
    describe('isTrustedIdentity', function() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue