Class: Slidict::External::SlidictIo::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/slidict/external/slidict_io/credentials.rb

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("~/.config/slidict/credentials.json")

Instance Method Summary collapse

Constructor Details

#initialize(path: DEFAULT_PATH) ⇒ Credentials

Returns a new instance of Credentials.



12
13
14
# File 'lib/slidict/external/slidict_io/credentials.rb', line 12

def initialize(path: DEFAULT_PATH)
  @path = path
end

Instance Method Details

#read_cli_tokenObject

Returns { access_token:, token_type: } or nil if no CLI access token is saved.



30
31
32
33
34
35
36
37
38
39
# File 'lib/slidict/external/slidict_io/credentials.rb', line 30

def read_cli_token
  return nil unless File.exist?(@path)

  data = JSON.parse(File.read(@path))
  return nil unless data["kind"] == "cli_access_token" && data["access_token"]

  { access_token: data["access_token"], token_type: data.fetch("token_type", "Bearer") }
rescue JSON::ParserError
  nil
end

#write_cli_token!(access_token:, token_type: "Bearer", provider: "github") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slidict/external/slidict_io/credentials.rb', line 16

def write_cli_token!(access_token:, token_type: "Bearer", provider: "github")
  FileUtils.mkdir_p(File.dirname(@path), mode: 0o700)
  json = JSON.pretty_generate(
    "access_token" => access_token,
    "token_type" => token_type,
    "provider" => provider,
    "kind" => "cli_access_token"
  )
  File.write(@path, "#{json}\n", mode: "w", perm: 0o600)
  File.chmod(0o600, @path)
  @path
end