Fix registration.

pull/1/head
Matthew Chen 6 years ago
parent 736d0c421d
commit 63260ee94d

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -13,11 +13,19 @@ import SignalServiceKit
@objc @objc
public class AccountManager: NSObject { public class AccountManager: NSObject {
// MARK: - Dependencies
var pushManager: PushManager { var pushManager: PushManager {
// dependency injection hack since PushManager has *alot* of dependencies, and would induce a cycle. // dependency injection hack since PushManager has *alot* of dependencies, and would induce a cycle.
return PushManager.shared() return PushManager.shared()
} }
var profileManager: OWSProfileManager {
return OWSProfileManager.shared()
}
// MARK: -
@objc @objc
public override init() { public override init() {
super.init() super.init()
@ -58,22 +66,20 @@ public class AccountManager: NSObject {
Logger.debug("registering with signal server") Logger.debug("registering with signal server")
let registrationPromise: Promise<Void> = firstly { let registrationPromise: Promise<Void> = firstly {
return self.registerForTextSecure(verificationCode: verificationCode, pin: pin) return self.registerForTextSecure(verificationCode: verificationCode, pin: pin)
}.then { }.then { _ -> Promise<Void> in
return self.syncPushTokens() return self.syncPushTokens().recover { (error) -> Promise<Void> in
}.recover { (error) -> Promise<Void> in switch error {
switch error { case PushRegistrationError.pushNotSupported(let description):
case PushRegistrationError.pushNotSupported(let description): // This can happen with:
// This can happen with: // - simulators, none of which support receiving push notifications
// - simulators, none of which support receiving push notifications // - on iOS11 devices which have disabled "Allow Notifications" and disabled "Enable Background Refresh" in the system settings.
// - on iOS11 devices which have disabled "Allow Notifications" and disabled "Enable Background Refresh" in the system settings. Logger.info("Recovered push registration error. Registering for manual message fetcher because push not supported: \(description)")
Logger.info("Recovered push registration error. Registering for manual message fetcher because push not supported: \(description)") return self.enableManualMessageFetching()
return self.enableManualMessageFetching() default:
default: throw error
throw error }
} }
}.then { (_) in }.done { (_) -> Void in
self.tsAccountManager.performUpdateAccountAttributes()
}.done { (_) in
self.completeRegistration() self.completeRegistration()
} }
@ -86,9 +92,9 @@ public class AccountManager: NSObject {
pin: String?) -> Promise<Void> { pin: String?) -> Promise<Void> {
return Promise { resolver in return Promise { resolver in
tsAccountManager.verifyAccount(withCode: verificationCode, tsAccountManager.verifyAccount(withCode: verificationCode,
pin: pin, pin: pin,
success: resolver.fulfill, success: resolver.fulfill,
failure: resolver.reject) failure: resolver.reject)
} }
} }

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -8,7 +8,7 @@ import SignalMessaging
class DebugUIProfile: DebugUIPage { class DebugUIProfile: DebugUIPage {
// MARK: Dependencies // MARK: - Dependencies
var messageSender: MessageSender { var messageSender: MessageSender {
return SSKEnvironment.shared.messageSender return SSKEnvironment.shared.messageSender
@ -17,7 +17,7 @@ class DebugUIProfile: DebugUIPage {
return OWSProfileManager.shared() return OWSProfileManager.shared()
} }
// MARK: Overrides // MARK: - Overrides
override func name() -> String { override func name() -> String {
return "Profile" return "Profile"

@ -398,8 +398,32 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
case 200: case 200:
case 204: { case 204: {
OWSLogInfo(@"Verification code accepted."); OWSLogInfo(@"Verification code accepted.");
[TSPreKeyManager createPreKeysWithSuccess:successBlock failure:failureBlock];
[self.profileManager fetchLocalUsersProfile]; [self storeServerAuthToken:authToken];
[[[SignalServiceRestClient new] updateAccountAttributesObjC]
.thenInBackground(^{
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[TSPreKeyManager
createPreKeysWithSuccess:^{
resolve(@(1));
}
failure:^(NSError *error) {
resolve(error);
}];
}];
})
.then(^{
[self.profileManager fetchLocalUsersProfile];
})
.then(^{
successBlock();
})
.catchInBackground(^(NSError *error) {
OWSLogError(@"Error: %@", error);
failureBlock(error);
}) retainUntilComplete];
break; break;
} }
default: { default: {

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
#import "TSPreKeyManager.h" #import "TSPreKeyManager.h"
@ -13,6 +13,8 @@
#import <SignalCoreKit/NSDate+OWS.h> #import <SignalCoreKit/NSDate+OWS.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h> #import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
// Time before deletion of signed prekeys (measured in seconds) // Time before deletion of signed prekeys (measured in seconds)
#define kSignedPreKeysDeletionTime (7 * kDayInterval) #define kSignedPreKeysDeletionTime (7 * kDayInterval)
@ -107,12 +109,10 @@ static const NSUInteger kMaxPrekeyUpdateFailureCount = 5;
+ (void)checkPreKeysIfNecessary + (void)checkPreKeysIfNecessary
{ {
if (!CurrentAppContext().isMainApp) { if (!CurrentAppContext().isMainAppAndActive) {
return; return;
} }
OWSAssertDebug(CurrentAppContext().isMainAppAndActive); if (!self.tsAccountManager.isRegisteredAndReady) {
if (!self.tsAccountManager.isRegistered) {
return; return;
} }
@ -156,6 +156,8 @@ static const NSUInteger kMaxPrekeyUpdateFailureCount = 5;
+ (void)createPreKeysWithSuccess:(void (^)(void))successHandler failure:(void (^)(NSError *error))failureHandler + (void)createPreKeysWithSuccess:(void (^)(void))successHandler failure:(void (^)(NSError *error))failureHandler
{ {
OWSAssertDebug(!self.tsAccountManager.isRegisteredAndReady);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
SSKCreatePreKeysOperation *operation = [SSKCreatePreKeysOperation new]; SSKCreatePreKeysOperation *operation = [SSKCreatePreKeysOperation new];
[self.operationQueue addOperations:@[ operation ] waitUntilFinished:YES]; [self.operationQueue addOperations:@[ operation ] waitUntilFinished:YES];
@ -175,6 +177,8 @@ static const NSUInteger kMaxPrekeyUpdateFailureCount = 5;
+ (void)rotateSignedPreKeyWithSuccess:(void (^)(void))successHandler failure:(void (^)(NSError *error))failureHandler + (void)rotateSignedPreKeyWithSuccess:(void (^)(void))successHandler failure:(void (^)(NSError *error))failureHandler
{ {
OWSAssertDebug(!self.tsAccountManager.isRegisteredAndReady);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
SSKRotateSignedPreKeyOperation *operation = [SSKRotateSignedPreKeyOperation new]; SSKRotateSignedPreKeyOperation *operation = [SSKRotateSignedPreKeyOperation new];
[self.operationQueue addOperations:@[ operation ] waitUntilFinished:YES]; [self.operationQueue addOperations:@[ operation ] waitUntilFinished:YES];
@ -197,6 +201,9 @@ static const NSUInteger kMaxPrekeyUpdateFailureCount = 5;
if (!CurrentAppContext().isMainApp) { if (!CurrentAppContext().isMainApp) {
return; return;
} }
if (!self.tsAccountManager.isRegisteredAndReady) {
return;
}
SSKRefreshPreKeysOperation *operation = [SSKRefreshPreKeysOperation new]; SSKRefreshPreKeysOperation *operation = [SSKRefreshPreKeysOperation new];
[self.operationQueue addOperation:operation]; [self.operationQueue addOperation:operation];
@ -292,3 +299,5 @@ static const NSUInteger kMaxPrekeyUpdateFailureCount = 5;
} }
@end @end
NS_ASSUME_NONNULL_END

@ -1252,11 +1252,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
void (^handle404)(void) = ^{ void (^handle404)(void) = ^{
OWSLogWarn(@"Unregistered recipient: %@", recipient.uniqueId); OWSLogWarn(@"Unregistered recipient: %@", recipient.uniqueId);
TSThread *_Nullable thread = messageSend.thread;
OWSAssertDebug(thread);
dispatch_async([OWSDispatch sendingQueue], ^{ dispatch_async([OWSDispatch sendingQueue], ^{
[self unregisteredRecipient:recipient message:message thread:thread]; if (![messageSend.message isKindOfClass:[OWSOutgoingSyncMessage class]]) {
TSThread *_Nullable thread = messageSend.thread;
OWSAssertDebug(thread);
[self unregisteredRecipient:recipient message:message thread:thread];
}
NSError *error = OWSErrorMakeNoSuchSignalRecipientError(); NSError *error = OWSErrorMakeNoSuchSignalRecipientError();
// No need to retry if the recipient is not registered. // No need to retry if the recipient is not registered.

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -155,6 +155,10 @@ public class OWSUDManagerImpl: NSObject, OWSUDManager {
func registrationStateDidChange() { func registrationStateDidChange() {
AssertIsOnMainThread() AssertIsOnMainThread()
guard tsAccountManager.isRegisteredAndReady() else {
return
}
// Any error is silently ignored // Any error is silently ignored
ensureSenderCertificate(certificateExpirationPolicy: .strict).retainUntilComplete() ensureSenderCertificate(certificateExpirationPolicy: .strict).retainUntilComplete()
} }

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
#import "TSNetworkManager.h" #import "TSNetworkManager.h"
@ -37,10 +37,19 @@ typedef void (^failureBlock)(NSURLSessionDataTask *task, NSError *error);
@implementation TSNetworkManager @implementation TSNetworkManager
#pragma mark - Dependencies
+ (TSAccountManager *)tsAccountManager
{
return TSAccountManager.sharedInstance;
}
#pragma mark -
@synthesize udSessionManager = _udSessionManager; @synthesize udSessionManager = _udSessionManager;
@synthesize udSerialQueue = _udSerialQueue; @synthesize udSerialQueue = _udSerialQueue;
#pragma mark Singleton implementation #pragma mark - Singleton
+ (instancetype)sharedManager + (instancetype)sharedManager
{ {
@ -108,7 +117,7 @@ typedef void (^failureBlock)(NSURLSessionDataTask *task, NSError *error);
OWSLogInfo(@"Non-UD request succeeded : %@", request); OWSLogInfo(@"Non-UD request succeeded : %@", request);
if (request.shouldHaveAuthorizationHeaders) { if (request.shouldHaveAuthorizationHeaders) {
[TSAccountManager.sharedInstance setIsDeregistered:NO]; [TSNetworkManager.tsAccountManager setIsDeregistered:NO];
} }
successBlock(task, responseObject); successBlock(task, responseObject);
@ -406,7 +415,12 @@ typedef void (^failureBlock)(NSURLSessionDataTask *task, NSError *error);
// * etc. // * etc.
if ([task.originalRequest.URL.absoluteString hasPrefix:textSecureServerURL] if ([task.originalRequest.URL.absoluteString hasPrefix:textSecureServerURL]
&& request.shouldHaveAuthorizationHeaders) { && request.shouldHaveAuthorizationHeaders) {
[TSAccountManager.sharedInstance setIsDeregistered:YES]; if (self.tsAccountManager.isRegisteredAndReady) {
[self.tsAccountManager setIsDeregistered:YES];
} else {
OWSFailDebug(
@"Ignoring auth failure; not registered and ready: %@.", task.originalRequest.URL.absoluteString);
}
} else { } else {
OWSLogWarn(@"Ignoring %d for URL: %@", (int)statusCode, task.originalRequest.URL.absoluteString); OWSLogWarn(@"Ignoring %d for URL: %@", (int)statusCode, task.originalRequest.URL.absoluteString);
} }

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
#import "OWSWebSocket.h" #import "OWSWebSocket.h"
@ -639,7 +639,11 @@ NSString *const kNSNotification_OWSWebSocketStateDidChange = @"kNSNotification_O
if (responseStatus == 403) { if (responseStatus == 403) {
// This should be redundant with our check for the socket // This should be redundant with our check for the socket
// failing due to 403, but let's be thorough. // failing due to 403, but let's be thorough.
[self.tsAccountManager setIsDeregistered:YES]; if (self.tsAccountManager.isRegisteredAndReady) {
[self.tsAccountManager setIsDeregistered:YES];
} else {
OWSFailDebug(@"Ignoring auth failure; not registered and ready.");
}
} }
NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeMessageResponseFailed, NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeMessageResponseFailed,
@ -705,7 +709,11 @@ NSString *const kNSNotification_OWSWebSocketStateDidChange = @"kNSNotification_O
if ([error.domain isEqualToString:SRWebSocketErrorDomain] && error.code == 2132) { if ([error.domain isEqualToString:SRWebSocketErrorDomain] && error.code == 2132) {
NSNumber *_Nullable statusCode = error.userInfo[SRHTTPResponseErrorKey]; NSNumber *_Nullable statusCode = error.userInfo[SRHTTPResponseErrorKey];
if (statusCode.unsignedIntegerValue == 403) { if (statusCode.unsignedIntegerValue == 403) {
[self.tsAccountManager setIsDeregistered:YES]; if (self.tsAccountManager.isRegisteredAndReady) {
[self.tsAccountManager setIsDeregistered:YES];
} else {
OWSFailDebug(@"Ignoring auth failure; not registered and ready.");
}
} }
} }
@ -932,7 +940,7 @@ NSString *const kNSNotification_OWSWebSocketStateDidChange = @"kNSNotification_O
return NO; return NO;
} }
if (![self.tsAccountManager isRegistered]) { if (![self.tsAccountManager isRegisteredAndReady]) {
return NO; return NO;
} }

Loading…
Cancel
Save