onetime 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/onetime.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "onetime/version"
4
+ require_relative "onetime/errors"
5
+ require_relative "onetime/configuration"
6
+ require_relative "onetime/response"
7
+ require_relative "onetime/transport"
8
+ require_relative "onetime/client"
9
+
10
+ # Onetime is the official Ruby client for the OnetimeSecret API.
11
+ #
12
+ # It supports the v1 and v2 APIs over a zero-dependency, stdlib-only
13
+ # transport. See Onetime::Client for the primary interface.
14
+ #
15
+ # require "onetime"
16
+ #
17
+ # client = Onetime::Client.new(
18
+ # base_url: "https://ca.onetimesecret.com",
19
+ # customer: "ur1abc23def",
20
+ # api_token: ENV["ONETIME_API_TOKEN"],
21
+ # api_version: :v2,
22
+ # )
23
+ # res = client.secrets.conceal(secret: "hunter2", ttl: 3600)
24
+ # res.dig("record", "secret", "secret_value")
25
+ module Onetime
26
+ # Convenience constructor mirroring Onetime::Client.new.
27
+ #
28
+ # Onetime.client(base_url: "https://ca.onetimesecret.com",
29
+ # customer: "ur1abc23def", api_token: "...")
30
+ def self.client(**options)
31
+ Client.new(**options)
32
+ end
33
+ end
metadata CHANGED
@@ -1,132 +1,86 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onetime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Delano Mandelbaum
9
- autorequire:
10
8
  bindir: bin
11
- cert_chain:
12
- - !binary |-
13
- LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUROakNDQWg2Z0F3SUJB
14
- Z0lCQURBTkJna3Foa2lHOXcwQkFRVUZBREJCTVE4d0RRWURWUVFEREFaa1pX
15
- eGgKYm04eEdUQVhCZ29Ka2lhSmsvSXNaQUVaRmdsemIyeDFkR2x2ZFhNeEV6
16
- QVJCZ29Ka2lhSmsvSXNaQUVaRmdOagpiMjB3SGhjTk1UTXdNakEyTVRFMU56
17
- UTFXaGNOTVRRd01qQTJNVEUxTnpRMVdqQkJNUTh3RFFZRFZRUUREQVprClpX
18
- eGhibTh4R1RBWEJnb0praWFKay9Jc1pBRVpGZ2x6YjJ4MWRHbHZkWE14RXpB
19
- UkJnb0praWFKay9Jc1pBRVoKRmdOamIyMHdnZ0VpTUEwR0NTcUdTSWIzRFFF
20
- QkFRVUFBNElCRHdBd2dnRUtBb0lCQVFEZzFoTXRsMFhzTXVVSwpBS1RnWVd2
21
- M2dqajd2dUVzRTJFalQrdnlCZzgvTHBxVlZ3WnppaWFlYkpUOUlaaVErc0NG
22
- cWJpYWtqMGI1M3BJCmhnMXlPYUJFbUg2L1cwTDdyd3pxYVJWOXNXMWVKczlK
23
- eEZZUUNuZDY3elVuemo4bm5SbE9qRytoaElHK1ZzaWoKbnBzR2J0MjhwZWZ1
24
- TlpKak81cTJjbEFsZlNuaUlJSGZJc1U3L1N0RVl1NkZVR09qbndyeVowcjV5
25
- SmxyOVJyRQpHcytxMERXOFFuWjlVcEFmdURGUVp1SXFlS1FGRkxFN25NbUNH
26
- YUErMEJOMW5MbDNmVkhOYkxIcTdBdms4K1orClp1dXZrZHNjYkhsTy9sKzN4
27
- Q05RNW5Vbkh3cTBBREFiTUxPbG1pWVl6cVhvV0xqbWVJNm1lL2Nsa3RKQ2ZO
28
- MlIKb1pHM1VRdnZBZ01CQUFHak9UQTNNQWtHQTFVZEV3UUNNQUF3SFFZRFZS
29
- ME9CQllFRk1TSk9FdEh6RTRsMGF6dgpNMEpLMGtLTlRvSzFNQXNHQTFVZER3
30
- UUVBd0lFc0RBTkJna3Foa2lHOXcwQkFRVUZBQU9DQVFFQXRPZEU3M3F4Ck9I
31
- MnlkaTlvVDJoUzVmOUcweTFaNzBUbHdoK1ZHRXh5Znh6VkU5WHdDK2lQcEp4
32
- TnJhaUhZZ0YvOS9va3k3WloKUjlxMC90Sm5ldWhBZW5aZGlRa1g3b2k0TzN2
33
- OXdSUzZZSG9XQnhNUEZLVlJMTlR6dlZKc2JtZnBDQWxwNS81ZwpwczR3UUZ5
34
- NW1pYkVsR1ZsT29iZi9naHFaMjVIUzlKNmtkMC9DL3J5MEFVdFRvZ3NMN1R4
35
- R3dUNGtiQ3g2M3ViCjN2eXdFRWhzSlV6ZmQ5N0dDQUJtdFFmUlRsZFgvajdG
36
- MXovNXdkOHAraGZkb3gxaWliZHM5WnRmYVpBM0t6S24Ka2NoV045QjZ6Zzly
37
- MVhNUThCTTJKejBYb1BhblBlMzU0K2xXd2pwa1JLYkZvdy9aYlFIY0NMQ3Ey
38
- NCtONmI2ZwpkZ0tmTkR6d2lEcHFDQT09Ci0tLS0tRU5EIENFUlRJRklDQVRF
39
- LS0tLS0K
40
- date: 2013-02-12 00:00:00.000000000 Z
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
41
11
  dependencies:
42
12
  - !ruby/object:Gem::Dependency
43
- name: drydock
13
+ name: minitest
44
14
  requirement: !ruby/object:Gem::Requirement
45
- none: false
46
15
  requirements:
47
- - - ! '>='
16
+ - - "~>"
48
17
  - !ruby/object:Gem::Version
49
- version: 0.6.9
50
- type: :runtime
18
+ version: '5.0'
19
+ type: :development
51
20
  prerelease: false
52
21
  version_requirements: !ruby/object:Gem::Requirement
53
- none: false
54
22
  requirements:
55
- - - ! '>='
23
+ - - "~>"
56
24
  - !ruby/object:Gem::Version
57
- version: 0.6.9
25
+ version: '5.0'
58
26
  - !ruby/object:Gem::Dependency
59
- name: httparty
27
+ name: rake
60
28
  requirement: !ruby/object:Gem::Requirement
61
- none: false
62
29
  requirements:
63
- - - ! '>='
30
+ - - "~>"
64
31
  - !ruby/object:Gem::Version
65
- version: 0.7.7
66
- type: :runtime
32
+ version: '13.0'
33
+ type: :development
67
34
  prerelease: false
68
35
  version_requirements: !ruby/object:Gem::Requirement
69
- none: false
70
36
  requirements:
71
- - - ! '>='
37
+ - - "~>"
72
38
  - !ruby/object:Gem::Version
73
- version: 0.7.7
74
- - !ruby/object:Gem::Dependency
75
- name: json
76
- requirement: !ruby/object:Gem::Requirement
77
- none: false
78
- requirements:
79
- - - ! '>='
80
- - !ruby/object:Gem::Version
81
- version: 1.6.8
82
- type: :runtime
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- none: false
86
- requirements:
87
- - - ! '>='
88
- - !ruby/object:Gem::Version
89
- version: 1.6.8
90
- description: Command-line tool and library for onetimesecret.com API
91
- email: delano@onetimesecret.com
92
- executables:
93
- - onetime
39
+ version: '13.0'
40
+ description: A dependency-free Ruby client for the OnetimeSecret API, supporting the
41
+ v1 and v2 API versions.
42
+ email:
43
+ - gems@onetimesecret.com
44
+ executables: []
94
45
  extensions: []
95
- extra_rdoc_files:
96
- - LICENSE.txt
97
- - README.md
46
+ extra_rdoc_files: []
98
47
  files:
99
48
  - CHANGES.txt
100
49
  - LICENSE.txt
101
50
  - README.md
