mirror of https://github.com/oxen-io/session-ios
Sketch out 2FA feature.
parent
3f9eb603eb
commit
1f6cbd399e
@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@class TSRequest;
|
||||||
|
|
||||||
|
@interface OWSRequestFactory : NSObject
|
||||||
|
|
||||||
|
- (instancetype)init NS_UNAVAILABLE;
|
||||||
|
|
||||||
|
+ (TSRequest *)enable2FARequestWithPin:(NSString *)pin;
|
||||||
|
|
||||||
|
+ (TSRequest *)disable2FARequest;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWSRequestFactory.h"
|
||||||
|
#import "TSConstants.h"
|
||||||
|
#import "TSRequest.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@implementation OWSRequestFactory
|
||||||
|
|
||||||
|
+ (TSRequest *)enable2FARequestWithPin:(NSString *)pin;
|
||||||
|
{
|
||||||
|
OWSAssert(pin.length > 0);
|
||||||
|
|
||||||
|
return [TSRequest requestWithUrl:[NSURL URLWithString:textSecure2FAAPI]
|
||||||
|
method:@"PUT"
|
||||||
|
parameters:@{
|
||||||
|
@"pin" : pin,
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (TSRequest *)disable2FARequest
|
||||||
|
{
|
||||||
|
return [TSRequest requestWithUrl:[NSURL URLWithString:textSecure2FAAPI] method:@"DELETE" parameters:@{}];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -1,11 +1,13 @@
|
|||||||
//
|
//
|
||||||
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
@interface TSRequest : NSMutableURLRequest
|
@interface TSRequest : NSMutableURLRequest
|
||||||
|
|
||||||
@property (nonatomic, retain) NSMutableDictionary *parameters;
|
@property (nonatomic) NSDictionary *parameters;
|
||||||
|
|
||||||
- (void)makeAuthenticatedRequest;
|
+ (instancetype)requestWithUrl:(NSURL *)url
|
||||||
|
method:(NSString *)method
|
||||||
|
parameters:(NSDictionary<NSString *, id> *)parameters;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
typedef void (^OWS2FASuccess)(void);
|
||||||
|
typedef void (^OWS2FAFailure)(NSError *error);
|
||||||
|
|
||||||
|
// This class can be safely accessed and used from any thread.
|
||||||
|
@interface OWS2FAManager : NSObject
|
||||||
|
|
||||||
|
- (instancetype)init NS_UNAVAILABLE;
|
||||||
|
|
||||||
|
+ (instancetype)sharedManager;
|
||||||
|
|
||||||
|
- (BOOL)is2FAEnabled;
|
||||||
|
|
||||||
|
- (void)enable2FAWithPin:(NSString *)pin success:(OWS2FASuccess)success failure:(OWS2FAFailure)failure;
|
||||||
|
|
||||||
|
- (void)disable2FAWithSuccess:(OWS2FASuccess)success failure:(OWS2FAFailure)failure;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,105 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWS2FAManager.h"
|
||||||
|
#import "OWSRequestFactory.h"
|
||||||
|
#import "TSNetworkManager.h"
|
||||||
|
#import "TSStorageManager.h"
|
||||||
|
#import "YapDatabaseConnection+OWS.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
NSString *const kOWS2FAManager_Collection = @"kOWS2FAManager_Collection";
|
||||||
|
NSString *const kOWS2FAManager_IsEnabledKey = @"kOWS2FAManager_IsEnabledKey";
|
||||||
|
|
||||||
|
@interface OWS2FAManager ()
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
||||||
|
@property (nonatomic, readonly) TSNetworkManager *networkManager;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
#pragma mark -
|
||||||
|
|
||||||
|
@implementation OWS2FAManager
|
||||||
|
|
||||||
|
+ (instancetype)sharedManager
|
||||||
|
{
|
||||||
|
static OWS2FAManager *sharedMyManager = nil;
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
sharedMyManager = [[self alloc] initDefault];
|
||||||
|
});
|
||||||
|
return sharedMyManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)initDefault
|
||||||
|
{
|
||||||
|
TSStorageManager *storageManager = [TSStorageManager sharedManager];
|
||||||
|
TSNetworkManager *networkManager = [TSNetworkManager sharedManager];
|
||||||
|
|
||||||
|
return [self initWithStorageManager:storageManager networkManager:networkManager];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager
|
||||||
|
networkManager:(TSNetworkManager *)networkManager
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
|
||||||
|
if (!self) {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
OWSAssert(storageManager);
|
||||||
|
OWSAssert(networkManager);
|
||||||
|
|
||||||
|
_dbConnection = storageManager.newDatabaseConnection;
|
||||||
|
_networkManager = networkManager;
|
||||||
|
|
||||||
|
OWSSingletonAssert();
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)is2FAEnabled
|
||||||
|
{
|
||||||
|
return [self.dbConnection boolForKey:kOWS2FAManager_IsEnabledKey
|
||||||
|
inCollection:kOWS2FAManager_Collection
|
||||||
|
defaultValue:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)enable2FAWithPin:(NSString *)pin success:(OWS2FASuccess)success failure:(OWS2FAFailure)failure
|
||||||
|
{
|
||||||
|
OWSAssert(pin.length > 0);
|
||||||
|
OWSAssert(success);
|
||||||
|
OWSAssert(failure);
|
||||||
|
|
||||||
|
TSRequest *request = [OWSRequestFactory enable2FARequestWithPin:pin];
|
||||||
|
[self.networkManager makeRequest:request
|
||||||
|
success:^(NSURLSessionDataTask *task, id responseObject) {
|
||||||
|
success();
|
||||||
|
}
|
||||||
|
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
||||||
|
failure(error);
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)disable2FAWithSuccess:(OWS2FASuccess)success failure:(OWS2FAFailure)failure
|
||||||
|
{
|
||||||
|
OWSAssert(success);
|
||||||
|
OWSAssert(failure);
|
||||||
|
|
||||||
|
TSRequest *request = [OWSRequestFactory disable2FARequest];
|
||||||
|
[self.networkManager makeRequest:request
|
||||||
|
success:^(NSURLSessionDataTask *task, id responseObject) {
|
||||||
|
success();
|
||||||
|
}
|
||||||
|
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
||||||
|
failure(error);
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue