Merge branch 'feature/secureid' of https://github.com/peter-iakovlev/TelegramCoreDev into feature/secureid

# Conflicts:
#	TelegramCore/ProxySettings.swift
This commit is contained in:
Mikhail Filimonov 2018-04-20 13:16:36 +04:00
commit 3a4698f25c
10 changed files with 248 additions and 71 deletions

View File

@ -341,6 +341,7 @@ public struct TwoStepAuthData {
public let nextSalt: Data
public let currentSalt: Data?
public let hasRecovery: Bool
public let hasSecretValues: Bool
public let currentHint: String?
public let unconfirmedEmailPattern: String?
public let secretRandom: Data
@ -352,9 +353,9 @@ public func twoStepAuthData(_ network: Network) -> Signal<TwoStepAuthData, MTRpc
|> map { config -> TwoStepAuthData in
switch config {
case let .noPassword(newSalt, newSecureSalt, secretRandom, emailUnconfirmedPattern):
return TwoStepAuthData(nextSalt: newSalt.makeData(), currentSalt: nil, hasRecovery: false, currentHint: nil, unconfirmedEmailPattern: emailUnconfirmedPattern, secretRandom: secretRandom.makeData(), nextSecureSalt: newSecureSalt.makeData())
case let .password(currentSalt, newSalt, newSecureSalt, secretRandom, hint, hasRecovery, emailUnconfirmedPattern):
return TwoStepAuthData(nextSalt: newSalt.makeData(), currentSalt: currentSalt.makeData(), hasRecovery: hasRecovery == .boolTrue, currentHint: hint, unconfirmedEmailPattern: emailUnconfirmedPattern, secretRandom: secretRandom.makeData(), nextSecureSalt: newSecureSalt.makeData())
return TwoStepAuthData(nextSalt: newSalt.makeData(), currentSalt: nil, hasRecovery: false, hasSecretValues: false, currentHint: nil, unconfirmedEmailPattern: emailUnconfirmedPattern, secretRandom: secretRandom.makeData(), nextSecureSalt: newSecureSalt.makeData())
case let .password(flags, currentSalt, newSalt, newSecureSalt, secretRandom, hint, emailUnconfirmedPattern):
return TwoStepAuthData(nextSalt: newSalt.makeData(), currentSalt: currentSalt.makeData(), hasRecovery: (flags & (1 << 0)) != 0, hasSecretValues: (flags & (1 << 1)) != 0, currentHint: hint, unconfirmedEmailPattern: emailUnconfirmedPattern, secretRandom: secretRandom.makeData(), nextSecureSalt: newSecureSalt.makeData())
}
}
}

View File