102
- - Rakefile
103
- - VERSION
104
- - bin/onetime
105
- - gem-public_cert.pem
106
- - lib/onetime/api.rb
107
- - onetime.gemspec
51
+ - lib/onetime.rb
52
+ - lib/onetime/client.rb
53
+ - lib/onetime/configuration.rb
54
+ - lib/onetime/errors.rb
55
+ - lib/onetime/resources/receipts.rb
56
+ - lib/onetime/resources/secrets.rb
57
+ - lib/onetime/response.rb
58
+ - lib/onetime/transport.rb
59
+ - lib/onetime/version.rb
108
60
  homepage: https://github.com/onetimesecret/onetime-ruby
109
- licenses: []
110
- post_install_message:
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ source_code_uri: https://github.com/onetimesecret/onetime-ruby
65
+ bug_tracker_uri: https://github.com/onetimesecret/onetime-ruby/issues
66
+ documentation_uri: https://docs.onetimesecret.com/
67
+ changelog_uri: https://github.com/onetimesecret/onetime-ruby/blob/main/CHANGES.txt
68
+ rubygems_mfa_required: 'true'
111
69
  rdoc_options: []
112
70
  require_paths:
113
71
  - lib
114
72
  required_ruby_version: !ruby/object:Gem::Requirement
115
- none: false
116
73
  requirements:
117
- - - ! '>='
74
+ - - ">="
118
75
  - !ruby/object:Gem::Version
119
- version: '0'
76
+ version: '3.1'
120
77
  required_rubygems_version: !ruby/object:Gem::Requirement
121
- none: false
122
78
  requirements:
123
- - - ! '>='
79
+ - - ">="
124
80
  - !ruby/object:Gem::Version
125
81
  version: '0'
126
82
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 1.8.25
129
- signing_key:
130
- specification_version: 3
131
- summary: Command-line tool and library for onetimesecret.com API
83
+ rubygems_version: 3.6.9
84
+ specification_version: 4
85
+ summary: Official Ruby client for the OnetimeSecret API
132
86
  test_files: []
