Sha256: ce06aaebdd6fa3645fac243b875ef8edd208f6f466069f8548cc80478fc43953

Contents?: true

Size: 924 Bytes

Versions: 36

Compression:

Stored size: 924 Bytes

Contents

# frozen_string_literal: true

module SdrClient
  # The stored credentials
  class Credentials
    class NoCredentialsError < StandardError; end

    # @param [String] a json string that contains a field 'token'
    def self.write(body)
      json = JSON.parse(body)
      FileUtils.mkdir_p(credentials_path, mode: 0o700)
      File.atomic_write(credentials_file) do |file|
        file.write(json.fetch('token'))
      end
      File.chmod(0o600, credentials_file)
    end

    def self.read
      raise NoCredentialsError unless ::File.exist?(credentials_file)

      creds = File.readlines(credentials_file, chomp: true).first if ::File.exist?(credentials_file)
      raise NoCredentialsError if creds.nil?

      creds
    end

    def self.credentials_path
      @credentials_path ||= File.join(Dir.home, '.sdr')
    end

    def self.credentials_file
      File.join(credentials_path, 'credentials')
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
sdr-client-2.18.0 lib/sdr_client/credentials.rb
sdr-client-2.17.0 lib/sdr_client/credentials.rb
sdr-client-2.16.0 lib/sdr_client/credentials.rb
sdr-client-2.13.2 lib/sdr_client/credentials.rb
sdr-client-2.13.1 lib/sdr_client/credentials.rb
sdr-client-2.13.0 lib/sdr_client/credentials.rb
sdr-client-2.13.0.beta2 lib/sdr_client/credentials.rb
sdr-client-2.13.0.beta1 lib/sdr_client/credentials.rb
sdr-client-2.12.0 lib/sdr_client/credentials.rb
sdr-client-2.11.1 lib/sdr_client/credentials.rb
sdr-client-2.11.0 lib/sdr_client/credentials.rb
sdr-client-2.10.1 lib/sdr_client/credentials.rb
sdr-client-2.10.0 lib/sdr_client/credentials.rb
sdr-client-2.9.0 lib/sdr_client/credentials.rb
sdr-client-2.8.0 lib/sdr_client/credentials.rb
sdr-client-2.7.1 lib/sdr_client/credentials.rb
sdr-client-2.7.0 lib/sdr_client/credentials.rb
sdr-client-2.6.0 lib/sdr_client/credentials.rb
sdr-client-2.5.0 lib/sdr_client/credentials.rb
sdr-client-2.4.0 lib/sdr_client/credentials.rb