mirror of https://github.com/oxen-io/session-ios
Partially implement storage API
parent
77cd19d501
commit
8b0141596c
@ -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; }
|
||||||
|
|
@ -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 }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue