Replace deprecated method that cause ruby warnings File.exit?
[arvados.git] / lib / google / api_client / client_secrets.rb
index ab299d6144182a10ed69c4f31866a2c9bc626b3d..0432671d42e0bff066cd87f373412a26ce0ece76 100644 (file)
 
 
 require 'multi_json'
+require 'compat/multi_json'
 
 
 module Google
   class APIClient
     ##
-    # Manages the persistence of client configuration data and secrets.
+    # Manages the persistence of client configuration data and secrets. Format
+    # inspired by the Google API Python client.
+    #
+    # @see https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
+    #
+    # @example
+    #   {
+    #     "web": {
+    #       "client_id": "asdfjasdljfasdkjf",
+    #       "client_secret": "1912308409123890",
+    #       "redirect_uris": ["https://www.example.com/oauth2callback"],
+    #       "auth_uri": "https://accounts.google.com/o/oauth2/auth",
+    #       "token_uri": "https://accounts.google.com/o/oauth2/token"
+    #     }
+    #   }
+    #
+    # @example
+    #   {
+    #     "installed": {
+    #       "client_id": "837647042410-75ifg...usercontent.com",
+    #       "client_secret":"asdlkfjaskd",
+    #       "redirect_uris": ["http://localhost", "urn:ietf:oauth:2.0:oob"],
+    #       "auth_uri": "https://accounts.google.com/o/oauth2/auth",
+    #       "token_uri": "https://accounts.google.com/o/oauth2/token"
+    #     }
+    #   }
     class ClientSecrets
+      
+      ##
+      # Reads client configuration from a file
+      #
+      # @param [String] filename
+      #   Path to file to load
+      #
+      # @return [Google::APIClient::ClientSecrets]
+      #   OAuth client settings
       def self.load(filename=nil)
         if filename && File.directory?(filename)
           search_path = File.expand_path(filename)
@@ -28,8 +63,7 @@ module Google
         end
         while filename == nil
           search_path ||= File.expand_path('.')
-          puts search_path
-          if File.exist?(File.join(search_path, 'client_secrets.json'))
+          if File.exists?(File.join(search_path, 'client_secrets.json'))
             filename = File.join(search_path, 'client_secrets.json')
           elsif search_path == '/' || search_path =~ /[a-zA-Z]:[\/\\]/
             raise ArgumentError,
@@ -39,10 +73,15 @@ module Google
             search_path = File.expand_path(File.join(search_path, '..'))
           end
         end
-        data = File.open(filename, 'r') { |file| MultiJson.decode(file.read) }
+        data = File.open(filename, 'r') { |file| MultiJson.load(file.read) }
         return self.new(data)
       end
 
+      ##
+      # Intialize OAuth client settings.
+      #
+      # @param [Hash] options
+      #   Parsed client secrets files
       def initialize(options={})
         # Client auth configuration
         @flow = options[:flow] || options.keys.first.to_s || 'web'
@@ -76,8 +115,13 @@ module Google
         :refresh_token, :id_token, :expires_in, :expires_at, :issued_at
       )
 
+      ##
+      # Serialize back to the original JSON form
+      #
+      # @return [String]
+      #   JSON
       def to_json
-        return MultiJson.encode({
+        return MultiJson.dump({
           self.flow => ({
             'client_id' => self.client_id,
             'client_secret' => self.client_secret,
@@ -100,6 +144,33 @@ module Google
           end
         })
       end
+      
+      def to_authorization
+        gem 'signet', '>= 0.4.0'
+        require 'signet/oauth_2/client'
+        # NOTE: Do not rely on this default value, as it may change
+        new_authorization = Signet::OAuth2::Client.new
+        new_authorization.client_id = self.client_id
+        new_authorization.client_secret = self.client_secret
+        new_authorization.authorization_uri = (
+          self.authorization_uri ||
+          'https://accounts.google.com/o/oauth2/auth'
+        )
+        new_authorization.token_credential_uri = (
+          self.token_credential_uri ||
+          'https://accounts.google.com/o/oauth2/token'
+        )
+        new_authorization.redirect_uri = self.redirect_uris.first
+
+        # These are supported, but unlikely.
+        new_authorization.access_token = self.access_token
+        new_authorization.refresh_token = self.refresh_token
+        new_authorization.id_token = self.id_token
+        new_authorization.expires_in = self.expires_in
+        new_authorization.issued_at = self.issued_at if self.issued_at
+        new_authorization.expires_at = self.expires_at if self.expires_at
+        return new_authorization
+      end
     end
   end
 end