class ZoomLauncher::GoogleAuth

Constants

OOB_URI

Public Instance Methods

application_credentials_for(scope) click to toggle source

Returns application credentials for the given scope.

# File lib/zoom_launcher/google_auth.rb, line 54
def application_credentials_for(scope)
  Google::Auth.get_application_default(scope)
end
client_secrets_path() click to toggle source

Returns the path to the client_secrets.json file.

# File lib/zoom_launcher/google_auth.rb, line 31
def client_secrets_path
  return ENV['GOOGLE_CLIENT_SECRETS'] if ENV.key?('GOOGLE_CLIENT_SECRETS')
  well_known_path_for('client_secrets.json')
end
token_store_path() click to toggle source

Returns the path to the token store.

# File lib/zoom_launcher/google_auth.rb, line 37
def token_store_path
  return ENV['GOOGLE_CREDENTIAL_STORE'] if ENV.key?('GOOGLE_CREDENTIAL_STORE')
  well_known_path_for('credentials.yaml')
end
user_credentials_for(scope) click to toggle source

Returns user credentials for the given scope. Requests authorization if required.

# File lib/zoom_launcher/google_auth.rb, line 60
def user_credentials_for(scope)
  FileUtils.mkdir_p(File.dirname(token_store_path))

  client_id = if ENV['GOOGLE_CLIENT_ID']
                Google::Auth::ClientId.new(ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'])
              else
                Google::Auth::ClientId.from_file(client_secrets_path)
              end
  token_store = Google::Auth::Stores::FileTokenStore.new(file: token_store_path)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)

  user_id = options[:user] || 'default'

  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI)
    `open "#{url}"`
    code = ask 'Enter the authorization code:'
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end
well_known_path_for(file) click to toggle source

Builds a path to a file in $HOME/.config/google (or %APPDATA%/google, on Windows)

# File lib/zoom_launcher/google_auth.rb, line 44
def well_known_path_for(file)
  if OS.windows?
    dir = ENV.fetch('HOME') { ENV['APPDATA'] }
    File.join(dir, 'google', file)
  else
    File.join(ENV['HOME'], '.config', 'google', file)
  end
end