Class: FreshBooks::CLI::Auth

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

Defined Under Namespace

Classes: TokenRefreshError

Constant Summary collapse

TOKEN_URL =
"https://api.freshbooks.com/auth/oauth/token"
AUTH_URL =
"https://auth.freshbooks.com/oauth/authorize"
ME_URL =
"https://api.freshbooks.com/auth/api/v1/users/me"
REDIRECT_URI =
"https://localhost"
REQUIRED_SCOPES =
%w[
  user:profile:read
  user:clients:read
  user:projects:read
  user:billable_items:read
  user:time_entries:read
  user:time_entries:write
].freeze

Class Method Summary collapse

Class Method Details

.auth_statusObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/freshbooks/auth.rb', line 206

def auth_status
  config = load_config
  tokens = load_tokens
  refresh_error = nil

  if config && tokens && token_expired?(tokens)
    begin
      tokens = refresh_token_with_lock(config, tokens, abort_on_failure: false)
    rescue TokenRefreshError => e
      refresh_error = e.message
    end
  end

  tokens_expired = tokens ? token_expired?(tokens) : nil
  requires_reauth = config.nil? || tokens.nil? || tokens_expired == true
  {
    "config_exists" => !config.nil?,
    "config_path" => config_path,
    "tokens_exist" => !tokens.nil?,
    "tokens_expired" => tokens_expired,
    "requires_reauth" => requires_reauth,
    "refresh_error" => refresh_error,
    "business_id" => config&.dig("business_id"),
    "account_id" => config&.dig("account_id")
  }.compact
end

.authorize(config) ⇒ Object

--- OAuth Flow ---



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/freshbooks/auth.rb', line 388

def authorize(config)
  url = "#{AUTH_URL}?client_id=#{config["client_id"]}&response_type=code&redirect_uri=#{URI.encode_www_form_component(REDIRECT_URI)}"

  puts "Open this URL in your browser:\n\n"
  puts "  #{url}\n\n"
  puts "After authorizing, you'll be redirected to a URL that fails to load."
  puts "Copy the full URL from your browser's address bar and paste it here.\n\n"

  print "Redirect URL: "
  redirect_url = $stdin.gets&.strip
  abort("Aborted.") if redirect_url.nil? || redirect_url.empty?

  uri = URI.parse(redirect_url)
  params = URI.decode_www_form(uri.query || "").to_h
  code = params["code"]

  abort("Could not find 'code' parameter in the URL.") unless code

  exchange_code(config, code)
end

.authorize_url(config) ⇒ Object



196
197
198
# File 'lib/freshbooks/auth.rb', line 196

def authorize_url(config)
  "#{AUTH_URL}?client_id=#{config["client_id"]}&response_type=code&redirect_uri=#{URI.encode_www_form_component(REDIRECT_URI)}"
end

.cache_pathObject



73
74
75
# File 'lib/freshbooks/auth.rb', line 73

def cache_path
  File.join(data_dir, "cache.json")
end

.check_scopes(granted_scope) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/freshbooks/auth.rb', line 441

def check_scopes(granted_scope)
  return if granted_scope.nil? # skip check if API doesn't return scopes

  granted = granted_scope.split(" ")
  missing = REQUIRED_SCOPES - granted

  return if missing.empty?

  puts "ERROR: Your FreshBooks app is missing required scopes:\n\n"
  missing.each { |s| puts "  - #{s}" }
  puts "\nAdd them at https://my.freshbooks.com/#/developer"
  puts "then re-run: fb auth"
  abort
end

.config_pathObject



57
58
59
# File 'lib/freshbooks/auth.rb', line 57

def config_path
  File.join(data_dir, "config.json")
end

.data_dirObject



28
29
30
31
# File 'lib/freshbooks/auth.rb', line 28

def data_dir
  return @data_dir unless @data_dir.nil?
  resolve_data_dir
end

.data_dir=(path) ⇒ Object



33
34
35
# File 'lib/freshbooks/auth.rb', line 33

def data_dir=(path)
  @data_dir = path
