Prechádzať zdrojové kódy

add API : Get public keys

Marino Faggiana 7 rokov pred
rodič
commit
b8a492229e

+ 2 - 0
iOSClient/CCGlobal.h

@@ -196,6 +196,7 @@ extern NSString *const dav;
 #define actionUpdateShare                               @"updateShare"
 
 #define actionGetEndToEndPrivateKey                     @"getEndToEndPrivateKey"
+#define actionGetEndToEndPublicKey                      @"getEndToEndPublicKey"
 
 // Metadata : FileType
 #define k_metadataTypeFile_audio                        @"audio"
@@ -242,6 +243,7 @@ extern NSString *const dav;
 #define k_activityDebugActionPushProxy                  @"Subscribing Push Proxy"
 #define k_activityDebugActionServerPush                 @"Subscribing Server Push"
 #define k_activityDebugActionCapabilities               @"Capabilities Of Server"
+#define k_activityDebugActionEndToEndEncryption         @"End To End Encryption "
 
 // Priority Auto Upload
 #define k_priorityAutoUploadImage                       0

+ 2 - 0
iOSClient/Library/OCCommunicationLib/OCCommunication.h

@@ -904,6 +904,8 @@ typedef enum {
 
 - (void)getEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest;
 
+- (void)getEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest;
+
 @end
 
 

+ 21 - 0
iOSClient/Library/OCCommunicationLib/OCCommunication.m

@@ -1974,6 +1974,27 @@
         failureRequest(response, error, request.redirectedServer);
     }];
 }
+
+-(void)getEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
+    
+    serverPath = [serverPath stringByAppendingString:k_url_client_side_encryption];
+    serverPath = [serverPath stringByAppendingString:@"/public-key"];
+    serverPath = [serverPath encodeString:NSUTF8StringEncoding];
+    
+    OCWebDAVClient *request = [OCWebDAVClient new];
+    request = [self getRequestWithCredentials:request];
+    
+    [request getEndToEndPublicKey:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
+        
+        //Return success
+        successRequest(response, request.redirectedServer);
+        
+    } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
+        
+        //Return error
+        failureRequest(response, error, request.redirectedServer);
+    }];
+}
     
 #pragma mark - Clear Cache
 

+ 2 - 0
iOSClient/Library/OCCommunicationLib/OCWebDavClient/OCWebDAVClient.h

@@ -602,5 +602,7 @@ extern NSString * _Nullable OCWebDAVModificationDateKey;
 
 - (void)getEndToEndPrivateKey:(NSString * _Nonnull)serverPath onCommunication:(OCCommunication * _Nonnull)sharedOCComunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response))success failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull operation, id  _Nullable responseObject, NSError * _Nonnull error))failure;
 
+- (void)getEndToEndPublicKey:(NSString * _Nonnull)serverPath onCommunication:(OCCommunication * _Nonnull)sharedOCComunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull operation, id _Nonnull response))success failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull operation, id  _Nullable responseObject, NSError * _Nonnull error))failure;
+
 
 @end

+ 15 - 0
iOSClient/Library/OCCommunicationLib/OCWebDavClient/OCWebDAVClient.m

@@ -862,6 +862,21 @@ NSString const *OCWebDAVModificationDateKey	= @"modificationdate";
     [operation resume];
 }
 
