Class: GmailCli::Oauth2Helper

Inherits:
Object
  • Object
show all
Includes:
LoggerSupport
Defined in:
lib/gmail_cli/oauth2_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoggerSupport

#log, #trace

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(options={})
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @access_token = options[:access_token]
  @refresh_token = options[:refresh_token]
  @scope = options[:scope] || 'https://mail.google.com/'
  @redirect_uri = options[:redirect_uri] || 'urn:ietf:wg:oauth:2.0:oob'
  @application_name = options[:application_name] || 'gmail_cli'
  @application_version = options[: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_tokenObject

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_nameObject

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_versionObject

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_codeObject

Returns the value of attribute authorization_code.



16
17
18
# File 'lib/gmail_cli/oauth2_helper.rb', line 16

def authorization_code
  @authorization_code
end

#client_idObject

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_secretObject

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_uriObject

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_tokenObject

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

#scopeObject

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 authorize!(options={})
  new(options).authorize!
rescue Interrupt
  $stderr.puts "..interrupted."
end

Instance Method Details

#api_clientObject



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.authorization.client_id = client_id
    api_client.authorization.client_secret = client_secret
    api_client.authorization.redirect_uri = redirect_uri
    api_client.authorization.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 authorize!
  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.authorization.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.authorization.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
  authorization_uri = get_authorization_uri
  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.authorization.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_uriObject



104
105
106
# File 'lib/gmail_cli/oauth2_helper.rb', line 104

def get_authorization_uri
  api_client.authorization.authorization_uri
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.authorization.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