Add auth token handling
This commit is contained in:
parent
34bbff1ab4
commit
1d65f717b5
|
@ -71,27 +71,28 @@ extension Storage {
|
|||
|
||||
// MARK: - Authorization
|
||||
|
||||
private static func getAuthTokenCollection(for server: String) -> String {
|
||||
return (server == FileServerAPI.server) ? "LokiStorageAuthTokenCollection" : "LokiGroupChatAuthTokenCollection"
|
||||
}
|
||||
private static let authTokenCollection = "SNAuthTokenCollection"
|
||||
|
||||
public func getAuthToken(for server: String) -> String? {
|
||||
let collection = Storage.getAuthTokenCollection(for: server)
|
||||
public func getAuthToken(for room: String, on server: String) -> String? {
|
||||
let collection = Storage.authTokenCollection
|
||||
let key = "\(server).\(room)"
|
||||
var result: String? = nil
|
||||
Storage.read { transaction in
|
||||
result = transaction.object(forKey: server, inCollection: collection) as? String
|
||||
result = transaction.object(forKey: key, inCollection: collection) as? String
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public func setAuthToken(for server: String, to newValue: String, using transaction: Any) {
|
||||
let collection = Storage.getAuthTokenCollection(for: server)
|
||||
(transaction as! YapDatabaseReadWriteTransaction).setObject(newValue, forKey: server, inCollection: collection)
|
||||
public func setAuthToken(for room: String, on server: String, to newValue: String, using transaction: Any) {
|
||||
let collection = Storage.authTokenCollection
|
||||
let key = "\(server).\(room)"
|
||||
(transaction as! YapDatabaseReadWriteTransaction).setObject(newValue, forKey: key, inCollection: collection)
|
||||
}
|
||||
|
||||
public func removeAuthToken(for server: String, using transaction: Any) {
|
||||
let collection = Storage.getAuthTokenCollection(for: server)
|
||||
(transaction as! YapDatabaseReadWriteTransaction).removeObject(forKey: server, inCollection: collection)
|
||||
public func removeAuthToken(for room: String, on server: String, using transaction: Any) {
|
||||
let collection = Storage.authTokenCollection
|
||||
let key = "\(server).\(room)"
|
||||
(transaction as! YapDatabaseReadWriteTransaction).removeObject(forKey: key, inCollection: collection)
|
||||
}
|
||||
|
||||
|
||||
|
@ -212,4 +213,31 @@ extension Storage {
|
|||
public func setProfilePictureURL(to profilePictureURL: String?, forOpenGroupWithID openGroupID: String, using transaction: Any) {
|
||||
(transaction as! YapDatabaseReadWriteTransaction).setObject(profilePictureURL, forKey: openGroupID, inCollection: Storage.openGroupProfilePictureURLCollection)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: - Deprecated
|
||||
|
||||
private static func getAuthTokenCollection(for server: String) -> String {
|
||||
return (server == FileServerAPI.server) ? "LokiStorageAuthTokenCollection" : "LokiGroupChatAuthTokenCollection"
|
||||
}
|
||||
|
||||
public func getAuthToken(for server: String) -> String? {
|
||||
let collection = Storage.getAuthTokenCollection(for: server)
|
||||
var result: String? = nil
|
||||
Storage.read { transaction in
|
||||
result = transaction.object(forKey: server, inCollection: collection) as? String
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public func setAuthToken(for server: String, to newValue: String, using transaction: Any) {
|
||||
let collection = Storage.getAuthTokenCollection(for: server)
|
||||
(transaction as! YapDatabaseReadWriteTransaction).setObject(newValue, forKey: server, inCollection: collection)
|
||||
}
|
||||
|
||||
public func removeAuthToken(for server: String, using transaction: Any) {
|
||||
let collection = Storage.getAuthTokenCollection(for: server)
|
||||
(transaction as! YapDatabaseReadWriteTransaction).removeObject(forKey: server, inCollection: collection)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import PromiseKit
|
||||
import SessionSnodeKit
|
||||
|
||||
// TODO: Auth token & public key storage
|
||||
|
||||
public enum OpenGroupAPIV2 {
|
||||
|
||||
// MARK: Error
|
||||
|
@ -72,7 +70,7 @@ public enum OpenGroupAPIV2 {
|
|||
tsRequest.setValue(request.room, forKey: "Room")
|
||||
if request.useOnionRouting {
|
||||
guard let publicKey = SNMessagingKitConfiguration.shared.storage.getOpenGroupPublicKey(for: request.server) else { return Promise(error: Error.noPublicKey) }
|
||||
return getAuthToken(for: request.server).then(on: DispatchQueue.global(qos: .default)) { authToken -> Promise<JSON> in
|
||||
return getAuthToken(for: request.room, on: request.server).then(on: DispatchQueue.global(qos: .default)) { authToken -> Promise<JSON> in
|
||||
tsRequest.setValue(authToken, forKey: "Authorization")
|
||||
return OnionRequestAPI.sendOnionRequest(tsRequest, to: request.server, using: publicKey)
|
||||
}
|
||||
|
@ -82,8 +80,23 @@ public enum OpenGroupAPIV2 {
|
|||
}
|
||||
|
||||
// MARK: Authorization
|
||||
private static func getAuthToken(for server: String) -> Promise<String> {
|
||||
return Promise.value("") // TODO: Implement
|
||||
private static func getAuthToken(for room: String, on server: String) -> Promise<String> {
|
||||
let storage = SNMessagingKitConfiguration.shared.storage
|
||||
if let authToken = storage.getAuthToken(for: room, on: server) {
|
||||
return Promise.value(authToken)
|
||||
} else {
|
||||
return requestNewAuthToken(for: room, on: server)
|
||||
.then(on: DispatchQueue.global(qos: .userInitiated)) { claimAuthToken($0, for: room, on: server) }
|
||||
.then(on: DispatchQueue.global(qos: .userInitiated)) { authToken -> Promise<String> in
|
||||
let (promise, seal) = Promise<String>.pending()
|
||||
storage.write(with: { transaction in
|
||||
storage.setAuthToken(for: room, on: server, to: authToken, using: transaction)
|
||||
}, completion: {
|
||||
seal.fulfill(authToken)
|
||||
})
|
||||
return promise
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static func requestNewAuthToken(for room: String, on server: String) -> Promise<String> {
|
||||
|
@ -102,11 +115,11 @@ public enum OpenGroupAPIV2 {
|
|||
}
|
||||
}
|
||||
|
||||
public static func claimAuthToken(for room: String, on server: String) -> Promise<Void> {
|
||||
public static func claimAuthToken(_ authToken: String, for room: String, on server: String) -> Promise<String> {
|
||||
guard let userKeyPair = SNMessagingKitConfiguration.shared.storage.getUserKeyPair() else { return Promise(error: Error.generic) }
|
||||
let parameters = [ "public_key" : userKeyPair.publicKey.toHexString() ]
|
||||
let request = Request(verb: .post, room: room, server: server, endpoint: "claim_auth_token", parameters: parameters)
|
||||
return send(request).map(on: DispatchQueue.global(qos: .userInitiated)) { _ in }
|
||||
return send(request).map(on: DispatchQueue.global(qos: .userInitiated)) { _ in authToken }
|
||||
}
|
||||
|
||||
/// Should be called when leaving a group.
|
||||
|
|
|
@ -37,9 +37,9 @@ public protocol SessionMessagingKitStorageProtocol {
|
|||
|
||||
// MARK: - Authorization
|
||||
|
||||
func getAuthToken(for server: String) -> String?
|
||||
func setAuthToken(for server: String, to newValue: String, using transaction: Any)
|
||||
func removeAuthToken(for server: String, using transaction: Any)
|
||||
func getAuthToken(for room: String, on server: String) -> String?
|
||||
func setAuthToken(for room: String, on server: String, to newValue: String, using transaction: Any)
|
||||
func removeAuthToken(for room: String, on server: String, using transaction: Any)
|
||||
|
||||
// MARK: - Open Groups
|
||||
|
||||
|
@ -87,4 +87,10 @@ public protocol SessionMessagingKitStorageProtocol {
|
|||
func setAttachmentState(to state: TSAttachmentPointerState, for pointer: TSAttachmentPointer, associatedWith tsIncomingMessageID: String, using transaction: Any)
|
||||
/// Also touches the associated message.
|
||||
func persist(_ stream: TSAttachmentStream, associatedWith tsIncomingMessageID: String, using transaction: Any)
|
||||
|
||||
// MARK: - Deprecated
|
||||
|
||||
func getAuthToken(for server: String) -> String?
|
||||
func setAuthToken(for server: String, to newValue: String, using transaction: Any)
|
||||
func removeAuthToken(for server: String, using transaction: Any)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue