Sha256: bbd0899144242696524d0380c2c6b4be44ab3755e7216cce6e54a862eefddf10

Contents?: true

Size: 944 Bytes

Versions: 42

Compression:

Stored size: 944 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)
      Dir.mkdir(credentials_path, 0o700) unless Dir.exist?(credentials_path)
      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 = IO.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

42 entries across 42 versions & 1 rubygems

Version Path
sdr-client-0.63.0 lib/sdr_client/credentials.rb
sdr-client-0.62.0 lib/sdr_client/credentials.rb
sdr-client-0.61.1 lib/sdr_client/credentials.rb
sdr-client-0.61.0 lib/sdr_client/credentials.rb
sdr-client-0.60.0 lib/sdr_client/credentials.rb
sdr-client-0.59.0 lib/sdr_client/credentials.rb
sdr-client-0.58.0 lib/sdr_client/credentials.rb
sdr-client-0.57.0 lib/sdr_client/credentials.rb
sdr-client-0.56.0 lib/sdr_client/credentials.rb
sdr-client-0.55.1 lib/sdr_client/credentials.rb
sdr-client-0.55.0 lib/sdr_client/credentials.rb
sdr-client-0.54.0 lib/sdr_client/credentials.rb
sdr-client-0.53.0 lib/sdr_client/credentials.rb
sdr-client-0.52.0 lib/sdr_client/credentials.rb
sdr-client-0.51.0 lib/sdr_client/credentials.rb
sdr-client-0.51.0.beta.1 lib/sdr_client/credentials.rb
sdr-client-0.50.0 lib/sdr_client/credentials.rb
sdr-client-0.49.0 lib/sdr_client/credentials.rb
sdr-client-0.48.0 lib/sdr_client/credentials.rb
sdr-client-0.47.0 lib/sdr_client/credentials.rb