浏览代码

gdrive controller: add token sharing

Felix Paul Kühne 9 年之前
父节点
当前提交
76609b95d5
共有 2 个文件被更改,包括 35 次插入1 次删除
  1. 1 0
      Sources/VLCConstants.h
  2. 34 1
      Sources/VLCGoogleDriveController.m

+ 1 - 0
Sources/VLCConstants.h

@@ -89,6 +89,7 @@
 #define kVLCStoreDropboxCredentials @"kVLCStoreDropboxCredentials"
 #define kVLCStoreOneDriveCredentials @"kVLCStoreOneDriveCredentials"
 #define kVLCStoreBoxCredentials @"kVLCStoreBoxCredentials"
+#define kVLCStoreGDriveCredentials @"kVLCStoreGDriveCredentials"
 
 #define kVLCUserActivityPlaying @"org.videolan.vlc-ios.playing"
 #define kVLCUserActivityLibrarySelection @"org.videolan.vlc-ios.libraryselection"

+ 34 - 1
Sources/VLCGoogleDriveController.m

@@ -15,6 +15,7 @@
 #import "NSString+SupportedMedia.h"
 #import "VLCPlaybackController.h"
 #import "VLCMediaFileDiscoverer.h"
+#import "SSKeychain.h"
 
 @interface VLCGoogleDriveController ()
 {
@@ -54,6 +55,7 @@
 
 - (void)startSession
 {
+    [self restoreFromSharedCredentials];
     self.driveService = [GTLServiceDrive new];
     self.driveService.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kVLCGoogleDriveClientID clientSecret:kVLCGoogleDriveClientSecret];
 }
@@ -69,6 +71,9 @@
 {
     [GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName];
     self.driveService.authorizer = nil;
+    NSUbiquitousKeyValueStore *ubiquitousStore = [NSUbiquitousKeyValueStore defaultStore];
+    [ubiquitousStore setString:nil forKey:kVLCStoreGDriveCredentials];
+    [ubiquitousStore synchronize];
     [self stopSession];
     if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
         [self.delegate mediaListUpdated];
@@ -79,7 +84,35 @@
     if (!self.driveService) {
         [self startSession];
     }
-    return [((GTMOAuth2Authentication *)self.driveService.authorizer) canAuthorize];
+    BOOL ret = [((GTMOAuth2Authentication *)self.driveService.authorizer) canAuthorize];
+    if (ret) {
+        [self shareCredentials];
+    }
+    return ret;
+}
+
+- (void)shareCredentials
+{
+    /* share our credentials */
+    NSString *credentials = [SSKeychain passwordForService:kKeychainItemName account:@"OAuth"]; // kGTMOAuth2AccountName
+    if (credentials == nil)
+        return;
+
+    NSUbiquitousKeyValueStore *ubiquitousStore = [NSUbiquitousKeyValueStore defaultStore];
+    [ubiquitousStore setString:credentials forKey:kVLCStoreGDriveCredentials];
+    [ubiquitousStore synchronize];
+}
+
+- (BOOL)restoreFromSharedCredentials
+{
+    NSUbiquitousKeyValueStore *ubiquitousStore = [NSUbiquitousKeyValueStore defaultStore];
+    [ubiquitousStore synchronize];
+    NSString *credentials = [ubiquitousStore stringForKey:kVLCStoreGDriveCredentials];
+    if (!credentials)
+        return NO;
+
+    [SSKeychain setPassword:credentials forService:kKeychainItemName account:@"OAuth"]; // kGTMOAuth2AccountName
+    return YES;
 }
 
 - (void)showAlert:(NSString *)title message:(NSString *)message