Add oauth helper for installed apps, update CLI
[arvados.git] / lib / google / api_client / auth / installed_app.rb
1 # Copyright 2010 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'webrick'
16 require 'launchy'
17
18 module Google
19   class APIClient
20
21     # Small helper for the sample apps for performing OAuth 2.0 flows from the command
22     # line or in any other installed app environment.
23     #
24     # @example
25     #
26     #    client = Google::APIClient.new
27     #    flow = Google::APIClient::InstalledAppFlow.new(
28     #      :client_id => '691380668085.apps.googleusercontent.com',
29     #      :client_secret => '...,
30     #      :scope => 'https://www.googleapis.com/auth/drive'
31     #    )
32     #    client.authorization = flow.authorize
33     #
34     class InstalledAppFlow
35       
36       RESPONSE_BODY = <<-HTML
37         <html>
38           <head>
39             <script>
40               function closeWindow() { 
41                 window.open('', '_self', '');
42                 window.close();
43               }
44               setTimeout(closeWindow, 10);
45             </script>
46           </head>
47           <body>You may close this window.</body>
48         </html>
49       HTML
50       
51       ##
52       # Configure the flow
53       #
54       # @param [Hash] options The configuration parameters for the client.
55       # @option options [Fixnum] :port
56       #   Port to run the embedded server on. Defaults to 9292
57       # @option options [String] :client_id 
58       #   A unique identifier issued to the client to identify itself to the
59       #   authorization server.
60       # @option options [String] :client_secret
61       #   A shared symmetric secret issued by the authorization server,
62       #   which is used to authenticate the client.
63       # @option options [String] :scope
64       #   The scope of the access request, expressed either as an Array
65       #   or as a space-delimited String.
66       #
67       # @see Signet::OAuth2::Client
68       def initialize(options)
69         @port = options[:port] || 9292
70         @authorization = Signet::OAuth2::Client.new({
71           :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
72           :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
73           :redirect_uri => "http://localhost:#{@port}/"}.update(options)
74         )
75       end
76       
77       ##
78       # Request authorization. Opens a browser and waits for response.
79       #
80       # @return [Signet::OAuth2::Client]
81       #  Authorization instance, nil if user cancelled.
82       def authorize
83         auth = @authorization
84     
85         server = WEBrick::HTTPServer.new(
86           :Port => @port,
87           :BindAddress =>"localhost",
88           :Logger => WEBrick::Log.new(STDOUT, 0),
89           :AccessLog => []
90         )
91         trap("INT") { server.shutdown }
92         
93         server.mount_proc '/' do |req, res|
94             auth.code = req.query['code']
95             if auth.code
96               auth.fetch_access_token!
97             end
98             res.status = WEBrick::HTTPStatus::RC_ACCEPTED
99             res.body = RESPONSE_BODY
100             server.stop
101         end
102
103         Launchy.open(auth.authorization_uri.to_s)
104         server.start
105         if @authorization.access_token
106           return @authorization
107         else
108           return nil
109         end
110       end
111     end
112
113   end
114 end
115