Merge branch 'charlesmchen/accountAttributesVsPin'

pull/1/head
Matthew Chen 7 years ago
commit 09d561823b

@ -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 NS_ASSUME_NONNULL_BEGIN
@interface TSAttributes : NSObject @interface TSAttributes : NSObject
+ (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled; + (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled pin:(nullable NSString *)pin;
+ (NSDictionary *)attributesWithSignalingKey:(NSString *)signalingKey + (NSDictionary *)attributesWithSignalingKey:(NSString *)signalingKey
serverAuthToken:(NSString *)authToken serverAuthToken:(NSString *)authToken
manualMessageFetching:(BOOL)isEnabled; manualMessageFetching:(BOOL)isEnabled
pin:(nullable NSString *)pin;
@end @end

@ -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" #import "TSAttributes.h"
@ -9,28 +9,35 @@ NS_ASSUME_NONNULL_BEGIN
@implementation TSAttributes @implementation TSAttributes
+ (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled + (NSDictionary *)attributesFromStorageWithManualMessageFetching:(BOOL)isEnabled pin:(nullable NSString *)pin
{ {
return [self attributesWithSignalingKey:TSAccountManager.signalingKey return [self attributesWithSignalingKey:TSAccountManager.signalingKey
serverAuthToken:TSAccountManager.serverAuthToken serverAuthToken:TSAccountManager.serverAuthToken
manualMessageFetching:isEnabled]; manualMessageFetching:isEnabled
pin:pin];
} }
+ (NSDictionary *)attributesWithSignalingKey:(NSString *)signalingKey + (NSDictionary *)attributesWithSignalingKey:(NSString *)signalingKey
serverAuthToken:(NSString *)authToken serverAuthToken:(NSString *)authToken
manualMessageFetching:(BOOL)isEnabled manualMessageFetching:(BOOL)isEnabled
pin:(nullable NSString *)pin
{ {
OWSAssert(signalingKey.length > 0); OWSAssert(signalingKey.length > 0);
OWSAssert(authToken.length > 0); OWSAssert(authToken.length > 0);
return @{ NSMutableDictionary *result = [@{
@"signalingKey" : signalingKey, @"signalingKey" : signalingKey,
@"AuthKey" : authToken, @"AuthKey" : authToken,
@"voice" : @(YES), // all Signal-iOS clients support voice @"voice" : @(YES), // all Signal-iOS clients support voice
@"video" : @(YES), // all Signal-iOS clients support WebRTC-based voice and video calls. @"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]] @"registrationId" : [NSString stringWithFormat:@"%i", [TSAccountManager getOrGenerateRegistrationId]]
}; } mutableCopy];
if (pin.length > 0) {
result[@"pin"] = pin;
}
return [result copy];
} }
@end @end

@ -3,13 +3,14 @@
// //
#import "OWSRequestFactory.h" #import "OWSRequestFactory.h"
#import "NSData+Base64.h"
#import "OWS2FAManager.h"
#import "OWSDevice.h" #import "OWSDevice.h"
#import "TSAttributes.h" #import "TSAttributes.h"
#import "TSConstants.h" #import "TSConstants.h"
#import "TSRequest.h" #import "TSRequest.h"
#import <AxolotlKit/NSData+keyVersionByte.h> #import <AxolotlKit/NSData+keyVersionByte.h>
#import <AxolotlKit/SignedPreKeyRecord.h> #import <AxolotlKit/SignedPreKeyRecord.h>
#import <SignalServiceKit/NSData+Base64.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -171,10 +172,12 @@ NS_ASSUME_NONNULL_BEGIN
+ (TSRequest *)updateAttributesRequestWithManualMessageFetching:(BOOL)enableManualMessageFetching + (TSRequest *)updateAttributesRequestWithManualMessageFetching:(BOOL)enableManualMessageFetching
{ {
NSString *path = [textSecureAccountsAPI stringByAppendingString:textSecureAttributesAPI]; NSString *path = [textSecureAccountsAPI stringByAppendingString:textSecureAttributesAPI];
NSString *_Nullable pin = [OWS2FAManager.sharedManager pinCode];
return [TSRequest return [TSRequest
requestWithUrl:[NSURL URLWithString:path] requestWithUrl:[NSURL URLWithString:path]
method:@"PUT" method:@"PUT"
parameters:[TSAttributes attributesFromStorageWithManualMessageFetching:enableManualMessageFetching]]; parameters:[TSAttributes attributesFromStorageWithManualMessageFetching:enableManualMessageFetching
pin:pin]];
} }
+ (TSRequest *)unregisterAccountRequest + (TSRequest *)unregisterAccountRequest

@ -24,13 +24,8 @@ NS_ASSUME_NONNULL_BEGIN
NSURL *url = NSURL *url =
[NSURL URLWithString:[NSString stringWithFormat:@"%@/code/%@", textSecureAccountsAPI, verificationCode]]; [NSURL URLWithString:[NSString stringWithFormat:@"%@/code/%@", textSecureAccountsAPI, verificationCode]];
NSMutableDictionary *parameters = NSDictionary *parameters =
[[TSAttributes attributesWithSignalingKey:signalingKey serverAuthToken:authKey manualMessageFetching:NO] [TSAttributes attributesWithSignalingKey:signalingKey serverAuthToken:authKey manualMessageFetching:NO pin:pin];
mutableCopy];
if (pin) {
OWSAssert(pin.length > 0);
parameters[@"pin"] = pin;
}
self = [super initWithURL:url method:@"PUT" parameters:parameters]; self = [super initWithURL:url method:@"PUT" parameters:parameters];
_numberToValidate = phoneNumber; _numberToValidate = phoneNumber;

@ -14,7 +14,6 @@ NS_ASSUME_NONNULL_BEGIN
NSString *const NSNotificationName_2FAStateDidChange = @"NSNotificationName_2FAStateDidChange"; NSString *const NSNotificationName_2FAStateDidChange = @"NSNotificationName_2FAStateDidChange";
NSString *const kOWS2FAManager_Collection = @"kOWS2FAManager_Collection"; NSString *const kOWS2FAManager_Collection = @"kOWS2FAManager_Collection";
NSString *const kOWS2FAManager_IsEnabledKey = @"kOWS2FAManager_IsEnabledKey";
NSString *const kOWS2FAManager_LastSuccessfulReminderDateKey = @"kOWS2FAManager_LastSuccessfulReminderDateKey"; NSString *const kOWS2FAManager_LastSuccessfulReminderDateKey = @"kOWS2FAManager_LastSuccessfulReminderDateKey";
NSString *const kOWS2FAManager_PinCode = @"kOWS2FAManager_PinCode"; NSString *const kOWS2FAManager_PinCode = @"kOWS2FAManager_PinCode";
NSString *const kOWS2FAManager_RepetitionInterval = @"kOWS2FAManager_RepetitionInterval"; NSString *const kOWS2FAManager_RepetitionInterval = @"kOWS2FAManager_RepetitionInterval";
@ -71,16 +70,19 @@ const NSUInteger kDaySecs = kHourSecs * 24;
return self; return self;
} }
- (nullable NSString *)pinCode
{
return [self.dbConnection objectForKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection];
}
- (BOOL)is2FAEnabled - (BOOL)is2FAEnabled
{ {
return [self.dbConnection boolForKey:kOWS2FAManager_IsEnabledKey return self.pinCode != nil;
inCollection:kOWS2FAManager_Collection
defaultValue:NO];
} }
- (void)setIs2FAEnabled:(BOOL)value - (void)set2FANotEnabled
{ {
[self.dbConnection setBool:value forKey:kOWS2FAManager_IsEnabledKey inCollection:kOWS2FAManager_Collection]; [self.dbConnection removeObjectForKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection];
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange [[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange
object:nil object:nil
@ -89,8 +91,13 @@ const NSUInteger kDaySecs = kHourSecs * 24;
- (void)mark2FAAsEnabledWithPin:(NSString *)pin - (void)mark2FAAsEnabledWithPin:(NSString *)pin
{ {
[self setIs2FAEnabled:YES]; OWSAssert(pin.length > 0);
[self storePinCode:pin];
[self.dbConnection setObject:pin forKey:kOWS2FAManager_PinCode inCollection:kOWS2FAManager_Collection];
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange
object:nil
userInfo:nil];
} }
- (void)requestEnable2FAWithPin:(NSString *)pin - (void)requestEnable2FAWithPin:(NSString *)pin
@ -107,6 +114,7 @@ const NSUInteger kDaySecs = kHourSecs * 24;
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
[self mark2FAAsEnabledWithPin:pin]; [self mark2FAAsEnabledWithPin:pin];
if (success) { if (success) {
success(); success();
} }
@ -127,7 +135,7 @@ const NSUInteger kDaySecs = kHourSecs * 24;
success:^(NSURLSessionDataTask *task, id responseObject) { success:^(NSURLSessionDataTask *task, id responseObject) {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
[self setIs2FAEnabled:NO]; [self set2FANotEnabled];
if (success) { if (success) {
success(); success();
@ -145,16 +153,6 @@ const NSUInteger kDaySecs = kHourSecs * 24;
#pragma mark - Reminders #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 - (nullable NSDate *)lastSuccessfulReminderDate
{ {
return [self.dbConnection dateForKey:kOWS2FAManager_LastSuccessfulReminderDateKey return [self.dbConnection dateForKey:kOWS2FAManager_LastSuccessfulReminderDateKey

Loading…
Cancel
Save