diff --git a/SignalServiceKit/src/Account/TSAttributes.h b/SignalServiceKit/src/Account/TSAttributes.h index 20f9d8d2f..a9168b440 100644 --- a/SignalServiceKit/src/Account/TSAttributes.h +++ b/SignalServiceKit/src/Account/TSAttributes.h @@ -1,16 +1,17 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // NS_ASSUME_NONNULL_BEGIN @interface TSAttributes : NSObject -+ (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled; ++ (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled pin:(nullable NSString *)pin; + (NSDictionary *)attributesWithSignalingKey:(NSString *)signalingKey serverAuthToken:(NSString *)authToken - manualMessageFetching:(BOOL)isEnabled; + manualMessageFetching:(BOOL)isEnabled + pin:(nullable NSString *)pin; @end diff --git a/SignalServiceKit/src/Account/TSAttributes.m b/SignalServiceKit/src/Account/TSAttributes.m index da208af3d..da326b82c 100644 --- a/SignalServiceKit/src/Account/TSAttributes.m +++ b/SignalServiceKit/src/Account/TSAttributes.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "TSAttributes.h" @@ -9,28 +9,35 @@ NS_ASSUME_NONNULL_BEGIN @implementation TSAttributes -+ (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled ++ (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled pin:(nullable NSString *)pin { return [self attributesWithSignalingKey:TSAccountManager.signalingKey serverAuthToken:TSAccountManager.serverAuthToken - manualMessageFetching:isEnabled]; + manualMessageFetching:isEnabled + pin:pin]; } + (NSDictionary *)attributesWithSignalingKey:(NSString *)signalingKey serverAuthToken:(NSString *)authToken manualMessageFetching:(BOOL)isEnabled + pin:(nullable NSString *)pin { OWSAssert(signalingKey.length > 0); OWSAssert(authToken.length > 0); - return @{ + NSMutableDictionary *result = [@{ @"signalingKey" : signalingKey, @"AuthKey" : authToken, @"voice" : @(YES), // all Signal-iOS clients support voice @"video" : @(YES), // all Signal-iOS clients support WebRTC-based voice and video calls. - @"fetchesMessages" : @(isEnabled), // devices that don't support push must tell the server they fetch messages manually + @"fetchesMessages" : + @(isEnabled), // devices that don't support push must tell the server they fetch messages manually @"registrationId" : [NSString stringWithFormat:@"%i", [TSAccountManager getOrGenerateRegistrationId]] - }; + } mutableCopy]; + if (pin.length > 0) { + result[@"pin"] = pin; + } + return [result copy]; } @end diff --git a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m index fca8b1d80..1b38b7ea5 100644 --- a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m +++ b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m @@ -3,13 +3,14 @@ // #import "OWSRequestFactory.h" +#import "NSData+Base64.h" +#import "OWS2FAManager.h" #import "OWSDevice.h" #import "TSAttributes.h" #import "TSConstants.h" #import "TSRequest.h" #import #import -#import NS_ASSUME_NONNULL_BEGIN @@ -171,10 +172,12 @@ NS_ASSUME_NONNULL_BEGIN + (TSRequest *)updateAttributesRequestWithManualMessageFetching:(BOOL)enableManualMessageFetching { NSString *path = [textSecureAccountsAPI stringByAppendingString:textSecureAttributesAPI]; + NSString *_Nullable pin = [OWS2FAManager.sharedManager pinCode]; return [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" - parameters:[TSAttributes attributesFromStorageWithManualMessageFetching:enableManualMessageFetching]]; + parameters:[TSAttributes attributesFromStorageWithManualMessageFetching:enableManualMessageFetching + pin:pin]]; } + (TSRequest *)unregisterAccountRequest diff --git a/SignalServiceKit/src/Network/API/Requests/TSVerifyCodeRequest.m b/SignalServiceKit/src/Network/API/Requests/TSVerifyCodeRequest.m index 8b8add53a..bfb77aac5 100644 --- a/SignalServiceKit/src/Network/API/Requests/TSVerifyCodeRequest.m +++ b/SignalServiceKit/src/Network/API/Requests/TSVerifyCodeRequest.m @@ -24,13 +24,8 @@ NS_ASSUME_NONNULL_BEGIN NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/code/%@", textSecureAccountsAPI, verificationCode]]; - NSMutableDictionary *parameters = - [[TSAttributes attributesWithSignalingKey:signalingKey serverAuthToken:authKey manualMessageFetching:NO] - mutableCopy]; - if (pin) { - OWSAssert(pin.length > 0); - parameters[@"pin"] = pin; - } + NSDictionary *parameters = + [TSAttributes attributesWithSignalingKey:signalingKey serverAuthToken:authKey manualMessageFetching:NO pin:pin]; self = [super initWithURL:url method:@"PUT" parameters:parameters]; _numberToValidate = phoneNumber; diff --git a/SignalServiceKit/src/Util/OWS2FAManager.m b/SignalServiceKit/src/Util/OWS2FAManager.m index 926daf000..e5430e616 100644 --- a/SignalServiceKit/src/Util/OWS2FAManager.m +++ b/SignalServiceKit/src/Util/OWS2FAManager.m @@ -14,7 +14,6 @@ NS_ASSUME_NONNULL_BEGIN NSString *const NSNotificationName_2FAStateDidChange = @"NSNotificationName_2FAStateDidChange"; NSString *const kOWS2FAManager_Collection = @"kOWS2FAManager_Collection"; -NSString *const kOWS2FAManager_IsEnabledKey = @"kOWS2FAManager_IsEnabledKey"; NSString *const kOWS2FAManager_LastSuccessfulReminderDateKey = @"kOWS2FAManager_LastSuccessfulReminderDateKey"; NSString *const kOWS2FAManager_PinCode = @"kOWS2FAManager_PinCode"; NSString *const kOWS2FAManager_RepetitionInterval = @"kOWS2FAManager_RepetitionInterval"; @@ -71,37 +70,36 @@ const NSUInteger kDaySecs = kHourSecs * 24; return self; } +- (nullable NSString *)pinCode +{ + return [self.dbConnection objectForKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection]; +} + - (BOOL)is2FAEnabled { - return [self.dbConnection hasObjectForKey:kOWS2FAManager_PINKey inCollection:kOWS2FAManager_Collection]; + return self.pinCode != nil; } - (void)set2FANotEnabled { - [self.dbConnection removeObjectForKey:kOWS2FAManager_PINKey inCollection:kOWS2FAManager_Collection]; + [self.dbConnection removeObjectForKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection]; [[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange object:nil userInfo:nil]; } -- (void)set2FAEnabledWithPin:(NSString *)pin +- (void)mark2FAAsEnabledWithPin:(NSString *)pin { OWSAssert(pin.length > 0); - [self.dbConnection setObject:pin forKey:kOWS2FAManager_PINKey inCollection:kOWS2FAManager_Collection]; + [self.dbConnection setObject:pin forKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection]; [[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange object:nil userInfo:nil]; } -- (void)mark2FAAsEnabledWithPin:(NSString *)pin -{ - [self setIs2FAEnabled:YES]; - [self storePinCode:pin]; -} - - (void)requestEnable2FAWithPin:(NSString *)pin success:(nullable OWS2FASuccess)success failure:(nullable OWS2FAFailure)failure @@ -155,16 +153,6 @@ const NSUInteger kDaySecs = kHourSecs * 24; #pragma mark - Reminders -- (void)storePinCode:(nullable NSString *)pinCode -{ - [self.dbConnection setObject:pinCode forKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection]; -} - -- (nullable NSString *)pinCode -{ - return [self.dbConnection objectForKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection]; -} - - (nullable NSDate *)lastSuccessfulReminderDate { return [self.dbConnection dateForKey:kOWS2FAManager_LastSuccessfulReminderDateKey