Class: Bastille::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/bastille/store.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



26
27
28
29
# File 'lib/bastille/store.rb', line 26

def authenticate
  response = Client.new(self).authenticate!
  response.success?
end

#ciphersObject



71
72
73
# File 'lib/bastille/store.rb', line 71

def ciphers
  store.fetch(:ciphers) { Hash.new }
end

#ciphers=(ciphers) ⇒ Object



75
76
77
# File 'lib/bastille/store.rb', line 75

def ciphers=(ciphers)
  store[:ciphers] = ciphers
end

#delete!Object



90
91
92
# File 'lib/bastille/store.rb', line 90

def delete!
  pathname.delete
end

#domainObject



47
48
49
# File 'lib/bastille/store.rb', line 47

def domain
  store.fetch(:domain) { raise_key_error :domain }
end

#domain=(domain) ⇒ Object



51
52
53
# File 'lib/bastille/store.rb', line 51

def domain=(domain)
  store[:domain] = domain
end

#each(&block) ⇒ Object



102
103
104
# File 'lib/bastille/store.rb', line 102

def each(&block)
  store.sort { |(a, _), (b, _)| a.to_s <=> b.to_s }.each(&block)
end

#exist?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/bastille/store.rb', line 114

def exist?
  pathname.exist?
end

#freeze!Object



79
80
81
82
83
84
# File 'lib/bastille/store.rb', line 79

def freeze!
  pathname.open('w+') do |f|
    f.write(YAML.dump(@store))
  end
  @store = nil
end

#generate(username, password, domain, name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/bastille/store.rb', line 4

def generate(username, password, domain, name)
  @client = Octokit::Client.new(:login => username, :password => password)
  auth = @client.create_authorization(:scopes => [], :note => name, :note_url => domain)
  self.username = username
  self.token    = auth['token']
  self.domain   = domain
  self.name     = name
  freeze!
  true
rescue Octokit::Unauthorized
  false
end

#generate_key_for(space, vault) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/bastille/store.rb', line 17

def generate_key_for(space, vault)
  new_ciphers = ciphers.dup
  cipher = Gibberish::SHA512("#{username}#{space}#{vault}#{Time.now}")
  new_ciphers["#{space}:#{vault}"] = cipher
  self.ciphers = new_ciphers
  freeze!
  cipher
end

#key(space, vault, type = :autogenerated) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/bastille/store.rb', line 63

def key(space, vault, type = :autogenerated)
  ciphers.fetch("#{space}:#{vault}") do
    if type == :autogenerated
      generate_key_for(space, vault)
    end
  end
end

#keysObject



106
107
108
# File 'lib/bastille/store.rb', line 106

def keys
  store.keys
end

#nameObject



55
56
57
# File 'lib/bastille/store.rb', line 55

def name
  store.fetch(:name) { raise_key_error :name }
end

#name=(name) ⇒ Object



59
60
61
# File 'lib/bastille/store.rb', line 59

def name=(name)
  store[:name] = name
end

#pathObject



118
119
120
# File 'lib/bastille/store.rb', line 118

def path
  ENV['BASTILLE_STORE'] || "#{ENV['HOME']}/.bastille"
end

#pathnameObject



110
111
112
# File 'lib/bastille/store.rb', line 110

def pathname
  @pathname ||= Pathname.new(path)
end

#raise_key_error(key) ⇒ Object

Raises:

  • (KeyError)


94
95
96
# File 'lib/bastille/store.rb', line 94

def raise_key_error(key)
  raise KeyError, "There is no :#{key} key in the ~/.bastille store. Try running `bastille tokenize` to generate a new store with the correct tokens."
end

#storeObject



98
99
100
# File 'lib/bastille/store.rb', line 98

def store
  @store ||= thaw!
end

#thaw!Object



86
87
88
# File 'lib/bastille/store.rb', line 86

def thaw!
  exist? ? pathname.open('r') { |f| YAML.load(f.read) } : {}
end

#tokenObject



39
40
41
# File 'lib/bastille/store.rb', line 39

def token
  store.fetch(:token) { raise_key_error :token }
end

#token=(token) ⇒ Object



43
44
45
# File 'lib/bastille/store.rb', line 43

def token=(token)
  store[:token] = token
end

#usernameObject



31
32
33
# File 'lib/bastille/store.rb', line 31

def username
  store.fetch(:username) { raise_key_error :username }
end

#username=(username) ⇒ Object



35
36
37
# File 'lib/bastille/store.rb', line 35

def username=(username)
  store[:username] = username
end