end

.defaults_pathObject



69
70
71
# File 'lib/freshbooks/auth.rb', line 69

def defaults_path
  File.join(data_dir, "defaults.json")
end

.discover_business(access_token, config) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/freshbooks/auth.rb', line 470

def discover_business(access_token, config)
  identity = fetch_identity(access_token)
  memberships = identity.dig("business_memberships") || []
  businesses = memberships.select { |m| m.dig("business", "account_id") }

  if businesses.empty?
    abort("No business memberships found on your FreshBooks account.")
  end

  selected = if businesses.length == 1
    businesses.first
  else
    puts "\nMultiple businesses found:\n\n"
    businesses.each_with_index do |m, i|
      biz = m["business"]
      puts "  #{i + 1}. #{biz["name"]} (ID: #{biz["id"]})"
    end
    print "\nSelect a business (1-#{businesses.length}): "
    choice = $stdin.gets&.strip&.to_i || 1
    choice = 1 if choice < 1 || choice > businesses.length
    businesses[choice - 1]
  end

  biz = selected["business"]
  config["business_id"] = biz["id"]
  config["account_id"] = biz["account_id"]
  save_config(config)

  puts "Business: #{biz["name"]}"
  puts "  business_id: #{biz["id"]}"
  puts "  account_id: #{biz["account_id"]}"
  config
end

.ensure_data_dirObject



77
78
79
# File 'lib/freshbooks/auth.rb', line 77

def ensure_data_dir
  FileUtils.mkdir_p(data_dir)
end

.exchange_code(config, code) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/freshbooks/auth.rb', line 409

def exchange_code(config, code)
  response = HTTParty.post(TOKEN_URL, {
    headers: { "Content-Type" => "application/json" },
    body: {
      grant_type: "authorization_code",
      client_id: config["client_id"],
      client_secret: config["client_secret"],
      redirect_uri: REDIRECT_URI,
      code: code
    }.to_json
  })

  unless response.success?
    body = response.parsed_response
    msg = body.is_a?(Hash) ? (body["error_description"] || body["error"] || response.body) : response.body
    abort("Token exchange failed: #{msg}")
  end

  data = response.parsed_response
  check_scopes(data["scope"])

  tokens = {
    "access_token" => data["access_token"],
    "refresh_token" => data["refresh_token"],
    "expires_in" => data["expires_in"],
    "created_at" => Time.now.to_i
  }
  save_tokens(tokens)
  puts "Authentication successful!"
  tokens
end

.extract_code_from_url(redirect_url) ⇒ Object



200
201
202
203
204
# File 'lib/freshbooks/auth.rb', line 200

def extract_code_from_url(redirect_url)
  uri = URI.parse(redirect_url)
  params = URI.decode_www_form(uri.query || "").to_h
  params["code"]
end

.fetch_businesses(access_token) ⇒ Object



233
234
235
236
237
# File 'lib/freshbooks/auth.rb', line 233

def fetch_businesses(access_token)
  identity = fetch_identity(access_token)
  memberships = identity.dig("business_memberships") || []
  memberships.select { |m| m.dig("business", "account_id") }
end

.fetch_identity(access_token) ⇒ Object

--- Business Discovery ---



458
459
460
461
462
463
464
465
466
467
468
# File 'lib/freshbooks/auth.rb', line 458

def fetch_identity(access_token)
  response = HTTParty.get(ME_URL, {
    headers: { "Authorization" => "Bearer #{access_token}" }
  })

  unless response.success?
    abort("Failed to fetch user identity: #{response.body}")
  end

  response.parsed_response["response"]
end

.load_cacheObject

--- Cache ---



520
521
522
523
524
525
# File 'lib/freshbooks/auth.rb', line 520

def load_cache
  return {} unless File.exist?(cache_path)
  JSON.parse(File.read(cache_path))
rescue JSON::ParserError
  {}
end

.load_configObject

--- Config ---



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/freshbooks/auth.rb', line 83

