forked from signalapp/Signal-iOS
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
14 deletions.
There are no files selected for viewing
Submodule Pods
updated
from 0018c3 to 1ab1cb
29 changes: 25 additions & 4 deletions
29
SignalServiceKit/src/Loki/Multidevice/LokiPairingAuthorisation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
@objc(LKPairingAuthorisation) | ||
public final class LokiPairingAuthorisation : NSObject { | ||
public final class LokiPairingAuthorisation : NSObject, NSCoding { | ||
@objc public let primaryDevicePubKey: String | ||
@objc public let secondaryDevicePubKey: String | ||
@objc public let isGranted: Bool | ||
@objc public let requestSignature: Data? | ||
@objc public let grantSignature: Data? | ||
|
||
@objc public init(primaryDevicePubKey: String, secondaryDevicePubKey: String, isGranted: Bool, requestSignature: Data? = nil, grantSignature: Data? = nil) { | ||
@objc public var isGranted: Bool { | ||
return grantSignature != nil | ||
} | ||
|
||
@objc public init(primaryDevicePubKey: String, secondaryDevicePubKey: String, requestSignature: Data? = nil, grantSignature: Data? = nil) { | ||
self.primaryDevicePubKey = primaryDevicePubKey | ||
self.secondaryDevicePubKey = secondaryDevicePubKey | ||
self.isGranted = isGranted | ||
self.requestSignature = requestSignature | ||
self.grantSignature = grantSignature | ||
} | ||
|
||
public convenience init?(coder aDecoder: NSCoder) { | ||
guard let primaryDevicePubKey = aDecoder.decodeObject(forKey: "primaryDevicePubKey") as? String, | ||
let secondaryDevicePubKey = aDecoder.decodeObject(forKey: "secondaryDevicePubKey") as? String else { | ||
return nil | ||
} | ||
|
||
let requestSignature = aDecoder.decodeObject(forKey: "requestSignature") as? Data | ||
let grantSignature = aDecoder.decodeObject(forKey: "grantSignature") as? Data | ||
|
||
self.init(primaryDevicePubKey: primaryDevicePubKey, secondaryDevicePubKey: secondaryDevicePubKey, requestSignature: requestSignature, grantSignature: grantSignature) | ||
} | ||
|
||
public func encode(with aCoder: NSCoder) { | ||
aCoder.encode(primaryDevicePubKey, forKey: "primaryDevicePubKey") | ||
aCoder.encode(secondaryDevicePubKey, forKey: "secondaryDevicePubKey") | ||
aCoder.encode(requestSignature, forKey: "requestSignature") | ||
aCoder.encode(grantSignature, forKey: "grantSignature") | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
SignalServiceKit/src/Loki/Multidevice/OWSPrimaryStorage+Multidevice.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
extension OWSPrimaryStorage { | ||
private func getCollection(for primaryDevice: String) -> String { | ||
return "LokiMultiDevice-\(primaryDevice)" | ||
} | ||
|
||
public func getAuthorisation(forSecondaryDevice secondaryDevice: String, with transaction: YapDatabaseReadTransaction) -> LokiPairingAuthorisation? { | ||
let query = YapDatabaseQuery(string: "WHERE \(PairingAuthorisationsIndex.secondaryDevicePubKey) = ?", parameters: [secondaryDevice]) | ||
let authorisations = PairingAuthorisationsIndex.getPairingAuthorisations(with: query, transaction: transaction) | ||
|
||
// This should never be the case | ||
if (authorisations.count > 1) { owsFailDebug("[Loki][Multidevice] Found multiple authorisations for secondary device: \(secondaryDevice)") } | ||
|
||
return authorisations.first | ||
} | ||
|
||
public func createOrUpdatePairingAuthorisation(_ authorisation: LokiPairingAuthorisation, with transaction: YapDatabaseReadWriteTransaction) { | ||
// iOS makes this easy, we can group all authorizations into the primary device collection | ||
// Then we associate an authorisation with the secondary device key | ||
transaction.setObject(authorisation, forKey: authorisation.secondaryDevicePubKey, inCollection: getCollection(for: authorisation.primaryDevicePubKey)) | ||
} | ||
|
||
public func getSecondaryDevices(forPrimaryDevice primaryDevice: String, with transaction: YapDatabaseReadTransaction) -> [String] { | ||
// primary device collection should have secondary devices as its keys | ||
return transaction.allKeys(inCollection: getCollection(for: primaryDevice)) | ||
} | ||
|
||
public func getPrimaryDevice(forSecondaryDevice secondaryDevice: String, with transaction: YapDatabaseReadTransaction) -> String? { | ||
let authorisation = getAuthorisation(forSecondaryDevice: secondaryDevice, with: transaction) | ||
return authorisation?.primaryDevicePubKey | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters