Class: GmailCli::Oauth2Helper
- Inherits:
-
Object
- Object
- GmailCli::Oauth2Helper
- Includes:
- LoggerSupport
- Defined in:
- lib/gmail_cli/oauth2_helper.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#application_name ⇒ Object
Returns the value of attribute application_name.
-
#application_version ⇒ Object
Returns the value of attribute application_version.
-
#authorization_code ⇒ Object
Returns the value of attribute authorization_code.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
Returns the value of attribute scope.
Class Method Summary collapse
-
.authorize!(options = {}) ⇒ Object
Command: convenience class method to invoke authorization phase.
Instance Method Summary collapse
- #api_client ⇒ Object
- #ask_for_entry(prompt) ⇒ Object
-
#authorize! ⇒ Object
Command: runs an interactive authorization phase.
- #echo(text) ⇒ Object
- #ensure_provided(key) ⇒ Object
- #fetch_access_token! ⇒ Object
- #fetch_refresh_token! ⇒ Object
- #get_access_token! ⇒ Object
- #get_authorization_uri ⇒ Object
-
#initialize(options = {}) ⇒ Oauth2Helper
constructor
A new instance of Oauth2Helper.
-
#refresh_access_token! ⇒ Object
Command: requests and returns a fresh access_token.
Methods included from LoggerSupport
Constructor Details
#initialize(options = {}) ⇒ Oauth2Helper
Returns a new instance of Oauth2Helper.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 19 def initialize(={}) @client_id = [:client_id] @client_secret = [:client_secret] @access_token = [:access_token] @refresh_token = [:refresh_token] @scope = [:scope] || 'https://mail.google.com/' @redirect_uri = [:redirect_uri] || 'urn:ietf:wg:oauth:2.0:oob' @application_name = [:application_name] || 'gmail_cli' @application_version = [:application_version] || GmailCli::VERSION trace "#{self.class.name} resolved options", { client_id: client_id, client_secret: client_secret, access_token: access_token, refresh_token: refresh_token, scope: scope, redirect_uri: redirect_uri, application_name: application_name, application_version: application_version } end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
16 17 18 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 16 def access_token @access_token end |
#application_name ⇒ Object
Returns the value of attribute application_name.
17 18 19 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 17 def application_name @application_name end |
#application_version ⇒ Object
Returns the value of attribute application_version.
17 18 19 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 17 def application_version @application_version end |
#authorization_code ⇒ Object
Returns the value of attribute authorization_code.
16 17 18 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 16 def end |
#client_id ⇒ Object
Returns the value of attribute client_id.
15 16 17 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 15 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
15 16 17 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 15 def client_secret @client_secret end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
17 18 19 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 17 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
16 17 18 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 16 def refresh_token @refresh_token end |
#scope ⇒ Object
Returns the value of attribute scope.
17 18 19 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 17 def scope @scope end |
Class Method Details
.authorize!(options = {}) ⇒ Object
Command: convenience class method to invoke authorization phase
8 9 10 11 12 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 8 def (={}) new(). rescue Interrupt $stderr.puts "..interrupted." end |
Instance Method Details
#api_client ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 57 def api_client @api_client ||= if ensure_provided(:client_id) && ensure_provided(:client_secret) && ensure_provided(:redirect_uri) && ensure_provided(:scope) # Initialize OAuth 2.0 client api_client = Google::APIClient.new(application_name: application_name, application_version: application_version) api_client..client_id = client_id api_client..client_secret = client_secret api_client..redirect_uri = redirect_uri api_client..scope = scope api_client end end |
#ask_for_entry(prompt) ⇒ Object
120 121 122 123 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 120 def ask_for_entry(prompt) print prompt STDIN.gets.chomp! end |
#authorize! ⇒ Object
Command: runs an interactive authorization phase
50 51 52 53 54 55 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 50 def echo %( Performing Google OAuth2 client authorization ---------------------------------------------) get_access_token! end |
#echo(text) ⇒ Object
36 37 38 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 36 def echo(text) puts text end |
#ensure_provided(key) ⇒ Object
116 117 118 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 116 def ensure_provided(key) eval("self.#{key} ||= ask_for_entry('#{key}: ')") end |
#fetch_access_token! ⇒ Object
108 109 110 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 108 def fetch_access_token! api_client..fetch_access_token! end |
#fetch_refresh_token! ⇒ Object
112 113 114 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 112 def fetch_refresh_token! api_client..refresh! end |
#get_access_token! ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 69 def get_access_token! # Request authorization = echo %( Go to the following URL in a web browser to grant the authorization. There you will be able to select specifically which gmail account the authorization is for. #{authorization_uri} When you have done that successfully it will provide a code to enter here: ) api_client..code = ensure_provided(:authorization_code) response = fetch_access_token! # => {"access_token"=>"ya29.AHES6ZS_KHUpdO5P0nyvADWf4tL5o8e8C_q5UK0HyyYOF3jw", "token_type"=>"Bearer", "expires_in"=>3600, "refresh_token"=>"1/o4DFZX1_iu_riPiu-OO6FLJ9M8pE5QWmY5DDoUHyOGw"} trace "#{__method__} response", response self.access_token = response['access_token'] self.refresh_token = response['refresh_token'] echo %( Authorization was successful! You can now use this credential to access gmail. For example, to get an authenticated IMAP connection to the '[email protected]' account: credentials = { client_id: '#{client_id}', client_secret: '#{client_secret}', access_token: '#{access_token}', refresh_token: '#{refresh_token}', username: '[email protected]' } imap = GmailCli.imap_connection(credentials) ) access_token end |
#get_authorization_uri ⇒ Object
104 105 106 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 104 def api_client.. end |
#refresh_access_token! ⇒ Object
Command: requests and returns a fresh access_token
41 42 43 44 45 46 47 |
# File 'lib/gmail_cli/oauth2_helper.rb', line 41 def refresh_access_token! api_client..refresh_token = refresh_token response = fetch_refresh_token! # => {"access_token"=>"ya29.AHES6ZRclrR13BePPwPmwdPUtoVqRxJ4fyVKgN1LJzIg-f8", "token_type"=>"Bearer", "expires_in"=>3600} trace "#{__method__} response", response self.access_token = response['access_token'] end |