Class: Everywhere::Platform::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/everywhere/platform/credentials.rb

Overview

Local credential store at ~/.config/everywhere/credentials.json (0600), keyed by Platform base URL so a dev localhost login and a production login coexist. Each entry holds an org-scoped token + the org it's bound to.

{ "https://platform.rubyeverywhere.com": {
  "token": "rbe_…",
  "organization": { "id": "org_…", "name": "Acme", "slug": "acme" } } }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Credentials

Returns a new instance of Credentials.



40
41
42
# File 'lib/everywhere/platform/credentials.rb', line 40

def initialize(data)
  @data = data
end

Class Method Details

.config_homeObject



21
22
23
# File 'lib/everywhere/platform/credentials.rb', line 21

def self.config_home
  ENV["XDG_CONFIG_HOME"].to_s.empty? ? File.expand_path("~/.config") : ENV["XDG_CONFIG_HOME"]
end

.loadObject



25
26
27
28
# File 'lib/everywhere/platform/credentials.rb', line 25

def self.load
  data = File.exist?(path) ? read_or_warn(path) : {}
  new(data.is_a?(Hash) ? data : {})
end

.pathObject



17
18
19
# File 'lib/everywhere/platform/credentials.rb', line 17

def self.path
  ENV["EVERYWHERE_CREDENTIALS"] || File.join(config_home, "everywhere", "credentials.json")
end

.read_or_warn(path) ⇒ Object

A corrupt store used to look identical to "logged out", which sends people re-running login forever. Say which file to delete.



32
33
34
35
36
37
38
# File 'lib/everywhere/platform/credentials.rb', line 32

def self.read_or_warn(path)
  JSON.parse(File.read(path))
rescue JSON::ParserError, SystemCallError, IOError => e
  Everywhere::UI.warn("ignoring unreadable credentials at #{path} (#{e.class}) — " \
                      "delete it and run `every platform login`")
  {}
end

Instance Method Details

#delete(url) ⇒ Object



57
58
59
60
61
# File 'lib/everywhere/platform/credentials.rb', line 57

def delete(url)
  removed = @data.delete(normalize(url))
  save
  removed
end

#for(url) ⇒ Object



44
45
46
# File 'lib/everywhere/platform/credentials.rb', line 44

def for(url)
  @data[normalize(url)]
end

#logged_in?(url) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/everywhere/platform/credentials.rb', line 48

def logged_in?(url)
  !self.for(url).nil?
end

#set(url, entry) ⇒ Object



52
53
54
55
# File 'lib/everywhere/platform/credentials.rb', line 52

def set(url, entry)
  @data[normalize(url)] = entry
  save
end