def load_config
  load_dotenv
  client_id = ENV["FRESHBOOKS_CLIENT_ID"]&.strip
  client_secret = ENV["FRESHBOOKS_CLIENT_SECRET"]&.strip
  return nil if client_id.nil? || client_id.empty? || client_secret.nil? || client_secret.empty?
  config = { "client_id" => client_id, "client_secret" => client_secret }
  if File.exist?(config_path)
    begin
      file_config = JSON.parse(File.read(config_path).strip)
      config["business_id"] = file_config["business_id"] if file_config["business_id"]
      config["account_id"] = file_config["account_id"] if file_config["account_id"]
    rescue JSON::ParserError
    end
  end
  config
end

.load_defaultsObject

--- Defaults ---



506
507
508
509
510
511
# File 'lib/freshbooks/auth.rb', line 506

def load_defaults
  return {} unless File.exist?(defaults_path)
  JSON.parse(File.read(defaults_path))
rescue JSON::ParserError
  {}
end

.load_dotenvObject



144
145
146
147
148
149
150
151
# File 'lib/freshbooks/auth.rb', line 144

def load_dotenv
  migrate_credentials_from_config
  dot_env_paths = [
    File.join(data_dir, ".env"),
    File.join(Dir.pwd, ".env")
  ].select { |p| File.exist?(p) }
  Dotenv.load(*dot_env_paths) unless dot_env_paths.empty?
end

.load_tokensObject

--- Tokens ---



274
275
276
277
# File 'lib/freshbooks/auth.rb', line 274

def load_tokens
  return nil unless File.exist?(tokens_path)
  JSON.parse(File.read(tokens_path))
end

.migrate_credentials_from_configObject



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/freshbooks/auth.rb', line 153

def migrate_credentials_from_config
  return unless File.exist?(config_path)
  contents = File.read(config_path).strip
  return if contents.empty?
  config = JSON.parse(contents) rescue {}
  client_id = config["client_id"]
  client_secret = config["client_secret"]
  return unless client_id || client_secret
  write_credentials_to_env(File.join(data_dir, ".env"), client_id.to_s, client_secret.to_s)
  safe_config = config.reject { |k, _| ["client_id", "client_secret"].include?(k) }
  File.write(config_path, JSON.pretty_generate(safe_config) + "\n")
end

.refresh_token!(config, tokens, abort_on_failure: true) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/freshbooks/auth.rb', line 299

def refresh_token!(config, tokens, abort_on_failure: true)
  begin
    response = HTTParty.post(TOKEN_URL, {
      headers: { "Content-Type" => "application/json" },
      body: {
        grant_type: "refresh_token",
        client_id: config["client_id"],
        client_secret: config["client_secret"],
        redirect_uri: REDIRECT_URI,
        refresh_token: tokens["refresh_token"]
      }.to_json
    })
  rescue StandardError => e
    message = "Token refresh failed: #{e.class}: #{e.message}\nPlease re-run: fb auth"
    raise if abort_on_failure
    raise TokenRefreshError, message
  end

  unless response.success?
    body = response.parsed_response
    msg = body.is_a?(Hash) ? (body["error_description"] || body["error"] || response.body) : response.body
    message = "Token refresh failed: #{msg}\nPlease re-run: fb auth"
    abort(message) if abort_on_failure
    raise TokenRefreshError, message
  end

  data = response.parsed_response
  new_tokens = {
    "access_token" => data["access_token"],
    "refresh_token" => data["refresh_token"],
    "expires_in" => data["expires_in"],
    "created_at" => Time.now.to_i
  }
  save_tokens(new_tokens)
  new_tokens
end

.refresh_token_with_lock(config, tokens, abort_on_failure: true) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/freshbooks/auth.rb', line 336

def refresh_token_with_lock(config, tokens, abort_on_failure: true)
  ensure_data_dir
  File.open(tokens_lock_path, File::RDWR | File::CREAT, 0o600) do |lock_file|
    lock_file.flock(File::LOCK_EX)

    latest_tokens = load_tokens || tokens
    return latest_tokens if latest_tokens && !token_expired?(latest_tokens)

    refresh_token!(config, latest_tokens || tokens, abort_on_failure: abort_on_failure)
  ensure
    lock_file.flock(File::LOCK_UN)
  end
