Start integrating LokiDeviceLinkingSession

This commit is contained in:
Niels Andriesse 2019-09-20 14:26:29 +10:00
parent d5d6d65b5c
commit da2d18f0fc
6 changed files with 53 additions and 26 deletions

View File

@ -60,7 +60,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
static NSTimeInterval launchStartedAt;
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@interface AppDelegate () <UNUserNotificationCenterDelegate, LKDeviceLinkingSessionDelegate>
@property (nonatomic) BOOL hasInitialRootViewController;
@property (nonatomic) BOOL areVersionMigrationsComplete;
@ -72,6 +72,7 @@ static NSTimeInterval launchStartedAt;
@property (nonatomic) LKGroupChatPoller *lokiPublicChatPoller;
@property (nonatomic) LKRSSFeedPoller *lokiNewsFeedPoller;
@property (nonatomic) LKRSSFeedPoller *lokiMessengerUpdatesFeedPoller;
@property (nonatomic) LKDeviceLinkingSession *lokiDeviceLinkingSession;
@end
@ -1638,4 +1639,21 @@ static NSTimeInterval launchStartedAt;
}
}
- (void)startListeningForLinkingRequests
{
[self.lokiDeviceLinkingSession stopListeningForLinkingRequests];
self.lokiDeviceLinkingSession = [[LKDeviceLinkingSession alloc] initWithDelegate:self];
[self.lokiDeviceLinkingSession startListeningForLinkingRequests];
}
- (void)requestUserAuthorizationFor:(LKDeviceLink *)deviceLink
{
}
- (void)handleDeviceLinkingSessionTimeout
{
}
@end

View File

