vn-verdnaturachat/ios/Shared/RocketChat/Storage.swift

63 lines
1.7 KiB
Swift
Raw Normal View History

[NEW] E2E Encryption push (iOS) (#2463) * link pods to notification service * push encryption poc * decrypt room key poc * read user key from mmkv and cast into a pkcs * push decrypt poc (iOS) * expose needed watermelon methods * watermelon -> database * indent & simple-crypto update * string extensions * storage * toBase64 -> toData * remove a forced unwrap * remove unused import * database driver * improvement * folder structure & watermelon bridge * more improvement stuff * watermelon -> database * reuse database instance * improvement * database fix: bypass watermelon cache * some code improvements * encryption instances * start api stuff * network layer * improve notification service * improve folder structure * watermelon patch * retry fetch logic * rocketchat class * fix try to decrypt without a roomKey * fallback to original content that is translated * some fixes to rocketchat logic * merge develop * remove unnecessary extension * [CHORE] Improve reply notification code (iOS) * undo sign changes * remove mocked value * import direct from library * send message request * reply notification with encrypted message working properly * revert apple sign * fix api onerror * trick to display sender name on group notifications * revert data.host change * fix some multithread issues * use sendername sent by server * small improvement * Bump crypto lib * Update ios/NotificationService/NotificationService.swift * add experimental string * remove trailing slash * remove trailing slash on reply * fix decrypt messages Co-authored-by: Diego Mello <diegolmello@gmail.com>
2020-09-24 18:34:13 +00:00
//
// Storage.swift
// NotificationService
//
// Created by Djorkaeff Alexandre Vilela Pereira on 9/15/20.
// Copyright © 2020 Rocket.Chat. All rights reserved.
//
import Foundation
struct Credentials {
let userId: String
let userToken: String
}
class Storage {
static let shared = Storage()
final var mmkv: MMKV? = nil
init() {
let mmapID = "default"
let instanceID = "com.MMKV.\(mmapID)"
let secureStorage = SecureStorage()
// get mmkv instance password from keychain
var key: Data?
secureStorage.getSecureKey(instanceID.toHex()) { (response) -> () in
if let password = response?[1] as? String {
key = password.data(using: .utf8)
}
}
guard let cryptKey = key else {
return
}
// Get App Group directory
let suiteName = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
guard let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: suiteName) else {
return
}
// Set App Group dir
MMKV.initialize(rootDir: nil, groupDir: directory.path, logLevel: MMKVLogLevel.none)
self.mmkv = MMKV(mmapID: mmapID, cryptKey: cryptKey, mode: MMKVMode.multiProcess)
}
func getCredentials(server: String) -> Credentials? {
if let userId = self.mmkv?.string(forKey: "reactnativemeteor_usertoken-\(server)") {
if let userToken = self.mmkv?.string(forKey: "reactnativemeteor_usertoken-\(userId)") {
return Credentials(userId: userId, userToken: userToken)
}
}
return nil
}
func getPrivateKey(server: String) -> String? {
return self.mmkv?.string(forKey: "\(server)-RC_E2E_PRIVATE_KEY")
}
}