|
|
|
@ -5,18 +5,47 @@
|
|
|
|
|
#import "OWSRequestFactory.h"
|
|
|
|
|
#import "OWS2FAManager.h"
|
|
|
|
|
#import "OWSDevice.h"
|
|
|
|
|
#import "TSAttributes.h"
|
|
|
|
|
#import "ProfileManagerProtocol.h"
|
|
|
|
|
#import "SSKEnvironment.h"
|
|
|
|
|
#import "TSAccountManager.h"
|
|
|
|
|
#import "TSConstants.h"
|
|
|
|
|
#import "TSRequest.h"
|
|
|
|
|
#import <AxolotlKit/NSData+keyVersionByte.h>
|
|
|
|
|
#import <AxolotlKit/SignedPreKeyRecord.h>
|
|
|
|
|
#import <Curve25519Kit/Curve25519.h>
|
|
|
|
|
#import <SignalCoreKit/Cryptography.h>
|
|
|
|
|
#import <SignalCoreKit/NSData+OWS.h>
|
|
|
|
|
#import <SignalMetadataKit/SignalMetadataKit-Swift.h>
|
|
|
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@implementation OWSRequestFactory
|
|
|
|
|
|
|
|
|
|
#pragma mark - Dependencies
|
|
|
|
|
|
|
|
|
|
+ (TSAccountManager *)tsAccountManager
|
|
|
|
|
{
|
|
|
|
|
return TSAccountManager.sharedInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (OWS2FAManager *)ows2FAManager
|
|
|
|
|
{
|
|
|
|
|
return OWS2FAManager.sharedManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id<ProfileManagerProtocol>)profileManager
|
|
|
|
|
{
|
|
|
|
|
return SSKEnvironment.shared.profileManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id<OWSUDManager>)udManager
|
|
|
|
|
{
|
|
|
|
|
return SSKEnvironment.shared.udManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
+ (TSRequest *)enable2FARequestWithPin:(NSString *)pin
|
|
|
|
|
{
|
|
|
|
|
OWSAssertDebug(pin.length > 0);
|
|
|
|
@ -165,15 +194,20 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (TSRequest *)updateAttributesRequestWithManualMessageFetching:(BOOL)enableManualMessageFetching
|
|
|
|
|
+ (TSRequest *)updateAttributesRequest
|
|
|
|
|
{
|
|
|
|
|
NSString *path = [textSecureAccountsAPI stringByAppendingString:textSecureAttributesAPI];
|
|
|
|
|
NSString *_Nullable pin = [OWS2FAManager.sharedManager pinCode];
|
|
|
|
|
return [TSRequest
|
|
|
|
|
requestWithUrl:[NSURL URLWithString:path]
|
|
|
|
|
method:@"PUT"
|
|
|
|
|
parameters:[TSAttributes attributesFromStorageWithManualMessageFetching:enableManualMessageFetching
|
|
|
|
|
pin:pin]];
|
|
|
|
|
|
|
|
|
|
NSString *signalingKey = self.tsAccountManager.signalingKey;
|
|
|
|
|
OWSAssertDebug(signalingKey.length > 0);
|
|
|
|
|
NSString *authKey = self.tsAccountManager.serverAuthToken;
|
|
|
|
|
OWSAssertDebug(authKey.length > 0);
|
|
|
|
|
NSString *_Nullable pin = [self.ows2FAManager pinCode];
|
|
|
|
|
|
|
|
|
|
NSDictionary<NSString *, id> *accountAttributes =
|
|
|
|
|
[self accountAttributesWithPin:pin signalingKey:signalingKey authKey:authKey];
|
|
|
|
|
|
|
|
|
|
return [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:accountAttributes];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (TSRequest *)unregisterAccountRequest
|
|
|
|
@ -205,6 +239,69 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (TSRequest *)verifyCodeRequestWithVerificationCode:(NSString *)verificationCode
|
|
|
|
|
forNumber:(NSString *)phoneNumber
|
|
|
|
|
pin:(nullable NSString *)pin
|
|
|
|
|
signalingKey:(NSString *)signalingKey
|
|
|
|
|
authKey:(NSString *)authKey
|
|
|
|
|
{
|
|
|
|
|
OWSAssertDebug(verificationCode.length > 0);
|
|
|
|
|
OWSAssertDebug(phoneNumber.length > 0);
|
|
|
|
|
OWSAssertDebug(signalingKey.length > 0);
|
|
|
|
|
OWSAssertDebug(authKey.length > 0);
|
|
|
|
|
|
|
|
|
|
NSString *path = [NSString stringWithFormat:@"%@/code/%@", textSecureAccountsAPI, verificationCode];
|
|
|
|
|
|
|
|
|
|
NSMutableDictionary<NSString *, id> *accountAttributes =
|
|
|
|
|
[[self accountAttributesWithPin:pin signalingKey:signalingKey authKey:authKey] mutableCopy];
|
|
|
|
|
[accountAttributes removeObjectForKey:@"AuthKey"];
|
|
|
|
|
|
|
|
|
|
TSRequest *request =
|
|
|
|
|
[TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:accountAttributes];
|
|
|
|
|
// The "verify code" request handles auth differently.
|
|
|
|
|
request.authUsername = phoneNumber;
|
|
|
|
|
request.authPassword = authKey;
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSDictionary<NSString *, id> *)accountAttributesWithPin:(nullable NSString *)pin
|
|
|
|
|
signalingKey:(NSString *)signalingKey
|
|
|
|
|
authKey:(NSString *)authKey
|
|
|
|
|
{
|
|
|
|
|
OWSAssertDebug(signalingKey.length > 0);
|
|
|
|
|
OWSAssertDebug(authKey.length > 0);
|
|
|
|
|
uint32_t registrationId = [self.tsAccountManager getOrGenerateRegistrationId];
|
|
|
|
|
|
|
|
|
|
BOOL isManualMessageFetchEnabled = self.tsAccountManager.isManualMessageFetchEnabled;
|
|
|
|
|
|
|
|
|
|
OWSAES256Key *profileKey = [self.profileManager localProfileKey];
|
|
|
|
|
NSError *error;
|
|
|
|
|
SMKUDAccessKey *_Nullable udAccessKey = [[SMKUDAccessKey alloc] initWithProfileKey:profileKey.keyData error:&error];
|
|
|
|
|
if (error || udAccessKey.keyData.length < 1) {
|
|
|
|
|
// Crash app if UD cannot be enabled.
|
|
|
|
|
OWSFail(@"Could not determine UD access key: %@.", error);
|
|
|
|
|
}
|
|
|
|
|
BOOL allowUnrestrictedUD = [self.udManager shouldAllowUnrestrictedAccess] && udAccessKey != nil;
|
|
|
|
|
|
|
|
|
|
NSMutableDictionary *accountAttributes = [@{
|
|
|
|
|
@"signalingKey" : signalingKey,
|
|
|
|
|
@"AuthKey" : authKey,
|
|
|
|
|
@"voice" : @(YES), // all Signal-iOS clients support voice
|
|
|
|
|
@"video" : @(YES), // all Signal-iOS clients support WebRTC-based voice and video calls.
|
|
|
|
|
@"fetchesMessages" : @(isManualMessageFetchEnabled), // devices that don't support push must tell the server
|
|
|
|
|
// they fetch messages manually
|
|
|
|
|
@"registrationId" : [NSString stringWithFormat:@"%i", registrationId],
|
|
|
|
|
@"unidentifiedAccessKey" : udAccessKey.keyData.base64EncodedString,
|
|
|
|
|
@"unrestrictedUnidentifiedAccess" : @(allowUnrestrictedUD),
|
|
|
|
|
} mutableCopy];
|
|
|
|
|
|
|
|
|
|
if (pin.length > 0) {
|
|
|
|
|
accountAttributes[@"pin"] = pin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [accountAttributes copy];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (TSRequest *)submitMessageRequestWithRecipient:(NSString *)recipientId
|
|
|
|
|
messages:(NSArray *)messages
|
|
|
|
|
timeStamp:(uint64_t)timeStamp
|
|
|
|
|