@ -346,6 +346,10 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = {
dict[-2128640689] = { return Api.account.SentEmailCode.parse_sentEmailCode($0) }
dict[-1038136962] = { return Api.EncryptedFile.parse_encryptedFileEmpty($0) }
dict[1248893260] = { return Api.EncryptedFile.parse_encryptedFile($0) }
dict[-391902247] = { return Api.SecureValueError.parse_secureValueErrorData($0) }
dict[2054162547] = { return Api.SecureValueError.parse_secureValueErrorFile($0) }
dict[1717706985] = { return Api.SecureValueError.parse_secureValueErrorFiles($0) }
dict[-449327402] = { return Api.SecureValueError.parse_secureValueErrorSelfie($0) }
dict[1489977929] = { return Api.ChannelBannedRights.parse_channelBannedRights($0) }
dict[-1613493288] = { return Api.NotifyPeer.parse_notifyPeer($0) }
dict[-1261946036] = { return Api.NotifyPeer.parse_notifyUsers($0) }
@ -528,7 +532,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = {
dict[-209768682] = { return Api.messages.FavedStickers.parse_favedStickers($0) }
dict[1776236393] = { return Api.ExportedChatInvite.parse_chatInviteEmpty($0) }
dict[-64092740] = { return Api.ExportedChatInvite.parse_chatInviteExported($0) }
dict[-1177300496] = { return Api.account.AuthorizationForm.parse_authorizationForm($0) }
dict[-879268525] = { return Api.account.AuthorizationForm.parse_authorizationForm($0) }
dict[2079516406] = { return Api.Authorization.parse_authorization($0) }
dict[-1361650766] = { return Api.MaskCoords.parse_maskCoords($0) }
dict[-395967805] = { return Api.messages.AllStickers.parse_allStickersNotModified($0) }
@ -546,7 +550,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = {
dict[-1289704741] = { return Api.SecureValueType.parse_secureValueTypePhone($0) }
dict[-1908627474] = { return Api.SecureValueType.parse_secureValueTypeEmail($0) }
dict[1587643126] = { return Api.account.Password.parse_noPassword($0) }
dict[-798203965] = { return Api.account.Password.parse_password($0) }
dict[-902187961] = { return Api.account.Password.parse_password($0) }
dict[-1462213465] = { return Api.InputBotInlineResult.parse_inputBotInlineResultPhoto($0) }
dict[-459324] = { return Api.InputBotInlineResult.parse_inputBotInlineResultDocument($0) }
dict[1336154098] = { return Api.InputBotInlineResult.parse_inputBotInlineResultGame($0) }
@ -889,6 +893,8 @@ public struct Api {
_1.serialize(buffer, boxed)
case let _1 as Api.EncryptedFile:
_1.serialize(buffer, boxed)
case let _1 as Api.SecureValueError:
_1.serialize(buffer, boxed)
case let _1 as Api.ChannelBannedRights:
_1.serialize(buffer, boxed)
case let _1 as Api.NotifyPeer:

View File

@ -7392,6 +7392,137 @@ public extension Api {
}
}
}
public enum SecureValueError {
case secureValueErrorData(type: Api.SecureValueType, dataHash: Buffer, field: String, text: String)
case secureValueErrorFile(type: Api.SecureValueType, fileHash: Buffer, text: String)
case secureValueErrorFiles(type: Api.SecureValueType, fileHash: [Buffer], text: String)
case secureValueErrorSelfie(type: Api.SecureValueType, fileHash: Buffer, text: String)
public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) {
switch self {
case .secureValueErrorData(let type, let dataHash, let field, let text):
if boxed {
buffer.appendInt32(-391902247)
}
type.serialize(buffer, true)
serializeBytes(dataHash, buffer: buffer, boxed: false)
serializeString(field, buffer: buffer, boxed: false)
serializeString(text, buffer: buffer, boxed: false)
break
case .secureValueErrorFile(let type, let fileHash, let text):
if boxed {
buffer.appendInt32(2054162547)
}
type.serialize(buffer, true)
serializeBytes(fileHash, buffer: buffer, boxed: false)
serializeString(text, buffer: buffer, boxed: false)
break
case .secureValueErrorFiles(let type, let fileHash, let text):
if boxed {
buffer.appendInt32(1717706985)
}
type.serialize(buffer, true)
buffer.appendInt32(481674261)
buffer.appendInt32(Int32(fileHash.count))
for item in fileHash {
serializeBytes(item, buffer: buffer, boxed: false)
}
serializeString(text, buffer: buffer, boxed: false)
break
case .secureValueErrorSelfie(let type, let fileHash, let text):
if boxed {
buffer.appendInt32(-449327402)
}
type.serialize(buffer, true)
serializeBytes(fileHash, buffer: buffer, boxed: false)
serializeString(text, buffer: buffer, boxed: false)
break
}
}
static func parse_secureValueErrorData(_ reader: BufferReader) -> SecureValueError? {
var _1: Api.SecureValueType?
if let signature = reader.readInt32() {
_1 = Api.parse(reader, signature: signature) as? Api.SecureValueType
}
var _2: Buffer?
_2 = parseBytes(reader)
var _3: String?
_3 = parseString(reader)
var _4: String?
_4 = parseString(reader)
let _c1 = _1 != nil
let _c2 = _2 != nil
let _c3 = _3 != nil
let _c4 = _4 != nil
if _c1 && _c2 && _c3 && _c4 {
return Api.SecureValueError.secureValueErrorData(type: _1!, dataHash: _2!, field: _3!, text: _4!)
}
else {
return nil
}
}
static func parse_secureValueErrorFile(_ reader: BufferReader) -> SecureValueError? {
var _1: Api.SecureValueType?
if let signature = reader.readInt32() {
_1 = Api.parse(reader, signature: signature) as? Api.SecureValueType
}
var _2: Buffer?
_2 = parseBytes(reader)
var _3: String?
_3 = parseString(reader)
let _c1 = _1 != nil
let _c2 = _2 != nil
let _c3 = _3 != nil
if _c1 && _c2 && _c3 {
return Api.SecureValueError.secureValueErrorFile(type: _1!, fileHash: _2!, text: _3!)
}
else {
return nil
}
}
static func parse_secureValueErrorFiles(_ reader: BufferReader) -> SecureValueError? {
var _1: Api.SecureValueType?
if let signature = reader.readInt32() {
_1 = Api.parse(reader, signature: signature) as? Api.SecureValueType
}
var _2: [Buffer]?
if let _ = reader.readInt32() {
_2 = Api.parseVector(reader, elementSignature: -1255641564, elementType: Buffer.self)
}
var _3: String?
_3 = parseString(reader)
let _c1 = _1 != nil
let _c2 = _2 != nil
let _c3 = _3 != nil
if _c1 && _c2 && _c3 {
return Api.SecureValueError.secureValueErrorFiles(type: _1!, fileHash: _2!, text: _3!)
}
else {
return nil
}
}
static func parse_secureValueErrorSelfie(_ reader: BufferReader) -> SecureValueError? {
var _1: Api.SecureValueType?
if let signature = reader.readInt32() {
_1 = Api.parse(reader, signature: signature) as? Api.SecureValueType
}
var _2: Buffer?
_2 = parseBytes(reader)
var _3: String?
_3 = parseString(reader)
let _c1 = _1 != nil
let _c2 = _2 != nil
let _c3 = _3 != nil
if _c1 && _c2 && _c3 {
return Api.SecureValueError.secureValueErrorSelfie(type: _1!, fileHash: _2!, text: _3!)
}
else {
return nil
}
}
}
public enum ChannelBannedRights {
case channelBannedRights(flags: Int32, untilDate: Int32)

View File

@ -2326,13 +2326,13 @@ public struct account {
}
public enum AuthorizationForm {
case authorizationForm(flags: Int32, requiredTypes: [Api.SecureValueType], values: [Api.SecureValue], users: [Api.User], privacyPolicyUrl: String?)
case authorizationForm(flags: Int32, requiredTypes: [Api.SecureValueType], values: [Api.SecureValue], errors: [Api.SecureValueError], users: [Api.User], privacyPolicyUrl: String?)
public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) {
switch self {
case .authorizationForm(let flags, let requiredTypes, let values, let users, let privacyPolicyUrl):
case .authorizationForm(let flags, let requiredTypes, let values, let errors, let users, let privacyPolicyUrl):
if boxed {
buffer.appendInt32(-1177300496)
buffer.appendInt32(-879268525)
}
serializeInt32(flags, buffer: buffer, boxed: false)
buffer.appendInt32(481674261)
@ -2346,6 +2346,11 @@ public struct account {
item.serialize(buffer, true)
}
buffer.appendInt32(481674261)
buffer.appendInt32(Int32(errors.count))
for item in errors {
item.serialize(buffer, true)
}
buffer.appendInt32(481674261)
buffer.appendInt32(Int32(users.count))
for item in users {
item.serialize(buffer, true)
@ -2366,19 +2371,24 @@ public struct account {
if let _ = reader.readInt32() {
_3 = Api.parseVector(reader, elementSignature: 0, elementType: Api.SecureValue.self)
}
var _4: [Api.User]?
var _4: [Api.SecureValueError]?
if let _ = reader.readInt32() {
_4 = Api.parseVector(reader, elementSignature: 0, elementType: Api.User.self)
_4 = Api.parseVector(reader, elementSignature: 0, elementType: Api.SecureValueError.self)
}
var _5: String?
if Int(_1!) & Int(1 << 0) != 0 {_5 = parseString(reader) }
var _5: [Api.User]?
if let _ = reader.readInt32() {
_5 = Api.parseVector(reader, elementSignature: 0, elementType: Api.User.self)
}
var _6: String?
if Int(_1!) & Int(1 << 0) != 0 {_6 = parseString(reader) }
let _c1 = _1 != nil
let _c2 = _2 != nil
let _c3 = _3 != nil
let _c4 = _4 != nil
let _c5 = (Int(_1!) & Int(1 << 0) == 0) || _5 != nil
if _c1 && _c2 && _c3 && _c4 && _c5 {
return Api.account.AuthorizationForm.authorizationForm(flags: _1!, requiredTypes: _2!, values: _3!, users: _4!, privacyPolicyUrl: _5)
let _c5 = _5 != nil
let _c6 = (Int(_1!) & Int(1 << 0) == 0) || _6 != nil
if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 {
return Api.account.AuthorizationForm.authorizationForm(flags: _1!, requiredTypes: _2!, values: _3!, errors: _4!, users: _5!, privacyPolicyUrl: _6)
}
else {
return nil
@ -2388,7 +2398,7 @@ public struct account {
}
public enum Password {
case noPassword(newSalt: Buffer, newSecureSalt: Buffer, secureRandom: Buffer, emailUnconfirmedPattern: String)
case password(currentSalt: Buffer, newSalt: Buffer, newSecureSalt: Buffer, secureRandom: Buffer, hint: String, hasRecovery: Api.Bool, emailUnconfirmedPattern: String)
case password(flags: Int32, currentSalt: Buffer, newSalt: Buffer, newSecureSalt: Buffer, secureRandom: Buffer, hint: String, emailUnconfirmedPattern: String)
public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) {
switch self {
@ -2401,16 +2411,16 @@ public struct account {
serializeBytes(secureRandom, buffer: buffer, boxed: false)
serializeString(emailUnconfirmedPattern, buffer: buffer, boxed: false)
break
case .password(let currentSalt, let newSalt, let newSecureSalt, let secureRandom, let hint, let hasRecovery, let emailUnconfirmedPattern):
case .password(let flags, let currentSalt, let newSalt, let newSecureSalt, let secureRandom, let hint, let emailUnconfirmedPattern):
if boxed {
buffer.appendInt32(-798203965)
buffer.appendInt32(-902187961)
}
serializeInt32(flags, buffer: buffer, boxed: false)
serializeBytes(currentSalt, buffer: buffer, boxed: false)
serializeBytes(newSalt, buffer: buffer, boxed: false)
serializeBytes(newSecureSalt, buffer: buffer, boxed: false)
serializeBytes(secureRandom, buffer: buffer, boxed: false)
serializeString(hint, buffer: buffer, boxed: false)
hasRecovery.serialize(buffer, true)
serializeString(emailUnconfirmedPattern, buffer: buffer, boxed: false)
break
}
@ -2437,20 +2447,18 @@ public struct account {
}
}
static func parse_password(_ reader: BufferReader) -> Password? {
var _1: Buffer?
_1 = parseBytes(reader)
var _1: Int32?
_1 = reader.readInt32()
var _2: Buffer?
_2 = parseBytes(reader)
var _3: Buffer?
_3 = parseBytes(reader)
var _4: Buffer?
_4 = parseBytes(reader)
var _5: String?
_5 = parseString(reader)
var _6: Api.Bool?
if let signature = reader.readInt32() {
_6 = Api.parse(reader, signature: signature) as? Api.Bool
}
var _5: Buffer?
_5 = parseBytes(reader)
var _6: String?
_6 = parseString(reader)
var _7: String?
_7 = parseString(reader)
let _c1 = _1 != nil
@ -2461,7 +2469,7 @@ public struct account {
let _c6 = _6 != nil
let _c7 = _7 != nil
if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 && _c7 {
return Api.account.Password.password(currentSalt: _1!, newSalt: _2!, newSecureSalt: _3!, secureRandom: _4!, hint: _5!, hasRecovery: _6!, emailUnconfirmedPattern: _7!)
return Api.account.Password.password(flags: _1!, currentSalt: _2!, newSalt: _3!, newSecureSalt: _4!, secureRandom: _5!, hint: _6!, emailUnconfirmedPattern: _7!)
}
else {
return nil

View File

@ -2769,6 +2769,25 @@ public extension Api {
return result
})
}
public static func setSecureValueErrors(id: Api.InputUser, errors: [Api.SecureValueError]) -> (CustomStringConvertible, Buffer, (Buffer) -> Api.Bool?) {
let buffer = Buffer()
buffer.appendInt32(-1865902923)
id.serialize(buffer, true)
buffer.appendInt32(481674261)
buffer.appendInt32(Int32(errors.count))
for item in errors {
item.serialize(buffer, true)
}
return (FunctionDescription({return "(users.setSecureValueErrors id: \(id), errors: \(errors))"}), buffer, { (buffer: Buffer) -> Api.Bool? in
let reader = BufferReader(buffer)
var result: Api.Bool?
if let signature = reader.readInt32() {
result = Api.parse(reader, signature: signature) as? Api.Bool
}
return result
})
}
}
public struct contacts {
public static func getStatuses() -> (CustomStringConvertible, Buffer, (Buffer) -> [Api.ContactStatus]?) {

View File

@ -156,7 +156,7 @@ public func authorizeWithCode(account: UnauthorizedAccount, code: String) -> Sig
switch result {
case .noPassword:
return .fail(.generic)
case let .password(_, _, _, _, hint, _, _):
case let .password(_, _, _, _, _, hint, _):
return .single(.password(hint: hint))
}
}

View File

@ -45,6 +45,7 @@ class Download: NSObject, MTRequestMessageServiceDelegate {
self.mtProto = MTProto(context: self.context, datacenterId: datacenterId, usageCalculationInfo: usageInfo)
self.mtProto.cdn = isCdn
self.mtProto.useTempAuthKeys = !isCdn
self.mtProto.media = true;
if !isCdn && datacenterId != masterDatacenterId {
self.mtProto.authTokenMasterDatacenterId = masterDatacenterId
self.mtProto.requiredAuthToken = Int(datacenterId) as NSNumber

View File

@ -9,18 +9,17 @@ import Foundation
import MtProtoKitDynamic
#endif
public final class ProxySettings: PreferencesEntry, Equatable {
public struct ProxyServerSettings: PostboxCoding, Equatable {
public let host: String
public let port: Int32
public let username: String?
public let password: String?
public let useForCalls: Bool
public init(host: String, port: Int32, username: String?, password: String?, useForCalls: Bool) {
public init(host: String, port: Int32, username: String?, password: String?) {
self.host = host
self.port = port
self.username = username
self.password = password
self.useForCalls = useForCalls
}
public init(decoder: PostboxDecoder) {
@ -28,7 +27,6 @@ public final class ProxySettings: PreferencesEntry, Equatable {
self.port = decoder.decodeInt32ForKey("port", orElse: 0)
self.username = decoder.decodeOptionalStringForKey("username")
self.password = decoder.decodeOptionalStringForKey("password")
self.useForCalls = decoder.decodeInt32ForKey("useForCalls", orElse: 0) != 0
}
public func encode(_ encoder: PostboxEncoder) {
@ -44,6 +42,37 @@ public final class ProxySettings: PreferencesEntry, Equatable {
} else {
encoder.encodeNil(forKey: "password")
}
}
}
public struct ProxySettings: PreferencesEntry, Equatable {
public var servers: [ProxyServerSettings]
public var activeServer: ProxyServerSettings?
public var useForCalls: Bool
public static var defaultSettings: ProxySettings {
return ProxySettings(servers: [], activeServer: nil, useForCalls: false)
}
public init(servers: [ProxyServerSettings], activeServer: ProxyServerSettings?, useForCalls: Bool) {
self.servers = servers
self.activeServer = activeServer
self.useForCalls = useForCalls
}
public init(decoder: PostboxDecoder) {
self.servers = decoder.decodeObjectArrayWithDecoderForKey("servers")
self.activeServer = decoder.decodeObjectForKey("activeServer", decoder: ProxyServerSettings.init(decoder:)) as? ProxyServerSettings
self.useForCalls = decoder.decodeInt32ForKey("useForCalls", orElse: 0) != 0
}
public func encode(_ encoder: PostboxEncoder) {
encoder.encodeObjectArray(self.servers, forKey: "servers")
if let activeServer = self.activeServer {
encoder.encodeObject(activeServer, forKey: "activeServer")
} else {
encoder.encodeNil(forKey: "activeServer")
}
encoder.encodeInt32(self.useForCalls ? 1 : 0, forKey: "useForCalls")
}
@ -54,46 +83,28 @@ public final class ProxySettings: PreferencesEntry, Equatable {
return self == to
}
public static func ==(lhs: ProxySettings, rhs: ProxySettings) -> Bool {
if lhs.host != rhs.host {
return false
}
if lhs.port != rhs.port {
return false
}
if lhs.username != rhs.username {
return false
}
if lhs.password != rhs.password {
return false
}
if lhs.useForCalls != rhs.useForCalls {
return false
}
return true
}
}
public func updateProxySettings(postbox:Postbox, _ f: @escaping (ProxySettings?)->ProxySettings?) -> Signal<Void, Void> {
public func updateProxySettingsInteractively(postbox: Postbox, network: Network, _ f: @escaping (ProxySettings) -> ProxySettings) -> Signal<Void, NoError> {
return postbox.modify { modifier -> Void in
var updateNetwork = false
var updatedSettings: ProxySettings?
modifier.updatePreferencesEntry(key: PreferencesKeys.proxySettings, { current in
return f(current as? ProxySettings)
})
}
}
public func applyProxySettings(postbox: Postbox, network: Network, settings: ProxySettings?) -> Signal<Void, NoError> {
return postbox.modify { modifier -> Void in
modifier.updatePreferencesEntry(key: PreferencesKeys.proxySettings, { _ in
return settings
let previous = (current as? ProxySettings) ?? ProxySettings.defaultSettings
let updated = f(previous)
updatedSettings = updated
if updated.activeServer != previous.activeServer {
updateNetwork = true
}
return updated
})
network.context.updateApiEnvironment { current in
return current?.withUpdatedSocksProxySettings(settings.flatMap { proxySettings -> MTSocksProxySettings? in
return MTSocksProxySettings(ip: proxySettings.host, port: UInt16(proxySettings.port), username: proxySettings.username, password: proxySettings.password)
})
if updateNetwork, let updatedSettings = updatedSettings {
network.context.updateApiEnvironment { current in
return current?.withUpdatedSocksProxySettings(updatedSettings.activeServer.flatMap { activeServer -> MTSocksProxySettings? in
return MTSocksProxySettings(ip: activeServer.host, port: UInt16(activeServer.port), username: activeServer.username, password: activeServer.password)
})
}
}
}
}

View File

@ -197,7 +197,7 @@ public func requestSecureIdForm(postbox: Postbox, network: Network, peerId: Peer
|> mapToSignal { result -> Signal<EncryptedSecureIdForm, RequestSecureIdFormError> in
return postbox.modify { modifier -> EncryptedSecureIdForm in
switch result {
case let .authorizationForm(flags, requiredTypes, values, users, termsUrl):
case let .authorizationForm(flags, requiredTypes, values, errors, users, termsUrl):
var peers: [Peer] = []
for user in users {
let parsed = TelegramUser(user: user)

View File

@ -21,8 +21,8 @@ public func twoStepVerificationConfiguration(account: Account) -> Signal<TwoStep
switch result {
case let .noPassword(_, _, _, emailUnconfirmedPattern):
return .notSet(pendingEmailPattern: emailUnconfirmedPattern)
case let .password(_, _, _, _, hint, hasRecovery, emailUnconfirmedPattern):
return .set(hint: hint, hasRecoveryEmail: hasRecovery == .boolTrue, pendingEmailPattern: emailUnconfirmedPattern)
case let .password(flags, _, _, _, _, hint, emailUnconfirmedPattern):
return .set(hint: hint, hasRecoveryEmail: (flags & (1 << 0)) != 0, pendingEmailPattern: emailUnconfirmedPattern)
}
}
}