data/Rakefile DELETED
@@ -1,50 +0,0 @@
1
- require "rubygems"
2
- require "rake"
3
- require "rake/clean"
4
- require "rdoc/task"
5
-
6
- task :default => ["build"]
7
- CLEAN.include [ 'pkg', 'rdoc' ]
8
- name = "onetime/api"
9
-
10
- $:.unshift File.join(File.dirname(__FILE__), 'lib')
11
- require name
12
- version = Onetime::API::VERSION.to_s
13
-
14
- begin
15
- require "jeweler"
16
- Jeweler::Tasks.new do |s|
17
- s.version = version
18
- s.name = "onetime"
19
- s.executables = ['onetime']
20
- s.summary = "Command-line tool and library for onetimesecret.com API"
21
- s.description = "Command-line tool and library for onetimesecret.com API"
22
- s.email = "delano@onetimesecret.com"
23
- s.homepage = "https://github.com/onetimesecret/onetime-ruby"
24
- s.authors = ["Delano Mandelbaum"]
25
- s.add_dependency('drydock', '>= 0.6.9')
26
- s.add_dependency('httparty', '>= 0.7.7')
27
- s.add_dependency('json', '>= 1.6.8')
28
-
29
- s.signing_key = File.join('/mnt/gem/', 'gem-private_key.pem')
30
- s.cert_chain = ['gem-public_cert.pem']
31
- end
32
- Jeweler::GemcutterTasks.new
33
- rescue LoadError
34
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
35
- end
36
-
37
- RDoc::Task.new do |rdoc|
38
- rdoc.rdoc_dir = "rdoc"
39
- rdoc.title = "#{name} #{version}"
40
- rdoc.generator = 'hanna'
41
- rdoc.main = 'README.rdoc'
42
- rdoc.rdoc_files.include("README*")
43
- rdoc.rdoc_files.include("LICENSE.txt")
44
- rdoc.rdoc_files.include("VERSION")
45
- rdoc.rdoc_files.include("bin/*.rb")
46
- rdoc.rdoc_files.include("lib/**/*.rb")
47
- end
48
-
49
-
50
-
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.5.0
data/bin/onetime DELETED
@@ -1,224 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- base_path = File.expand_path File.join(File.dirname(__FILE__), '..')
4
- $:.unshift File.join(base_path, 'lib')
5
-
6
- require 'uri'
7
- require 'onetime/api'
8
- require 'drydock'
9
-
10
- class Onetime::CLI
11
- class Definition
12
- extend Drydock
13
-
14
- default :share
15
-
16
- global :H, String, "Base URI (e.g. https://onetimesecret.com/api)"
17
- global :c, :custid, String, "Customer ID (e.g. you@yourcompany.com)"
18
- global :k, :apikey, String, "API key (e.g. 4eb33c6340006d6607c813fc7e707a32f8bf5342)"
19
-
20
- global :f, :format, String, "Output format (json or yaml)"
21
- global :j, :json, "Shorthand for -f json"
22
- global :y, :yaml, "Shorthand for -f yaml"
23
- global :s, :string, "Shorthand for -f string (default)"
24
-
25
- global :D, :debug do
26
- OT::API.debug_output STDERR
27
- end
28
-
29
- global :V, :version do
30
- puts OT::API::VERSION
31
- exit 0
32
- end
33
-
34
- before do |obj|
35
- OT::API.base_uri obj.global.H if obj.global.H
36
- @api = OT::API.new obj.global.custid, obj.global.apikey
37
- obj.global.format = 'yaml' if obj.global.yaml
38
- obj.global.format = 'json' if obj.global.json
39
- obj.global.format = 'string' if obj.global.string
40
- obj.global.format = nil if obj.global.format == 'string'
41
- if obj.global.format && !['json', 'yaml', 'csv'].member?(obj.global.format)
42
- raise RuntimeError, "Unsupported format: #{obj.global.format}"
43
- end
44
- end
45
-
46
- usage "onetime status"
47
- command :status do |obj|
48
- @res = @api.get '/status'
49
- if @res.nil?
50
- raise RuntimeError, 'Could not complete request'
51
- elsif @api.response.code != 200
52
- raise RuntimeError, @res['message']
53
- end
54
- case obj.global.format
55
- when 'json'
56
- puts @res.to_json
57
- when 'yaml'
58
- puts @res.to_yaml
59
- else
60
- msg = @api.anonymous ? 'Anonymous' : @api.custid
61
- STDERR.puts '# Host: %s' % OT::API.base_uri
62
- STDERR.puts '# Account: %s' % msg
63
- puts 'Service Status: %s' % @res[:status]
64
- end
65
- end
66
-
67
- argv :key
68
- usage "onetime metadata <KEY>"
69
- command :metadata do |obj|
70
- raise RuntimeError, "csv not supported" if obj.global.format == 'csv'
71
- raise RuntimeError, "Usage: #{$0} metadata <KEY>" unless obj.argv.key
72
- @res = @api.post '/metadata/%s' % obj.argv.key
73
- if @res.nil?
74
- raise RuntimeError, 'Could not complete request'
75
- elsif @api.response.code != 200
76
- raise RuntimeError, @res['message']
77
- end
78
- case obj.global.format
79
- when 'json'
80
- puts @res.to_json
81
- when 'yaml'
82
- puts @res.to_yaml
83
- else
84
- puts @res.to_yaml
85
- end
86
- end
87
-
88
- option :p, :passphrase, String, "Passphrase to decrypt the secret (only required if one was provided to you)"
89
- argv :key
90
- usage "onetime secret <KEY>"
91
- usage "onetime secret [-p PASSPHRASE] <KEY>"
92
- command :secret do |obj|
93
- raise RuntimeError, "csv not supported" if obj.global.format == 'csv'
94
- raise RuntimeError, "Usage: #{$0} secret <KEY>" unless obj.argv.key
95
- opts = {}
96
- opts[:passphrase] = obj.option.passphrase if obj.option.passphrase
97
- base_uri = URI.parse Onetime::API.base_uri
98
- if obj.argv.key =~ /#{base_uri.hostname}\/secret\/([a-zA-Z0-9]+)/
99
- obj.argv.key = $1
100
- end
101
- @res = @api.post '/secret/%s' % [obj.argv.key], opts
102
- if @res.nil?
103
- raise RuntimeError, 'Could not complete request'
104
- elsif @api.response.code != 200
105
- raise RuntimeError, @res['message']
106
- end
107
- case obj.global.format
108
- when 'json'
109
- puts @res.to_json
110
- when 'yaml'
111
- puts @res.to_yaml
112
- else
113
- puts @res[:value] if @res
114
- end
115
- end
116
- command_alias :secret, :get
117
-
118
- usage "onetime share"
119
- usage "onetime share [-t 3600] [-p PASSPHRASE]"
120
- usage "echo 'your secret' | onetime share"
121
- usage "<path/2/file onetime share"
122
- option :t, :ttl, Integer, "Time-to-live in seconds"
123
- option :p, :passphrase, String, "Passphrase to encrypt the secret (something only you and recipient know)"
124
- option :r, :recipient, Array, "Email address to deliver the secret link"
125
- command :share do |obj|
126
- begin
127
- if Kernel.select [$stdin], nil, nil, 0
128
- secret_value = STDIN.read
129
- else
130
- STDERR.puts "Paste message here (hit control-D to continue):"
131
- secret_value = ARGF.read
132
- STDERR.puts # new line to let the person know we got it.
133
- end
134
- rescue Interrupt => ex
135
- puts "Exiting..."
136
- exit 0
137
- end
138
- raise RuntimeError, "No secret provided" if secret_value.chomp.empty?
139
- opts = { :secret => secret_value, :ttl => obj.option.ttl, :recipient => obj.option.recipient }
140
- opts[:passphrase] = obj.option.passphrase if obj.option.passphrase
141
- @res = @api.post '/share', opts
142
- if @res.nil?
143
- raise RuntimeError, 'Could not complete request'
144
- elsif @api.response.code != 200
145
- raise RuntimeError, @res['message']
146
- end
147
- uri = OT::API.web_uri('secret', @res[:secret_key])
148
- case obj.global.format
149
- when 'json'
150
- puts @res.to_json
151
- when 'yaml'
152
- puts @res.to_yaml
153
- else
154
- if @res[:recipient] && !@res[:recipient].compact.empty?
155
- STDERR.puts '# Secret link sent to: %s' % @res[:recipient].join(',')
156
- else
157
- puts uri
158
- end
159
- end
160
- end
161
-
162
- usage "onetime generate"
163
- usage "onetime generate [-t 3600] [-p PASSPHRASE]"
164
- option :t, :ttl, Integer, "Time-to-live in seconds"
165
- option :p, :passphrase, String, "Passphrase to encrypt the secret (something only you and recipient know)"
166
- option :r, :recipient, Array, "Email address to deliver the secret link"
167
- command :generate do |obj|
168
- opts = { :ttl => obj.option.ttl, :recipient => obj.option.recipient }
169
- opts[:passphrase] = obj.option.passphrase if obj.option.passphrase
170
- @res = @api.post '/generate', opts
171
- if @res.nil?
172
- raise RuntimeError, 'Could not complete request'
173
- elsif @api.response.code != 200
174
- raise RuntimeError, @res['message']
175
- end
176
- uri, secret_value = OT::API.web_uri('secret', @res[:secret_key]), @res[:value]
177
- case obj.global.format
178
- when 'json'
179
- puts @res.to_json
180
- when 'yaml'
181
- puts @res.to_yaml
182
- when 'csv'
183
- puts [secret_value,uri].join ','
184
- else
185
- msg = 'Your secret (hit return to continue): %s' % secret_value
186
- if @res[:recipient] && !@res[:recipient].compact.empty?
187
- uri = "\e[K\r# Secret link sent to: %s" % [@res[:recipient].join(',').ljust(40, ' ')]
188
- end
189
- eraser = "\e[K\r%s" % [uri] # \e[K\r
190
- print msg
191
- # Check for tty b/c if we're receiving data via STDIN, even if
192
- # we're ignoring it, the stty commands return "stdin isn't a terminal"
193
- if STDIN.tty?
194
- begin
195
- old_stty = `stty -g` # store tty settings
196
- system "stty -echo"
197
- STDIN.getc
198
- rescue Interrupt
199
- ensure
200
- system "stty echo"
201
- system "stty #{old_stty}" # restore stty settings
202
- print eraser
203
- end
204
- else
205
- puts
206
- print uri
207
- end
208
- puts
209
- end
210
- end
211
-
212
- end
213
- end
214
-
215
- begin
216
- Drydock.run! ARGV, STDIN
217
- rescue RuntimeError => ex
218
- STDERR.puts ex.message
219
- exit 1
220
- rescue => ex
221
- puts ex.message
222
- puts ex.backtrace
223
- exit 1
224
- end
data/gem-public_cert.pem DELETED
@@ -1,20 +0,0 @@
1
- -----BEGIN CERTIFICATE-----
2
- MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMQ8wDQYDVQQDDAZkZWxh
3
- bm8xGTAXBgoJkiaJk/IsZAEZFglzb2x1dGlvdXMxEzARBgoJkiaJk/IsZAEZFgNj
4
- b20wHhcNMTMwMjA2MTE1NzQ1WhcNMTQwMjA2MTE1NzQ1WjBBMQ8wDQYDVQQDDAZk
5
- ZWxhbm8xGTAXBgoJkiaJk/IsZAEZFglzb2x1dGlvdXMxEzARBgoJkiaJk/IsZAEZ
6
- FgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDg1hMtl0XsMuUK
7
- AKTgYWv3gjj7vuEsE2EjT+vyBg8/LpqVVwZziiaebJT9IZiQ+sCFqbiakj0b53pI
8
- hg1yOaBEmH6/W0L7rwzqaRV9sW1eJs9JxFYQCnd67zUnzj8nnRlOjG+hhIG+Vsij
9
- npsGbt28pefuNZJjO5q2clAlfSniIIHfIsU7/StEYu6FUGOjnwryZ0r5yJlr9RrE
10
- Gs+q0DW8QnZ9UpAfuDFQZuIqeKQFFLE7nMmCGaA+0BN1nLl3fVHNbLHq7Avk8+Z+
11
- ZuuvkdscbHlO/l+3xCNQ5nUnHwq0ADAbMLOlmiYYzqXoWLjmeI6me/clktJCfN2R
12
- oZG3UQvvAgMBAAGjOTA3MAkGA1UdEwQCMAAwHQYDVR0OBBYEFMSJOEtHzE4l0azv
13
- M0JK0kKNToK1MAsGA1UdDwQEAwIEsDANBgkqhkiG9w0BAQUFAAOCAQEAtOdE73qx
14
- OH2ydi9oT2hS5f9G0y1Z70Tlwh+VGExyfxzVE9XwC+iPpJxNraiHYgF/9/oky7ZZ
15
- R9q0/tJneuhAenZdiQkX7oi4O3v9wRS6YHoWBxMPFKVRLNTzvVJsbmfpCAlp5/5g
16
- ps4wQFy5mibElGVlOobf/ghqZ25HS9J6kd0/C/ry0AUtTogsL7TxGwT4kbCx63ub
17
- 3vywEEhsJUzfd97GCABmtQfRTldX/j7F1z/5wd8p+hfdox1iibds9ZtfaZA3KzKn
18
- kchWN9B6zg9r1XMQ8BM2Jz0XoPanPe354+lWwjpkRKbFow/ZbQHcCLCq24+N6b6g
19
- dgKfNDzwiDpqCA==
20
- -----END CERTIFICATE-----
data/lib/onetime/api.rb DELETED
@@ -1,136 +0,0 @@
1
-
2
-
3
- require 'httparty'
4
- require 'uri'
5
-
6
- begin
7
- require 'yajl-ruby'
8
- rescue LoadError => ex
9
- require 'json'
10
- end
11
-
12
- begin
13
- require 'yaml'
14
- rescue LoadError => ex
15
- end
16
-
17
-
18
- # Onetime::API - v0.3
19
- #
20
- # A basic client library for the onetimesecret.com API.
21
- #
22
- # Usage:
23
- #
24
- # api = OT::API.new 'delano@onetimesecret.com', '4eb33c6340006d6607c813fc7e707a32f8bf5342'
25
- #
26
- # api.get '/status'
27
- # # => {'status' => 'nominal'}
28
- #
29
- # api.post '/generate', :passphrase => 'yourspecialpassphrase'
30
- # # => {'value' => '3Rg8R2sfD3?a', 'metadata_key' => '...', 'secret_key' => '...'}
31
- #
32
- module Onetime
33
- class API
34
- unless defined?(Onetime::API::HOME)
35
- HOME = File.expand_path File.join(File.dirname(__FILE__), '..', '..')
36
- end
37
- module VERSION
38
- @path = File.join(Onetime::API::HOME, 'VERSION')
39
- class << self
40
- attr_reader :version, :path
41
- def version
42
- @version || read_version
43
- end
44
- def read_version
45
- return if @version
46
- @version = File.read(path).strip!
47
- end
48
- def prerelease?() false end
49
- def to_a() version.split('.') end
50
- def to_s() version end
51
- def inspect() version end
52
- end
53
- end
54
- end
55
- class API
56
- include HTTParty
57
- base_uri 'https://onetimesecret.com/api'
58
- format :json
59
- headers 'X-Onetime-Client' => 'ruby: %s/%s' % [RUBY_VERSION, Onetime::API::VERSION.to_s]
60
- attr_reader :opts, :response, :custid, :key, :default_params, :anonymous
61
- attr_accessor :apiversion
62
- def initialize custid=nil, key=nil, opts={}
63
- unless ENV['ONETIME_HOST'].to_s.empty?
64
- self.class.base_uri ENV['ONETIME_HOST']
65
- end
66
- @apiversion = opts.delete(:apiversion) || opts.delete('apiversion') || 1
67
- @opts = opts
68
- @default_params = {}
69
- @custid = custid || ENV['ONETIME_CUSTID']
70
- @key = key || ENV['ONETIME_APIKEY']
71
- if @custid.to_s.empty? && @key.to_s.empty?
72
- @anonymous = true
73
- elsif @custid.to_s.empty? || @key.to_s.empty?
74
- raise RuntimeError, "You provided a custid without an apikey" if @key.to_s.empty?
75
- raise RuntimeError, "You provided an apikey without a custid" if @custid.to_s.empty?
76
- else
77
- @anonymous = false
78
- opts[:basic_auth] ||= { :username => @custid, :password => @key }
79
- end
80
- end
81
- def get path, params=nil
82
- opts = self.opts.clone
83
- opts[:query] = (params || {}).merge default_params
84
- execute_request :get, path, opts
85
- end
86
- def post path, params=nil
87
- opts = self.opts.clone
88
- opts[:body] = (params || {}).merge default_params
89
- execute_request :post, path, opts
90
- end
91
- def api_path *args
92
- args.unshift ['', "v#{apiversion}"] # force leading slash and version
93
- path = args.flatten.join('/')
94
- path.gsub '//', '/'
95
- end
96
- private
97
- def execute_request meth, path, opts
98
- path = api_path [path]
99
- @response = self.class.send meth, path, opts
100
- self.class.indifferent_params @response.parsed_response
101
- end
102
- class << self
103
- def web_uri *args
104
- uri = URI.parse(OT::API.base_uri)
105
- uri.path = web_path *args
106
- uri
107
- end
108
- def web_path *args
109
- args.unshift [''] # force leading slash
110
- path = args.flatten.join('/')
111
- path.gsub '//', '/'
112
- end
113
- def indifferent_params(params)
114
- if params.is_a?(Hash)
115
- params = indifferent_hash.merge(params)
116
- params.each do |key, value|
117
- next unless value.is_a?(Hash) || value.is_a?(Array)
118
- params[key] = indifferent_params(value)
119
- end
120
- elsif params.is_a?(Array)
121
- params.collect! do |value|
122
- if value.is_a?(Hash) || value.is_a?(Array)
123
- indifferent_params(value)
124
- else
125
- value
126
- end
127
- end
128
- end
129
- end
130
- def indifferent_hash
131
- Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
132
- end
133
- end
134
- end
135
- end
136
- OT = Onetime unless defined?(OT)