end

.require_business(config) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
# File 'lib/freshbooks/auth.rb', line 374

def require_business(config)
  return config if config["business_id"] && config["account_id"]

  tokens = load_tokens
  unless tokens
    puts "Not authenticated yet. Starting auth flow...\n\n"
    tokens = authorize(config)
  end

  discover_business(tokens["access_token"], config)
end

.require_configObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/freshbooks/auth.rb', line 250

def require_config
  if Thread.current[:fb_dry_run]
    config = {}
    if File.exist?(config_path)
      begin
        config = JSON.parse(File.read(config_path).strip)
      rescue JSON::ParserError
        config = {}
      end
    end
    config["business_id"] ||= "0"
    config["account_id"] ||= "0"
    return config
  end

  config = load_config
  return config if config

  puts "No config found. Let's set up FreshBooks CLI.\n\n"
  setup_config
end

.save_cache(cache) ⇒ Object



527
528
529
530
# File 'lib/freshbooks/auth.rb', line 527

def save_cache(cache)
  ensure_data_dir
  File.write(cache_path, JSON.pretty_generate(cache) + "\n")
end

.save_config(config) ⇒ Object



100
101
102
103
104
# File 'lib/freshbooks/auth.rb', line 100

def save_config(config)
  ensure_data_dir
  safe_config = config.reject { |k, _| ["client_id", "client_secret"].include?(k) }
  File.write(config_path, JSON.pretty_generate(safe_config) + "\n")
end

.save_defaults(defaults) ⇒ Object



513
514
515
516
# File 'lib/freshbooks/auth.rb', line 513

def save_defaults(defaults)
  ensure_data_dir
  File.write(defaults_path, JSON.pretty_generate(defaults) + "\n")
end

.save_tokens(tokens) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/freshbooks/auth.rb', line 279

def save_tokens(tokens)
  ensure_data_dir
  tmp_path = "#{tokens_path}.#{$$}.tmp"
  File.open(tmp_path, File::WRONLY | File::CREAT | File::TRUNC, 0o600) do |file|
    file.write(JSON.pretty_generate(tokens) + "\n")
    file.flush
    file.fsync
  end
  File.rename(tmp_path, tokens_path)
ensure
  File.delete(tmp_path) if tmp_path && File.exist?(tmp_path)
end

.select_business(config, business_id, businesses) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/freshbooks/auth.rb', line 239

def select_business(config, business_id, businesses)
  selected = businesses.find { |m| m.dig("business", "id").to_s == business_id.to_s }
  abort("Business not found: #{business_id}. Available: #{businesses.map { |m| "#{m.dig("business", "name")} (#{m.dig("business", "id")})" }.join(", ")}") unless selected

  biz = selected["business"]
  config["business_id"] = biz["id"]
  config["account_id"] = biz["account_id"]
  save_config(config)
  config
end

.setup_configObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/freshbooks/auth.rb', line 106

def setup_config
  puts "Welcome to FreshBooks CLI setup!\n\n"
  puts "You need a FreshBooks Developer App. Create one at:"
  puts "  https://my.freshbooks.com/#/developer\n\n"
  puts "Set the redirect URI to: #{REDIRECT_URI}\n\n"
  puts "Required scopes:"
  puts "  user:profile:read          (enabled by default)"
  puts "  user:clients:read"
  puts "  user:projects:read"
  puts "  user:billable_items:read"
  puts "  user:time_entries:read"
  puts "  user:time_entries:write\n\n"

  print "Client ID: "
  client_id = $stdin.gets&.strip
  abort("Aborted.") if client_id.nil? || client_id.empty?

  print "Client Secret: "
  client_secret = IO.console.getpass("")
  abort("Aborted.") if client_secret.nil? || client_secret.empty?

  env_path = File.join(data_dir, ".env")
  if File.exist?(env_path) && File.read(env_path).match?(/^FRESHBOOKS_CLIENT_ID=/)
    print "\nCredentials already exist in #{env_path}. Overwrite? (y/n): "
    answer = $stdin.gets&.strip&.downcase
    abort("Aborted.") unless answer == "y"
    contents = File.read(env_path)
    contents = contents.gsub(/^FRESHBOOKS_CLIENT_ID=.*$/, "FRESHBOOKS_CLIENT_ID=#{client_id}")
    contents = contents.gsub(/^FRESHBOOKS_CLIENT_SECRET=.*$/, "FRESHBOOKS_CLIENT_SECRET=#{client_secret}")
    File.write(env_path, contents)
  else
    write_credentials_to_env(env_path, client_id, client_secret)
  end

  puts "\nCredentials saved to #{env_path}"
  { "client_id" => client_id, "client_secret" => client_secret }
end

.setup_config_from_argsObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/freshbooks/auth.rb', line 179

def setup_config_from_args
  load_dotenv

  client_id = ENV["FRESHBOOKS_CLIENT_ID"]
  client_secret = ENV["FRESHBOOKS_CLIENT_SECRET"]

  if client_id.nil? || client_id.strip.empty?
    abort("Missing FRESHBOOKS_CLIENT_ID. Set it via:\n  export FRESHBOOKS_CLIENT_ID=your_id\n  or add it to #{data_dir}/.env")
  end

  if client_secret.nil? || client_secret.strip.empty?
    abort("Missing FRESHBOOKS_CLIENT_SECRET. Set it via:\n  export FRESHBOOKS_CLIENT_SECRET=your_secret\n  or add it to #{data_dir}/.env")
  end

  { "client_id" => client_id.strip, "client_secret" => client_secret.strip }
end

.token_expired?(tokens) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
295
296
297
# File 'lib/freshbooks/auth.rb', line 292

def token_expired?(tokens)
  return true unless tokens
  created = tokens["created_at"] || 0
  expires_in = tokens["expires_in"] || 0
  Time.now.to_i >= (created + expires_in - 60)
end

.tokens_lock_pathObject



65
66
67
# File 'lib/freshbooks/auth.rb', line 65

def tokens_lock_path
  File.join(data_dir, "tokens.json.lock")
end

.tokens_pathObject



61
62
63
# File 'lib/freshbooks/auth.rb', line 61

def tokens_path
  File.join(data_dir, "tokens.json")
end

.valid_access_tokenObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/freshbooks/auth.rb', line 350

def valid_access_token
  if Thread.current[:fb_dry_run]
    tokens = load_tokens
    return tokens["access_token"] if tokens && !token_expired?(tokens)
    return "dry-run-token"
  end

  config = require_config
  tokens = load_tokens

  unless tokens
    puts "Not authenticated yet. Starting auth flow...\n\n"
    tokens = authorize(config)
    discover_business(tokens["access_token"], config)
  end

  if token_expired?(tokens)
    puts "Token expired, refreshing..."
    tokens = refresh_token_with_lock(config, tokens)
  end

  tokens["access_token"]
end

.write_credentials_to_env(env_path, client_id, client_secret) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/freshbooks/auth.rb', line 166

def write_credentials_to_env(env_path, client_id, client_secret)
  ensure_data_dir
  if File.exist?(env_path)
    contents = File.read(env_path)
    append = ""
    append += "FRESHBOOKS_CLIENT_ID=#{client_id}\n" unless contents.match?(/^FRESHBOOKS_CLIENT_ID=/)
    append += "FRESHBOOKS_CLIENT_SECRET=#{client_secret}\n" unless contents.match?(/^FRESHBOOKS_CLIENT_SECRET=/)
    File.open(env_path, "a") { |f| f.write(append) } unless append.empty?
  else
    File.write(env_path, "FRESHBOOKS_CLIENT_ID=#{client_id}\nFRESHBOOKS_CLIENT_SECRET=#{client_secret}\n")
  end
end