+- (void)getEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
+                      failure:(void(^)(NSHTTPURLResponse *operation, id  _Nullable responseObject, NSError *error))failure{
+    
+    _requestMethod = @"GET";
+    
+    NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
+    serverPath = [serverPath stringByAppendingString:jsonQuery];
+    
+    NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
+    
+    OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
+    [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
+    [operation resume];
+}
+
 #pragma mark - Manage Redirections
 
 - (void) setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{

+ 2 - 2
iOSClient/Login/CCLoginWeb.swift

@@ -64,8 +64,8 @@ extension CCLoginWeb: SwiftModalWebVCDelegate {
                 
                     var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
                     
-                    if (serverUrl.characters.last == "/") {
-                        serverUrl = String(serverUrl.characters.dropLast())
+                    if (serverUrl.last == "/") {
+                        serverUrl = String(serverUrl.dropLast())
                     }
                 
                     let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")

+ 48 - 3
iOSClient/Main/CCMain.m

@@ -1187,6 +1187,9 @@
         
         metadataNet.action = actionGetEndToEndPrivateKey;
         [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
+        
+        metadataNet.action = actionGetEndToEndPublicKey;
+        [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
     }
 }
 
@@ -1237,11 +1240,53 @@
 
 - (void)getEndToEndPrivateKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
 {
-    if (errorCode == 404) {
-        NSLog(@"Chiave non trovata");
+    switch (errorCode) {
+        case 400:
+            message = @"bad request: unpredictable internal error";
+            [app messageNotification:@"E2E private key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
+            break;
+        case 404:
+            message = @"private key doesn't exists";
+            break;
+        case 409:
+            message = @"forbidden: the user can't access the private key";
+            [app messageNotification:@"E2E private key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
+            break;
+        default:
+            [app messageNotification:@"E2E private key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
+            break;
+    }
+    
+    // Activity
+    [[NCManageDatabase sharedInstance] addActivityClient:@"" fileID:@"" action:k_activityDebugActionEndToEndEncryption selector:metadataNet.selector note:message type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:app.activeUrl];
+}
+
+- (void)getEndToEndPublicKeySuccess:(CCMetadataNet *)metadataNet
+{
+    NSLog(@"OK");
+}
+
+- (void)getEndToEndPublicKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
+{
+    switch (errorCode) {
+        case 400:
+            message = @"bad request: unpredictable internal error";
+            [app messageNotification:@"E2E public key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
+            break;
+        case 404:
+            message = @"one or more public keys couldn't be found";
+            break;
+        case 409:
+            message = @"forbidden: the user can't access the public key";
+            [app messageNotification:@"E2E public key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
+            break;
+        default:
+            [app messageNotification:@"E2E public key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
+            break;
     }
     
-    NSLog(@"error");
+    // Activity
+    [[NCManageDatabase sharedInstance] addActivityClient:@"" fileID:@"" action:k_activityDebugActionEndToEndEncryption selector:metadataNet.selector note:message type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:app.activeUrl];
 }
 
 #pragma mark -

+ 2 - 0
iOSClient/Networking/OCNetworking.h

@@ -127,6 +127,8 @@
 // End-to-End Encryption
 - (void)getEndToEndPrivateKeySuccess:(CCMetadataNet *)metadataNet;
 - (void)getEndToEndPrivateKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode;
+- (void)getEndToEndPublicKeySuccess:(CCMetadataNet *)metadataNet;
+- (void)getEndToEndPublicKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode;
 
 @end
 

+ 34 - 4
iOSClient/Networking/OCNetworking.m

@@ -1688,10 +1688,6 @@
         
     } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
         
-        // 409 forbidden: if the user can't access the private key
-        // 404 not found: if the private key doesn't exists
-        // 400 bad request: unpredictable internal error
-        
         NSInteger errorCode = response.statusCode;
         if (errorCode == 0)
             errorCode = error.code;
@@ -1708,6 +1704,40 @@
     }];
 }
 
+- (void)getEndToEndPublicKey
+{
+    OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
+    
+    [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
+    [communication setUserAgent:[CCUtility getUserAgent]];
+    
+    [communication getEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
+        
+        // 200 ok: body contain the public key
+        
+        if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeySuccess:)])
+            [self.delegate getEndToEndPublicKeySuccess:_metadataNet];
+        
+        [self complete];
+        
+    } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
+        
+        NSInteger errorCode = response.statusCode;
+        if (errorCode == 0)
+            errorCode = error.code;
+        
+        // Error
+        if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeyFailure:message:errorCode:)])
+            [self.delegate getEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
+        
+        // Request trusted certificated
+        if ([error code] == NSURLErrorServerCertificateUntrusted)
+            [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
+        
+        [self complete];
+    }];
+}
+
 @end
 
 #pragma --------------------------------------------------------------------------------------------