Class: Pathfinder::OAuth
- Inherits:
-
Object
- Object
- Pathfinder::OAuth
- Defined in:
- lib/pathfinder_dnd/oauth.rb
Overview
Abstracts over the process to OAuth a command-line client with Google. Saves the user’s OAuth refresh token in whatever transactional Pstore-like bucket you provide.
Constant Summary collapse
- CLIENT_ID =
'539096868219.apps.googleusercontent.com'
- CLIENT_SECRET =
'HSJKoPRwscFvYvkP-6HhmEaH'
- SCOPE =
this tells Google what permissions we are requesting I’d prefer to use ReadOnly, but I don’t want to rewrite this gem SCOPE = ‘www.googleapis.com/auth/drive.readonly’
"https://docs.google.com/feeds/ " + "https://docs.googleusercontent.com/ " + "https://spreadsheets.google.com/feeds/"
- REDIRECT_URI =
REDIRECT_URI = ‘localhost’ # see the Google API console
'urn:ietf:wg:oauth:2.0:oob'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Instance Method Summary collapse
-
#authorize ⇒ Object
Round-trip the user throught the Google OAuth process.
-
#initialize(storage) ⇒ OAuth
constructor
Pass in a PSTORE to use for refresh_token storage.
-
#load_token ⇒ Object
create a new session token from whatever refresh token is in the storage.
-
#save_token ⇒ Object
write the refresh token to storage for future use.
Constructor Details
#initialize(storage) ⇒ OAuth
Pass in a PSTORE to use for refresh_token storage
28 29 30 31 32 33 34 35 36 |
# File 'lib/pathfinder_dnd/oauth.rb', line 28 def initialize(storage) @client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, { :site => 'https://accounts.google.com', :authorize_url => '/o/oauth2/auth', :token_url => '/o/oauth2/token' }) @storage = storage end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
25 26 27 |
# File 'lib/pathfinder_dnd/oauth.rb', line 25 def access_token @access_token end |
Instance Method Details
#authorize ⇒ Object
Round-trip the user throught the Google OAuth process
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pathfinder_dnd/oauth.rb', line 39 def () # Step 1 puts "\n\nOpen this URL into your browser to connect this app with Google: " puts @client.auth_code.( :scope => SCOPE, :access_type => 'offline', :redirect_uri => REDIRECT_URI, :approval_prompt => 'force' ) # Step 2 is performed in the browser by the user # Step 3 puts "\n\nPaste the `code` parameter from the redirect URL here to finish authorization: " code = gets.chomp @access_token = @client.auth_code.get_token(code, { :redirect_uri => REDIRECT_URI, :token_method => :post }) end |
#load_token ⇒ Object
create a new session token from whatever refresh token is in the storage.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pathfinder_dnd/oauth.rb', line 75 def load_token() token = nil refresh = nil @storage.transaction do refresh = @storage[:refesh_token] end if refresh.nil? puts 'Could not load OAuth token from storage' return nil end # fuck this @access_token = OAuth2::AccessToken.from_hash(@client, :refresh_token => refresh).refresh! @access_token end |
#save_token ⇒ Object
write the refresh token to storage for future use
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pathfinder_dnd/oauth.rb', line 62 def save_token() if @access_token.nil? raise 'No access token to store' end # wow look at the saftey! @storage.transaction do @storage[:refesh_token] = @access_token.refresh_token end end |