Partially implement storage API
This commit is contained in:
parent
77cd19d501
commit
8b0141596c
|
@ -563,7 +563,7 @@
|
||||||
B8162F0522892C5F00D46544 /* FriendRequestViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */; };
|
B8162F0522892C5F00D46544 /* FriendRequestViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */; };
|
||||||
B821F2F82272CED3002C88C0 /* DisplayNameVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */; };
|
B821F2F82272CED3002C88C0 /* DisplayNameVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */; };
|
||||||
B821F2FA2272CEEE002C88C0 /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F92272CEEE002C88C0 /* SeedVC.swift */; };
|
B821F2FA2272CEEE002C88C0 /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F92272CEEE002C88C0 /* SeedVC.swift */; };
|
||||||
B8258493230FA5E9001B41CB /* ScanQRCodeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */; };
|
B8258493230FA5E9001B41CB /* ScanQRCodeVC.m in Sources */ = {isa = PBXBuildFile; fileRef = B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */; };
|
||||||
B82584A02315024B001B41CB /* RSSFeedPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B825849F2315024B001B41CB /* RSSFeedPoller.swift */; };
|
B82584A02315024B001B41CB /* RSSFeedPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B825849F2315024B001B41CB /* RSSFeedPoller.swift */; };
|
||||||
B845B4D4230CD09100D759F0 /* GroupChatPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B845B4D3230CD09000D759F0 /* GroupChatPoller.swift */; };
|
B845B4D4230CD09100D759F0 /* GroupChatPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B845B4D3230CD09000D759F0 /* GroupChatPoller.swift */; };
|
||||||
B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; };
|
B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; };
|
||||||
|
@ -1371,8 +1371,8 @@
|
||||||
B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FriendRequestViewDelegate.swift; sourceTree = "<group>"; };
|
B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FriendRequestViewDelegate.swift; sourceTree = "<group>"; };
|
||||||
B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayNameVC.swift; sourceTree = "<group>"; };
|
B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayNameVC.swift; sourceTree = "<group>"; };
|
||||||
B821F2F92272CEEE002C88C0 /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = "<group>"; };
|
B821F2F92272CEEE002C88C0 /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = "<group>"; };
|
||||||
B8258491230FA5DA001B41CB /* ScanQRCodeViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanQRCodeViewController.h; sourceTree = "<group>"; };
|
B8258491230FA5DA001B41CB /* ScanQRCodeVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanQRCodeVC.h; sourceTree = "<group>"; };
|
||||||
B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanQRCodeViewController.m; sourceTree = "<group>"; };
|
B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanQRCodeVC.m; sourceTree = "<group>"; };
|
||||||
B825849F2315024B001B41CB /* RSSFeedPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSSFeedPoller.swift; sourceTree = "<group>"; };
|
B825849F2315024B001B41CB /* RSSFeedPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSSFeedPoller.swift; sourceTree = "<group>"; };
|
||||||
B845B4D3230CD09000D759F0 /* GroupChatPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupChatPoller.swift; sourceTree = "<group>"; };
|
B845B4D3230CD09000D759F0 /* GroupChatPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupChatPoller.swift; sourceTree = "<group>"; };
|
||||||
B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = "<group>"; };
|
B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = "<group>"; };
|
||||||
|
@ -2649,8 +2649,8 @@
|
||||||
B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */,
|
B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */,
|
||||||
24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */,
|
24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */,
|
||||||
B89841E222B7579F00B1BDC6 /* NewConversationVC.swift */,
|
B89841E222B7579F00B1BDC6 /* NewConversationVC.swift */,
|
||||||
B8258491230FA5DA001B41CB /* ScanQRCodeViewController.h */,
|
B8258491230FA5DA001B41CB /* ScanQRCodeVC.h */,
|
||||||
B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */,
|
B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */,
|
||||||
);
|
);
|
||||||
path = Loki;
|
path = Loki;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
@ -3922,7 +3922,7 @@
|
||||||
B86BD08423399ACF000F5AE3 /* Modal.swift in Sources */,
|
B86BD08423399ACF000F5AE3 /* Modal.swift in Sources */,
|
||||||
34D1F0861F8678AA0066283D /* ConversationViewController.m in Sources */,
|
34D1F0861F8678AA0066283D /* ConversationViewController.m in Sources */,
|
||||||
3427C64320F500E000EEC730 /* OWSMessageTimerView.m in Sources */,
|
3427C64320F500E000EEC730 /* OWSMessageTimerView.m in Sources */,
|
||||||
B8258493230FA5E9001B41CB /* ScanQRCodeViewController.m in Sources */,
|
B8258493230FA5E9001B41CB /* ScanQRCodeVC.m in Sources */,
|
||||||
B90418E6183E9DD40038554A /* DateUtil.m in Sources */,
|
B90418E6183E9DD40038554A /* DateUtil.m in Sources */,
|
||||||
3448E15E221333F5004B052E /* OnboardingController.swift in Sources */,
|
3448E15E221333F5004B052E /* OnboardingController.swift in Sources */,
|
||||||
340FC8BD204DAC8D007AEB0F /* ShowGroupMembersViewController.m in Sources */,
|
340FC8BD204DAC8D007AEB0F /* ShowGroupMembersViewController.m in Sources */,
|
||||||
|
|
|
@ -81,7 +81,7 @@ final class NewConversationVC : OWSViewController, OWSQRScannerDelegate {
|
||||||
@objc private func scanQRCode() {
|
@objc private func scanQRCode() {
|
||||||
ows_ask(forCameraPermissions: { [weak self] hasCameraAccess in
|
ows_ask(forCameraPermissions: { [weak self] hasCameraAccess in
|
||||||
if hasCameraAccess {
|
if hasCameraAccess {
|
||||||
let scanQRCodeVC = ScanQRCodeViewController()
|
let scanQRCodeVC = ScanQRCodeVC()
|
||||||
scanQRCodeVC.delegate = self
|
scanQRCodeVC.delegate = self
|
||||||
self?.navigationController!.pushViewController(scanQRCodeVC, animated: true)
|
self?.navigationController!.pushViewController(scanQRCodeVC, animated: true)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface ScanQRCodeViewController : OWSViewController
|
@interface ScanQRCodeVC : OWSViewController
|
||||||
|
|
||||||
@property (nonatomic, weak) UIViewController<OWSQRScannerDelegate> *delegate;
|
@property (nonatomic, weak) UIViewController<OWSQRScannerDelegate> *delegate;
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
#import "ScanQRCodeViewController.h"
|
#import "ScanQRCodeVC.h"
|
||||||
#import "Session-Swift.h"
|
#import "Session-Swift.h"
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface ScanQRCodeViewController ()
|
@interface ScanQRCodeVC ()
|
||||||
|
|
||||||
@property (nonatomic) OWSQRCodeScanningViewController *qrCodeScanningVC;
|
@property (nonatomic) OWSQRCodeScanningViewController *qrCodeScanningVC;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation ScanQRCodeViewController
|
@implementation ScanQRCodeVC
|
||||||
|
|
||||||
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
|
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
|
||||||
|
|
|
@ -44,7 +44,7 @@
|
||||||
#import "PrivacySettingsTableViewController.h"
|
#import "PrivacySettingsTableViewController.h"
|
||||||
#import "ProfileViewController.h"
|
#import "ProfileViewController.h"
|
||||||
#import "RemoteVideoView.h"
|
#import "RemoteVideoView.h"
|
||||||
#import "ScanQRCodeViewController.h"
|
#import "ScanQRCodeVC.h"
|
||||||
#import "SignalApp.h"
|
#import "SignalApp.h"
|
||||||
#import "UIViewController+Permissions.h"
|
#import "UIViewController+Permissions.h"
|
||||||
#import "ViewControllerUtils.h"
|
#import "ViewControllerUtils.h"
|
||||||
|
|
|
@ -2632,3 +2632,5 @@
|
||||||
"Authorize" = "Authorize";
|
"Authorize" = "Authorize";
|
||||||
"Enter the Other Device's Public Key" = "Enter the Other Device's Public Key";
|
"Enter the Other Device's Public Key" = "Enter the Other Device's Public Key";
|
||||||
"This is your personal secret. It can be used to restore your account if you lose access, or to migrate to a new device." = "This is your personal secret. It can be used to restore your account if you lose access, or to migrate to a new device.";
|
"This is your personal secret. It can be used to restore your account if you lose access, or to migrate to a new device." = "This is your personal secret. It can be used to restore your account if you lose access, or to migrate to a new device.";
|
||||||
|
"Device Link Authorized" = "Device Link Authorized";
|
||||||
|
"Your device has been linked successfully" = "Your device has been linked successfully";
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
import PromiseKit
|
import PromiseKit
|
||||||
|
|
||||||
@objc(LKGroupChatAPI)
|
@objc(LKGroupChatAPI)
|
||||||
public final class LokiGroupChatAPI : NSObject {
|
public final class LokiGroupChatAPI : LokiDotNetAPI {
|
||||||
private static let storage = OWSPrimaryStorage.shared()
|
|
||||||
|
|
||||||
private static var moderators: [String:[UInt64:Set<String>]] = [:] // Server URL to (channel ID to set of moderator IDs)
|
private static var moderators: [String:[UInt64:Set<String>]] = [:] // Server URL to (channel ID to set of moderator IDs)
|
||||||
|
|
||||||
// MARK: Settings
|
// MARK: Settings
|
||||||
|
@ -24,38 +22,11 @@ public final class LokiGroupChatAPI : NSObject {
|
||||||
return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous"
|
return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous"
|
||||||
}
|
}
|
||||||
|
|
||||||
private static var userKeyPair: ECKeyPair {
|
|
||||||
return OWSIdentityManager.shared().identityKeyPair()!
|
|
||||||
}
|
|
||||||
|
|
||||||
private static var userHexEncodedPublicKey: String {
|
|
||||||
return userKeyPair.hexEncodedPublicKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: Error
|
|
||||||
public enum Error : Swift.Error {
|
|
||||||
case parsingFailed, decryptionFailed
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: Database
|
// MARK: Database
|
||||||
private static let authTokenCollection = "LokiGroupChatAuthTokenCollection"
|
override internal class var authTokenCollection: String { "LokiGroupChatAuthTokenCollection" }
|
||||||
private static let lastMessageServerIDCollection = "LokiGroupChatLastMessageServerIDCollection"
|
private static let lastMessageServerIDCollection = "LokiGroupChatLastMessageServerIDCollection"
|
||||||
private static let lastDeletionServerIDCollection = "LokiGroupChatLastDeletionServerIDCollection"
|
private static let lastDeletionServerIDCollection = "LokiGroupChatLastDeletionServerIDCollection"
|
||||||
|
|
||||||
private static func getAuthTokenFromDatabase(for server: String) -> String? {
|
|
||||||
var result: String? = nil
|
|
||||||
storage.dbReadConnection.read { transaction in
|
|
||||||
result = transaction.object(forKey: server, inCollection: authTokenCollection) as! String?
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func setAuthToken(for server: String, to newValue: String) {
|
|
||||||
storage.dbReadWriteConnection.readWrite { transaction in
|
|
||||||
transaction.setObject(newValue, forKey: server, inCollection: authTokenCollection)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func getLastMessageServerID(for group: UInt64, on server: String) -> UInt? {
|
private static func getLastMessageServerID(for group: UInt64, on server: String) -> UInt? {
|
||||||
var result: UInt? = nil
|
var result: UInt? = nil
|
||||||
storage.dbReadConnection.read { transaction in
|
storage.dbReadConnection.read { transaction in
|
||||||
|
@ -83,53 +54,6 @@ public final class LokiGroupChatAPI : NSObject {
|
||||||
transaction.setObject(newValue, forKey: "\(server).\(group)", inCollection: lastDeletionServerIDCollection)
|
transaction.setObject(newValue, forKey: "\(server).\(group)", inCollection: lastDeletionServerIDCollection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Lifecycle
|
|
||||||
override private init() { }
|
|
||||||
|
|
||||||
// MARK: Private API
|
|
||||||
private static func requestNewAuthToken(for server: String) -> Promise<String> {
|
|
||||||
print("[Loki] Requesting group chat auth token for server: \(server).")
|
|
||||||
let queryParameters = "pubKey=\(userHexEncodedPublicKey)"
|
|
||||||
let url = URL(string: "\(server)/loki/v1/get_challenge?\(queryParameters)")!
|
|
||||||
let request = TSRequest(url: url)
|
|
||||||
return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in
|
|
||||||
guard let json = rawResponse as? JSON, let base64EncodedChallenge = json["cipherText64"] as? String, let base64EncodedServerPublicKey = json["serverPubKey64"] as? String,
|
|
||||||
let challenge = Data(base64Encoded: base64EncodedChallenge), var serverPublicKey = Data(base64Encoded: base64EncodedServerPublicKey) else {
|
|
||||||
throw Error.parsingFailed
|
|
||||||
}
|
|
||||||
// Discard the "05" prefix if needed
|
|
||||||
if (serverPublicKey.count == 33) {
|
|
||||||
let hexEncodedServerPublicKey = serverPublicKey.hexadecimalString
|
|
||||||
serverPublicKey = Data.data(fromHex: hexEncodedServerPublicKey.substring(from: 2))!
|
|
||||||
}
|
|
||||||
// The challenge is prefixed by the 16 bit IV
|
|
||||||
guard let tokenAsData = try? DiffieHellman.decrypt(challenge, publicKey: serverPublicKey, privateKey: userKeyPair.privateKey),
|
|
||||||
let token = String(bytes: tokenAsData, encoding: .utf8) else {
|
|
||||||
throw Error.decryptionFailed
|
|
||||||
}
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func submitAuthToken(_ token: String, for server: String) -> Promise<String> {
|
|
||||||
print("[Loki] Submitting group chat auth token for server: \(server).")
|
|
||||||
let url = URL(string: "\(server)/loki/v1/submit_challenge")!
|
|
||||||
let parameters = [ "pubKey" : userHexEncodedPublicKey, "token" : token ]
|
|
||||||
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
|
||||||
return TSNetworkManager.shared().makePromise(request: request).map { _ in token }
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func getAuthToken(for server: String) -> Promise<String> {
|
|
||||||
if let token = getAuthTokenFromDatabase(for: server) {
|
|
||||||
return Promise.value(token)
|
|
||||||
} else {
|
|
||||||
return requestNewAuthToken(for: server).then { submitAuthToken($0, for: server) }.map { token -> String in
|
|
||||||
setAuthToken(for: server, to: token)
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: Public API
|
// MARK: Public API
|
||||||
public static func getMessages(for group: UInt64, on server: String) -> Promise<[LokiGroupMessage]> {
|
public static func getMessages(for group: UInt64, on server: String) -> Promise<[LokiGroupMessage]> {
|
||||||
|
|
|
@ -3,12 +3,10 @@ import PromiseKit
|
||||||
@objc(LKAPI)
|
@objc(LKAPI)
|
||||||
public final class LokiAPI : NSObject {
|
public final class LokiAPI : NSObject {
|
||||||
private static var lastDeviceLinkUpdate: [String:Date] = [:] // Hex encoded public key to date
|
private static var lastDeviceLinkUpdate: [String:Date] = [:] // Hex encoded public key to date
|
||||||
internal static let storage = OWSPrimaryStorage.shared()
|
|
||||||
|
|
||||||
// MARK: Convenience
|
// MARK: Convenience
|
||||||
internal static var userHexEncodedPublicKey: String {
|
internal static let storage = OWSPrimaryStorage.shared()
|
||||||
return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
|
internal static let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: Settings
|
// MARK: Settings
|
||||||
private static let version = "v1"
|
private static let version = "v1"
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
import PromiseKit
|
||||||
|
|
||||||
|
public class LokiDotNetAPI : NSObject {
|
||||||
|
|
||||||
|
// MARK: Convenience
|
||||||
|
private static let userKeyPair = OWSIdentityManager.shared().identityKeyPair()!
|
||||||
|
internal static let storage = OWSPrimaryStorage.shared()
|
||||||
|
internal static let userHexEncodedPublicKey = userKeyPair.hexEncodedPublicKey
|
||||||
|
|
||||||
|
// MARK: Error
|
||||||
|
public enum Error : Swift.Error {
|
||||||
|
case parsingFailed, decryptionFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Database
|
||||||
|
/// To be overridden by subclasses.
|
||||||
|
internal class var authTokenCollection: String { preconditionFailure("authTokenCollection is abstract and must be overridden.") }
|
||||||
|
|
||||||
|
private static func getAuthTokenFromDatabase(for server: String) -> String? {
|
||||||
|
var result: String? = nil
|
||||||
|
storage.dbReadConnection.read { transaction in
|
||||||
|
result = transaction.object(forKey: server, inCollection: authTokenCollection) as! String?
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func setAuthToken(for server: String, to newValue: String) {
|
||||||
|
storage.dbReadWriteConnection.readWrite { transaction in
|
||||||
|
transaction.setObject(newValue, forKey: server, inCollection: authTokenCollection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Lifecycle
|
||||||
|
override private init() { }
|
||||||
|
|
||||||
|
// MARK: Internal API
|
||||||
|
internal static func getAuthToken(for server: String) -> Promise<String> {
|
||||||
|
if let token = getAuthTokenFromDatabase(for: server) {
|
||||||
|
return Promise.value(token)
|
||||||
|
} else {
|
||||||
|
return requestNewAuthToken(for: server).then { submitAuthToken($0, for: server) }.map { token -> String in
|
||||||
|
setAuthToken(for: server, to: token)
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Private API
|
||||||
|
private static func requestNewAuthToken(for server: String) -> Promise<String> {
|
||||||
|
print("[Loki] Requesting auth token for server: \(server).")
|
||||||
|
let queryParameters = "pubKey=\(userHexEncodedPublicKey)"
|
||||||
|
let url = URL(string: "\(server)/loki/v1/get_challenge?\(queryParameters)")!
|
||||||
|
let request = TSRequest(url: url)
|
||||||
|
return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in
|
||||||
|
guard let json = rawResponse as? JSON, let base64EncodedChallenge = json["cipherText64"] as? String, let base64EncodedServerPublicKey = json["serverPubKey64"] as? String,
|
||||||
|
let challenge = Data(base64Encoded: base64EncodedChallenge), var serverPublicKey = Data(base64Encoded: base64EncodedServerPublicKey) else {
|
||||||
|
throw Error.parsingFailed
|
||||||
|
}
|
||||||
|
// Discard the "05" prefix if needed
|
||||||
|
if (serverPublicKey.count == 33) {
|
||||||
|
let hexEncodedServerPublicKey = serverPublicKey.hexadecimalString
|
||||||
|
serverPublicKey = Data.data(fromHex: hexEncodedServerPublicKey.substring(from: 2))!
|
||||||
|
}
|
||||||
|
// The challenge is prefixed by the 16 bit IV
|
||||||
|
guard let tokenAsData = try? DiffieHellman.decrypt(challenge, publicKey: serverPublicKey, privateKey: userKeyPair.privateKey),
|
||||||
|
let token = String(bytes: tokenAsData, encoding: .utf8) else {
|
||||||
|
throw Error.decryptionFailed
|
||||||
|
}
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func submitAuthToken(_ token: String, for server: String) -> Promise<String> {
|
||||||
|
print("[Loki] Submitting auth token for server: \(server).")
|
||||||
|
let url = URL(string: "\(server)/loki/v1/submit_challenge")!
|
||||||
|
let parameters = [ "pubKey" : userHexEncodedPublicKey, "token" : token ]
|
||||||
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
||||||
|
return TSNetworkManager.shared().makePromise(request: request).map { _ in token }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,25 +1,54 @@
|
||||||
import PromiseKit
|
import PromiseKit
|
||||||
|
|
||||||
@objc(LKStorageAPI)
|
@objc(LKStorageAPI)
|
||||||
public final class LokiStorageAPI : NSObject {
|
public final class LokiStorageAPI : LokiDotNetAPI {
|
||||||
|
|
||||||
// MARK: Lifecycle
|
// MARK: Settings
|
||||||
override private init() { }
|
private static let server = ""
|
||||||
|
|
||||||
|
// MARK: Database
|
||||||
|
override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" }
|
||||||
|
|
||||||
// MARK: Public API
|
// MARK: Public API
|
||||||
|
/// Adds the given device link to the user's device mapping on the server.
|
||||||
public static func addDeviceLink(_ deviceLink: DeviceLink) -> Promise<Void> {
|
public static func addDeviceLink(_ deviceLink: DeviceLink) -> Promise<Void> {
|
||||||
// Adds the given device link to the user's device mapping on the server
|
var deviceLinks: Set<DeviceLink> = []
|
||||||
notImplemented()
|
storage.dbReadConnection.read { transaction in
|
||||||
|
deviceLinks = storage.getDeviceLinks(for: userHexEncodedPublicKey, in: transaction)
|
||||||
|
}
|
||||||
|
deviceLinks.insert(deviceLink)
|
||||||
|
return setDeviceLinks(deviceLinks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the given device link from the user's device mapping on the server.
|
||||||
public static func removeDeviceLink(_ deviceLink: DeviceLink) -> Promise<Void> {
|
public static func removeDeviceLink(_ deviceLink: DeviceLink) -> Promise<Void> {
|
||||||
// Removes the given device link from the user's device mapping on the server
|
var deviceLinks: Set<DeviceLink> = []
|
||||||
notImplemented()
|
storage.dbReadConnection.read { transaction in
|
||||||
|
deviceLinks = storage.getDeviceLinks(for: userHexEncodedPublicKey, in: transaction)
|
||||||
|
}
|
||||||
|
deviceLinks.remove(deviceLink)
|
||||||
|
return setDeviceLinks(deviceLinks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the device links associated with the given hex encoded public key from the
|
||||||
|
/// server and stores and returns the valid ones.
|
||||||
public static func getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> Promise<Set<DeviceLink>> {
|
public static func getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> Promise<Set<DeviceLink>> {
|
||||||
// Gets the device links associated with the given hex encoded public key from the
|
return Promise.value(Set<DeviceLink>()) // TODO: Implement
|
||||||
// server and stores and returns the valid ones
|
}
|
||||||
return Promise.value(Set<DeviceLink>())
|
|
||||||
|
// MARK: Private API
|
||||||
|
public static func setDeviceLinks(_ deviceLinks: Set<DeviceLink>) -> Promise<Void> {
|
||||||
|
return getAuthToken(for: server).then { token -> Promise<Void> in
|
||||||
|
let isMaster = deviceLinks.contains { $0.master.hexEncodedPublicKey == userHexEncodedPublicKey }
|
||||||
|
let deviceLinksAsJSON = deviceLinks.map { $0.toJSON() }
|
||||||
|
let value = !deviceLinksAsJSON.isEmpty ? [ "isPrimary" : isMaster ? 1 : 0, "authorisations" : deviceLinksAsJSON ] : nil
|
||||||
|
let annotation: JSON = [ "type" : "network.loki.messenger.devicemapping", "value" : value ]
|
||||||
|
let parameters: JSON = [ "annotations" : [ annotation ] ]
|
||||||
|
let url = URL(string: "\(server)/users/me")!
|
||||||
|
let request = TSRequest(url: url, method: "PATCH", parameters: parameters)
|
||||||
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
||||||
|
return TSNetworkManager.shared().makePromise(request: request).map { _ in }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,14 @@ public final class DeviceLink : NSObject, NSCoding {
|
||||||
coder.encode(master, forKey: "master")
|
coder.encode(master, forKey: "master")
|
||||||
coder.encode(slave, forKey: "slave")
|
coder.encode(slave, forKey: "slave")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: JSON
|
||||||
|
public func toJSON() -> JSON {
|
||||||
|
var result = [ "primaryDevicePubKey" : master.hexEncodedPublicKey, "secondaryDevicePubKey" : slave.hexEncodedPublicKey ]
|
||||||
|
if let masterSignature = master.signature { result["grantSignature"] = masterSignature.base64EncodedString() }
|
||||||
|
if let slaveSignature = slave.signature { result["requestSignature"] = slaveSignature.base64EncodedString() }
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Equality
|
// MARK: Equality
|
||||||
@objc override public func isEqual(_ other: Any?) -> Bool {
|
@objc override public func isEqual(_ other: Any?) -> Bool {
|
||||||
|
|
|
@ -10,12 +10,12 @@ extension OWSPrimaryStorage {
|
||||||
transaction.setObject(deviceLink, forKey: deviceLink.slave.hexEncodedPublicKey, inCollection: collection)
|
transaction.setObject(deviceLink, forKey: deviceLink.slave.hexEncodedPublicKey, inCollection: collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> [DeviceLink] {
|
public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set<DeviceLink> {
|
||||||
let collection = getCollection(for: masterHexEncodedPublicKey)
|
let collection = getCollection(for: masterHexEncodedPublicKey)
|
||||||
var result: [DeviceLink] = []
|
var result: Set<DeviceLink> = []
|
||||||
transaction.enumerateRows(inCollection: collection) { _, object, _, _ in
|
transaction.enumerateRows(inCollection: collection) { _, object, _, _ in
|
||||||
guard let deviceLink = object as? DeviceLink else { return }
|
guard let deviceLink = object as? DeviceLink else { return }
|
||||||
result.append(deviceLink)
|
result.insert(deviceLink)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue