Class: Rake::GemMaintenance::OtpProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/gem/maintenance/otp_provider.rb

Overview

Resolves a 2FA OTP code for gem push, either from the environment or interactively. Resolution order for otp_for:

1. RUBYGEMS_OTP env var set → use raw code (works in CI and locally)
2. otp_seed_env_var provided and env var set → generate TOTP code (works in CI and locally)
3. CI environment → nil (gate only interactive prompt)
4. Interactive prompt

Instance Method Summary collapse

Constructor Details

#initialize(ci_environment: CIEnvironment, input: $stdin) ⇒ OtpProvider

Returns a new instance of OtpProvider.



12
13
14
15
# File 'lib/rake/gem/maintenance/otp_provider.rb', line 12

def initialize(ci_environment: CIEnvironment, input: $stdin)
  @ci_environment = ci_environment
  @input = input
end

Instance Method Details

#otp_for(repository_name, otp_seed_env_var: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rake/gem/maintenance/otp_provider.rb', line 17

def otp_for(repository_name, otp_seed_env_var: nil)
  env_otp = ENV.fetch("RUBYGEMS_OTP", nil)
  return env_otp if env_otp && !env_otp.empty?

  if otp_seed_env_var
    seed = ENV.fetch(otp_seed_env_var, nil)
    return generate_totp(seed) if seed && !seed.empty?
  end

  return nil if @ci_environment.ci?

  prompt_for_otp(repository_name)
end