Class: GoogleDriveHandler::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/google_drive_handler/auth.rb

Constant Summary collapse

OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'
CLIENT_SECRETS_PATH =
ENV['CLIENT_SECRETS_PATH'] || File.join(Auth::default_credential_path,'client_secret.json')
INVALID_SCOPE_ERROR =
"Sorry, this is an invalid scope"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuth

Returns a new instance of Auth.



24
25
26
27
# File 'lib/google_drive_handler/auth.rb', line 24

def initialize
  @scope=ENV['SCOPE'].split.map(&:to_i)
  @user_id='default'
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



21
22
23
# File 'lib/google_drive_handler/auth.rb', line 21

def response
  @response
end

#scopeObject (readonly)

Returns the value of attribute scope.



22
23
24
# File 'lib/google_drive_handler/auth.rb', line 22

def scope
  @scope
end

#serviceObject

Returns the value of attribute service.



21
22
23
# File 'lib/google_drive_handler/auth.rb', line 21

def service
  @service
end

#spreadsheetObject

Returns the value of attribute spreadsheet.



21
22
23
# File 'lib/google_drive_handler/auth.rb', line 21

def spreadsheet
  @spreadsheet
end

#user_idObject (readonly)

Returns the value of attribute user_id.



22
23
24
# File 'lib/google_drive_handler/auth.rb', line 22

def user_id
  @user_id
end

Class Method Details

.default_credential_pathObject



11
12
13
# File 'lib/google_drive_handler/auth.rb', line 11

def default_credential_path
  File.join(Dir.home,'.credentials')
end

Instance Method Details

#authorizeObject



29
30
31
32
33
34
# File 'lib/google_drive_handler/auth.rb', line 29

def authorize
  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: credentials_path)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, scope_url, token_store)
  authorizer.get_credentials(user_id)
end

#choose_scopeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/google_drive_handler/auth.rb', line 36

def choose_scope

  puts "Choose one (typing the number) or more scopes (typing the numbers with one space between them):"
  scopes.each_pair do |k,v|
    puts "#{k}.- #{v[:name]}"
    puts "\t#{v[:desc]}"
  end

  s=gets(chomp: true)

  s=s.split
  @scope=s.map do |option|
    raise INVALID_SCOPE_ERROR if !scopes.keys.include?(option.to_i)
    option.to_i
  end

end

#credentials_pathObject



104
105
106
107
108
# File 'lib/google_drive_handler/auth.rb', line 104

def credentials_path
  path=ENV['CREDENTIALS_PATH'] || Auth::default_credential_path
  name=scope_url.map { |a| a.split('/').last }.join('.and.')
  File.join(path, "sheets.googleapis.#{name}.yaml")
end

#save_credentialsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/google_drive_handler/auth.rb', line 54

def save_credentials

  choose_scope

  FileUtils.mkdir_p(File.dirname(credentials_path))

  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: credentials_path)
  authorizer = Google::Auth::UserAuthorizer.new( client_id, scope_url, token_store)
  url = authorizer.get_authorization_url(base_url: OOB_URI)
  puts "Visit #{url} to get the code"

  code=gets(chomp: true)

  authorizer.get_and_store_credentials_from_code(user_id: user_id, code: code, base_url: OOB_URI)

  puts "the credentials have been saved in #{credentials_path}"

end

#scopesObject



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/google_drive_handler/auth.rb', line 74

def scopes
  {
    1 => {
      name: 'AUTH_DRIVE',
      desc: 'View and manage the files in your Google Drive',
      url: 'https://www.googleapis.com/auth/drive'
    },
    2 => {
      name: 'AUTH_DRIVE_FILE',
      desc: 'View and manage Google Drive files and folders that you have opened or created with this app',
      url: 'https://www.googleapis.com/auth/drive.file'
    },
    3 => {
      name: 'AUTH_DRIVE_READONLY',
      desc: 'View the files in your Google Drive',
      url: 'https://www.googleapis.com/auth/drive.readonly'
    },
    4 => {
      name: 'AUTH_SPREADSHEETS',
      desc: 'View and manage your spreadsheets in Google Drive',
      url: 'https://www.googleapis.com/auth/spreadsheets'
    },
    5 => {
      name: 'AUTH_SPREADSHEETS_READONLY',
      desc: 'View your Google Spreadsheets',
      url: 'https://www.googleapis.com/auth/spreadsheets.readonly'
    }
  }
end