@ -241,6 +241,7 @@
[section addItem:[OWSTableItem itemWithTitle:NSLocalizedString(@"Share Public Key", @"") actionBlock:^{ [weakSelf sharePublicKey]; }]];
[section addItem:[OWSTableItem itemWithTitle:NSLocalizedString(@"Show QR Code", @"") actionBlock:^{ [weakSelf showQRCode]; }]];
[section addItem:[OWSTableItem itemWithTitle:NSLocalizedString(@"Link Device", @"") actionBlock:^{ [weakSelf linkDevice]; }]];
[section addItem:[OWSTableItem itemWithTitle:NSLocalizedString(@"Show Seed", @"") actionBlock:^{ [weakSelf showSeed]; }]];
[section addItem:[OWSTableItem itemWithTitle:NSLocalizedString(@"Clear All Data", @"") actionBlock:^{ [weakSelf clearAllData]; }]];
@ -511,6 +512,11 @@
[self.navigationController pushViewController:qrCodeVC animated:YES];
}
- (void)linkDevice
{
}
- (void)showSeed
{
NSString *title = NSLocalizedString(@"Your Seed", @"");

View File

@ -2621,3 +2621,4 @@
"Please enter the public key of the person you'd like to message." = "Please enter the public key of the person you'd like to message.";
"Loki Messenger is currently in beta. For development purposes the beta version collects basic usage statistics and crash logs. In addition, the beta version doesn't provide full privacy and shouldn't be used to transmit sensitive information." = "Loki Messenger is currently in beta. For development purposes the beta version collects basic usage statistics and crash logs. In addition, the beta version doesn't provide full privacy and shouldn't be used to transmit sensitive information.";
"Copy Public Key" = "Copy Public Key";
"Link Device" = "Link Device";

View File

@ -1,75 +1,75 @@
@objc(LKDeviceLink)
public final class LokiDeviceLink : NSObject, NSCoding {
public let master: Device
public let slave: Device
@objc public let master: Device
@objc public let slave: Device
public var isAuthorized: Bool { return master.signature != nil }
@objc public var isAuthorized: Bool { return master.signature != nil }
// MARK: Types
@objc(LKDevice)
public final class Device : NSObject, NSCoding {
public let hexEncodedPublicKey: String
public let signature: Data?
@objc public let hexEncodedPublicKey: String
@objc public let signature: Data?
public init(hexEncodedPublicKey: String, signature: Data? = nil) {
@objc public init(hexEncodedPublicKey: String, signature: Data? = nil) {
self.hexEncodedPublicKey = hexEncodedPublicKey
self.signature = signature
}
public init?(coder: NSCoder) {
@objc public init?(coder: NSCoder) {
hexEncodedPublicKey = coder.decodeObject(forKey: "hexEncodedPublicKey") as! String
signature = coder.decodeObject(forKey: "signature") as! Data?
}
public func encode(with coder: NSCoder) {
@objc public func encode(with coder: NSCoder) {
coder.encode(hexEncodedPublicKey, forKey: "hexEncodedPublicKey")
if let signature = signature { coder.encode(signature, forKey: "signature") }
}
public override func isEqual(_ other: Any?) -> Bool {
@objc public override func isEqual(_ other: Any?) -> Bool {
guard let other = other as? Device else { return false }
return hexEncodedPublicKey == other.hexEncodedPublicKey && signature == other.signature
}
override public var hash: Int { // Override NSObject.hash and not Hashable.hashValue or Hashable.hash(into:)
@objc override public var hash: Int { // Override NSObject.hash and not Hashable.hashValue or Hashable.hash(into:)
var result = hexEncodedPublicKey.hashValue
if let signature = signature { result = result ^ signature.hashValue }
return result
}
override public var description: String { return hexEncodedPublicKey }
@objc override public var description: String { return hexEncodedPublicKey }
}
// MARK: Lifecycle
public init(between master: Device, and slave: Device) {
@objc public init(between master: Device, and slave: Device) {
self.master = master
self.slave = slave
}
// MARK: Coding
public init?(coder: NSCoder) {
@objc public init?(coder: NSCoder) {
master = coder.decodeObject(forKey: "master") as! Device
slave = coder.decodeObject(forKey: "slave") as! Device
super.init()
}
public func encode(with coder: NSCoder) {
@objc public func encode(with coder: NSCoder) {
coder.encode(master, forKey: "master")
coder.encode(slave, forKey: "slave")
}
// MARK: Equality
override public func isEqual(_ other: Any?) -> Bool {
@objc override public func isEqual(_ other: Any?) -> Bool {
guard let other = other as? LokiDeviceLink else { return false }
return master == other.master && slave == other.slave
}
// MARK: Hashing
override public var hash: Int { // Override NSObject.hash and not Hashable.hashValue or Hashable.hash(into:)
@objc override public var hash: Int { // Override NSObject.hash and not Hashable.hashValue or Hashable.hash(into:)
return master.hash ^ slave.hash
}
// MARK: Description
override public var description: String { return "\(master) - \(slave)" }
@objc override public var description: String { return "\(master) - \(slave)" }
}

View File

@ -1,12 +1,13 @@
import PromiseKit
@objc (LKDeviceLinkingSession)
final class LokiDeviceLinkingSession : NSObject {
private let delegate: LokiDeviceLinkingSessionDelegate
private var timer: Timer?
public var isListeningForLinkingRequests = false
@objc public var isListeningForLinkingRequests = false
// MARK: Lifecycle
public init(delegate: LokiDeviceLinkingSessionDelegate) {
@objc public init(delegate: LokiDeviceLinkingSessionDelegate) {
self.delegate = delegate
}
@ -14,7 +15,7 @@ final class LokiDeviceLinkingSession : NSObject {
private let listeningTimeout: TimeInterval = 60
// MARK: Public API
public func startListeningForLinkingRequests() {
@objc public func startListeningForLinkingRequests() {
isListeningForLinkingRequests = true
timer = Timer.scheduledTimer(withTimeInterval: listeningTimeout, repeats: false) { [weak self] timer in
guard let self = self else { return }
@ -23,7 +24,7 @@ final class LokiDeviceLinkingSession : NSObject {
}
}
public func processLinkingRequest(from slaveHexEncodedPublicKey: String, with slaveSignature: Data) {
@objc public func processLinkingRequest(from slaveHexEncodedPublicKey: String, with slaveSignature: Data) {
guard isListeningForLinkingRequests else { return }
stopListeningForLinkingRequests()
let master = LokiDeviceLink.Device(hexEncodedPublicKey: OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey)
@ -33,11 +34,11 @@ final class LokiDeviceLinkingSession : NSObject {
delegate.requestUserAuthorization(for: deviceLink)
}
public func authorizeDeviceLink(_ deviceLink: LokiDeviceLink) {
@objc public func authorizeDeviceLink(_ deviceLink: LokiDeviceLink) {
// TODO: Send a device link authorized message
}
public func stopListeningForLinkingRequests() {
@objc public func stopListeningForLinkingRequests() {
timer?.invalidate()
timer = nil
isListeningForLinkingRequests = false

View File

@ -1,6 +1,7 @@
@objc(LKDeviceLinkingSessionDelegate)
public protocol LokiDeviceLinkingSessionDelegate {
func requestUserAuthorization(for deviceLink: LokiDeviceLink)
func handleDeviceLinkingSessionTimeout()
@objc func requestUserAuthorization(for deviceLink: LokiDeviceLink)
@objc func handleDeviceLinkingSessionTimeout()
}