Skip to content

Commit

Permalink
Supporting passing OAuth2 access_token string directly in GoogleDrive…
Browse files Browse the repository at this point in the history
….login_with_oauth. #12
  • Loading branch information
gimite committed Jan 20, 2013
1 parent 2e6bdf0 commit 0be10b8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 72 deletions.
19 changes: 13 additions & 6 deletions lib/google_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def self.login(mail, password, proxy = nil)

# Authenticates with given OAuth1 or OAuth2 token.
#
# +access_token+ can be either OAuth2 access_token string, OAuth2::AccessToken or OAuth::AccessToken.
#
# +proxy+ can be nil or return value of Net::HTTP.Proxy. If +proxy+ is specified, all
# HTTP access in the session uses the proxy. If +proxy+ is nil, it uses the proxy
# specified by http_proxy environment variable if available. Otherwise it performs direct
# access.
#
# OAuth2 code example:
#
# client = OAuth2::Client.new(
Expand All @@ -36,14 +43,14 @@ def self.login(mail, password, proxy = nil)
# # Redirect the user to auth_url and get authorization code from redirect URL.
# auth_token = client.auth_code.get_token(
# authorization_code, :redirect_uri => "http://example.com/")
# session = GoogleDrive.login_with_oauth(auth_token)
# session = GoogleDrive.login_with_oauth(auth_token.token)
#
# Or, from existing refresh token:
#
# access_token = OAuth2::AccessToken.from_hash(client,
# auth_token = OAuth2::AccessToken.from_hash(client,
# {:refresh_token => refresh_token, :expires_at => expires_at})
# access_token = access_token.refresh!
# session = GoogleDrive.login_with_oauth(access_token)
# auth_token = auth_token.refresh!
# session = GoogleDrive.login_with_oauth(auth_token.token)
#
# If your app is not a Web app, use "urn:ietf:wg:oauth:2.0:oob" as redirect_url. Then
# authorization code is shown after authorization.
Expand All @@ -68,8 +75,8 @@ def self.login(mail, password, proxy = nil)
# - http://code.google.com/apis/accounts/docs/OAuth2.html
# - http://oauth.rubyforge.org/
# - http://code.google.com/apis/accounts/docs/OAuth.html
def self.login_with_oauth(oauth_token)
return Session.login_with_oauth(oauth_token)
def self.login_with_oauth(access_token, proxy = nil)
return Session.login_with_oauth(access_token, proxy)
end

# Restores session using return value of auth_tokens method of previous session.
Expand Down
48 changes: 48 additions & 0 deletions lib/google_drive/basic_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Author: Hiroshi Ichikawa <http://gimite.net/>
# The license of this source is "New BSD Licence"

require "net/https"
require "uri"
Net::HTTP.version_1_2


module GoogleDrive

class BasicFetcher #:nodoc:

def initialize(proxy)
if proxy
@proxy = proxy
elsif ENV["http_proxy"] && !ENV["http_proxy"].empty?
proxy_url = URI.parse(ENV["http_proxy"])
@proxy = Net::HTTP.Proxy(proxy_url.host, proxy_url.port)
else
@proxy = Net::HTTP
end
end

def request_raw(method, url, data, extra_header, auth)
uri = URI.parse(url)
http = @proxy.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start() do
path = uri.path + (uri.query ? "?#{uri.query}" : "")
header = auth_header(auth).merge(extra_header)
if method == :delete || method == :get
return http.__send__(method, path, header)
else
return http.__send__(method, path, data, header)
end
end
end

private

def auth_header(auth)
return {}
end

end

end
31 changes: 3 additions & 28 deletions lib/google_drive/client_login_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,20 @@
# Author: Hiroshi Ichikawa <http://gimite.net/>
# The license of this source is "New BSD Licence"

require "net/https"
require "uri"
Net::HTTP.version_1_2
require "google_drive/basic_fetcher"


module GoogleDrive

class ClientLoginFetcher #:nodoc:
class ClientLoginFetcher < BasicFetcher #:nodoc:

def initialize(auth_tokens, proxy)
super(proxy)
@auth_tokens = auth_tokens
if proxy
@proxy = proxy
elsif ENV["http_proxy"] && !ENV["http_proxy"].empty?
proxy_url = URI.parse(ENV["http_proxy"])
@proxy = Net::HTTP.Proxy(proxy_url.host, proxy_url.port)
else
@proxy = Net::HTTP
end
end

attr_accessor(:auth_tokens)

def request_raw(method, url, data, extra_header, auth)
uri = URI.parse(url)
http = @proxy.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start() do
path = uri.path + (uri.query ? "?#{uri.query}" : "")
header = auth_header(auth).merge(extra_header)
if method == :delete || method == :get
return http.__send__(method, path, header)
else
return http.__send__(method, path, data, header)
end
end
end

private

def auth_header(auth)
Expand Down
43 changes: 10 additions & 33 deletions lib/google_drive/oauth2_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
# Author: Hiroshi Ichikawa <http://gimite.net/>
# The license of this source is "New BSD Licence"

require "rubygems"
require "oauth2"
require "google_drive/basic_fetcher"


module GoogleDrive

class OAuth2Fetcher #:nodoc:
class OAuth2Fetcher < BasicFetcher #:nodoc:

class Response

def initialize(raw_res)
@raw_res = raw_res
end

def code
return @raw_res.status.to_s()
end

def body
return @raw_res.body
end

def [](name)
return @raw_res.headers[name]
end

def initialize(auth_token, proxy)
super(proxy)
@auth_token = auth_token
end

def initialize(oauth2_token)
@oauth2_token = oauth2_token
end

def request_raw(method, url, data, extra_header, auth)
if method == :delete || method == :get
raw_res = @oauth2_token.request(method, url, {:headers => extra_header})
else
raw_res = @oauth2_token.request(method, url, {:headers => extra_header, :body => data})
end
return Response.new(raw_res)
private

def auth_header(auth)
return {"Authorization" => "Bearer %s" % @auth_token}
end

end

end
15 changes: 10 additions & 5 deletions lib/google_drive/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

require "rubygems"
require "nokogiri"
require "oauth"
require "oauth2"

require "google_drive/util"
require "google_drive/client_login_fetcher"
Expand Down Expand Up @@ -38,15 +40,18 @@ def self.login(mail, password, proxy = nil)
end

# The same as GoogleDrive.login_with_oauth.
def self.login_with_oauth(oauth_token)
case oauth_token
def self.login_with_oauth(access_token, proxy = nil)
case access_token
when OAuth::AccessToken
fetcher = OAuth1Fetcher.new(oauth_token)
raise(GoogleDrive::Error, "proxy is not supported with OAuth1.") if proxy
fetcher = OAuth1Fetcher.new(access_token)
when OAuth2::AccessToken
fetcher = OAuth2Fetcher.new(oauth_token)
fetcher = OAuth2Fetcher.new(access_token.token, proxy)
when String
fetcher = OAuth2Fetcher.new(access_token, proxy)
else
raise(GoogleDrive::Error,
"oauth_token is neither OAuth::Token nor OAuth2::Token: %p" % oauth_token)
"access_token is neither String, OAuth2::Token nor OAuth::Token: %p" % access_token)
end
return Session.new(nil, fetcher)
end
Expand Down

0 comments on commit 0be10b8

Please sign in to comment.