onetime 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.txt ADDED
@@ -0,0 +1,7 @@
1
+ ONETIME, CHANGES
2
+
3
+ #### 0.3.0 (2012-01-06) ###############################
4
+
5
+ Initial public release
6
+
7
+
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011,2012 Delano Mandelbaum
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "rake/clean"
4
+ require 'yaml'
5
+
6
+ require 'rake/rdoctask'
7
+
8
+ config = YAML.load_file("VERSION.yml")
9
+ task :default => ["build"]
10
+ CLEAN.include [ 'pkg', 'doc' ]
11
+ name = "onetime"
12
+
13
+ begin
14
+ require "jeweler"
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.version = "#{config[:MAJOR]}.#{config[:MINOR]}.#{config[:PATCH]}"
17
+ gem.name = name
18
+ gem.executables = ['onetime']
19
+ gem.rubyforge_project = gem.name
20
+ gem.summary = "CLI tool and Ruby library for onetimesecret.com"
21
+ gem.description = "CLI tool and Ruby library for onetimesecret.com"
22
+ gem.email = "delano@onetimesecret.com"
23
+ gem.homepage = "https://github.com/onetimesecret/onetime-ruby"
24
+ gem.authors = ["Delano Mandelbaum"]
25
+ gem.add_dependency('drydock', '>= 0.6.9')
26
+ gem.add_dependency('json', '>= 1.6.4')
27
+ end
28
+ Jeweler::GemcutterTasks.new
29
+ rescue LoadError
30
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
31
+ end
32
+
33
+
34
+ Rake::RDocTask.new do |rdoc|
35
+ version = "#{config[:MAJOR]}.#{config[:MINOR]}.#{config[:PATCH]}"
36
+ rdoc.rdoc_dir = "doc"
37
+ rdoc.title = "#{name} #{version}"
38
+ rdoc.rdoc_files.include("README*")
39
+ rdoc.rdoc_files.include("LICENSE.txt")
40
+ rdoc.rdoc_files.include("bin/*.rb")
41
+ rdoc.rdoc_files.include("lib/**/*.rb")
42
+ end
43
+
44
+
data/VERSION.yml ADDED
@@ -0,0 +1,3 @@
1
+ :MAJOR: 0
2
+ :MINOR: 3
3
+ :PATCH: 0
data/bin/onetime ADDED
@@ -0,0 +1,138 @@
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 'onetime/api'
7
+ require 'drydock'
8
+
9
+ class Onetime::CLI
10
+ class Definition
11
+ extend Drydock
12
+
13
+ default :share
14
+
15
+ global :c, :custid, String, "Customer ID (e.g. you@yourcompany.com)"
16
+ global :k, :apikey, String, "API key (e.g. 4eb33c6340006d6607c813fc7e707a32f8bf5342)"
17
+ global :u, :uri, String, "Base URI (e.g. https://onetimesecret.com/api)"
18
+
19
+ global :f, :format, String, "Output format (json or yaml)"
20
+ global :j, :json, "Shorthand for -f json"
21
+ global :y, :yaml, "Shorthand for -f yaml"
22
+
23
+ global :D, :debug do
24
+ OT::API.debug_output STDERR
25
+ end
26
+
27
+ global :V, :version do
28
+ puts OT::API::VERSION
29
+ exit 0
30
+ end
31
+
32
+ before do |obj|
33
+ @api = OT::API.new obj.global.custid, obj.global.apikey
34
+ obj.global.format = 'yaml' if obj.global.yaml
35
+ obj.global.format = 'json' if obj.global.json
36
+ obj.global.format = nil if obj.global.format == 'string'
37
+ if obj.global.format && !['json', 'yaml', 'csv'].member?(obj.global.format)
38
+ raise RuntimeError, "Unsupported format: #{obj.global.format}"
39
+ end
40
+ if !STDOUT.tty? && !obj.global.format
41
+ raise RuntimeError, "No tty. Must specify an output format. See #{$0} generate -h"
42
+ end
43
+ end
44
+
45
+ after do |obj|
46
+ case obj.global.format
47
+ when 'json'
48
+ puts @ret.to_json
49
+ when 'yaml'
50
+ puts @ret.to_yaml
51
+ end
52
+ end
53
+
54
+ command :status do |obj|
55
+ @ret = @api.get '/status'
56
+ unless obj.global.format
57
+ puts @ret[:status]
58
+ end
59
+ end
60
+
61
+ argv :key
62
+ command :metadata do |obj|
63
+ raise RuntimeError, "csv not supported" if obj.global.format == 'csv'
64
+ raise RuntimeError, "Usage: #{$0} metadata <KEY>" unless obj.argv.key
65
+ @ret = @api.post '/metadata/%s' % obj.argv.key
66
+ unless obj.global.format
67
+ puts @ret.to_yaml
68
+ end
69
+ end
70
+
71
+ argv :key
72
+ command :secret do |obj|
73
+ raise RuntimeError, "csv not supported" if obj.global.format == 'csv'
74
+ raise RuntimeError, "Usage: #{$0} secret <KEY>" unless obj.argv.key
75
+ @ret = @api.post '/secret/%s' % obj.argv.key
76
+ unless obj.global.format
77
+ puts @ret.to_yaml
78
+ end
79
+ end
80
+
81
+ command :share do |obj|
82
+ begin
83
+ if Kernel.select [$stdin], nil, nil, 0
84
+ secret_value = STDIN.read
85
+ else
86
+ STDERR.puts "Paste secret here (hit control-D to continue):"
87
+ secret_value = ARGF.read
88
+ STDERR.puts "Generating link..."
89
+ end
90
+ rescue Interrupt => ex
91
+ puts "Exiting..."
92
+ exit 0
93
+ end
94
+ raise RuntimeError, "No secret provided" if secret_value.chomp.empty?
95
+ @ret = @api.post '/share', :secret => secret_value
96
+ uri = OT::API.web_uri('secret', @ret[:secret_key])
97
+ if obj.global.format == 'csv' || obj.global.format.nil?
98
+ puts uri
99
+ end
100
+ end
101
+
102
+ command :generate do |obj|
103
+ @ret = @api.post '/generate'
104
+ uri, secret_value = OT::API.web_uri('secret', @ret[:secret_key]), @ret[:value]
105
+ STDERR.puts ret.inspect if obj.global.debug
106
+ if obj.global.format == 'csv'
107
+ puts [secret_value,uri].join ','
108
+ elsif obj.global.format.nil?
109
+ msg = "Your secret (hit return to continue): %s " % secret_value
110
+ eraser = "\e[K\r%s" % [uri] # \e[K\r
111
+ print msg
112
+ begin
113
+ old_stty = `stty -g` # store tty settings
114
+ system "stty -echo"
115
+ STDIN.getc
116
+ rescue Interrupt
117
+ ensure
118
+ system "stty echo"
119
+ system "stty #{old_stty}" # restore stty settings
120
+ print eraser
121
+ end
122
+ puts
123
+ end
124
+ end
125
+
126
+ end
127
+ end
128
+
129
+ begin
130
+ Drydock.run! ARGV, STDIN
131
+ rescue RuntimeError => ex
132
+ STDERR.puts ex.message
133
+ exit 1
134
+ rescue => ex
135
+ puts ex.message
136
+ puts ex.backtrace
137
+ exit 1
138
+ end
@@ -0,0 +1,123 @@
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
+ include HTTParty
35
+ LIB_HOME = File.expand_path File.dirname(__FILE__) unless defined?(Onetime::API::LIB_HOME)
36
+ base_uri 'https://onetimesecret.com/api'
37
+ format :json
38
+ attr_reader :opts, :response, :custid, :key, :default_params
39
+ attr_accessor :apiversion
40
+ def initialize custid=nil, key=nil, opts={}
41
+ unless ENV['ONETIME_HOST'].to_s.empty?
42
+ self.class.base_uri ENV['ONETIME_HOST']
43
+ end
44
+ @apiversion = opts.delete(:apiversion) || opts.delete('apiversion') || 1
45
+ @opts = opts
46
+ @default_params = {}
47
+ @custid = custid || ENV['ONETIME_ACCOUNT']
48
+ @key = key || ENV['ONETIME_APIKEY']
49
+ unless @custid.to_s.empty? || @key.to_s.empty?
50
+ opts[:basic_auth] ||= { :username => @custid, :password => @key }
51
+ end
52
+ end
53
+ def get path, params=nil
54
+ opts = self.opts.clone
55
+ opts[:query] = (params || {}).merge default_params
56
+ execute_request :get, path, opts
57
+ end
58
+ def post path, params=nil
59
+ opts = self.opts.clone
60
+ opts[:body] = (params || {}).merge default_params
61
+ execute_request :post, path, opts
62
+ end
63
+ def api_path *args
64
+ args.unshift ['', "v#{apiversion}"] # force leading slash and version
65
+ path = args.flatten.join('/')
66
+ path.gsub '//', '/'
67
+ end
68
+ private
69
+ def execute_request meth, path, opts
70
+ path = api_path [path]
71
+ @response = self.class.send meth, path, opts
72
+ self.class.indifferent_params @response.parsed_response
73
+ end
74
+ class << self
75
+ def web_uri *args
76
+ uri = URI.parse(OT::API.base_uri)
77
+ uri.path = web_path *args
78
+ uri
79
+ end
80
+ def web_path *args
81
+ args.unshift [''] # force leading slash
82
+ path = args.flatten.join('/')
83
+ path.gsub '//', '/'
84
+ end
85
+ def indifferent_params(params)
86
+ if params.is_a?(Hash)
87
+ params = indifferent_hash.merge(params)
88
+ params.each do |key, value|
89
+ next unless value.is_a?(Hash) || value.is_a?(Array)
90
+ params[key] = indifferent_params(value)
91
+ end
92
+ elsif params.is_a?(Array)
93
+ params.collect! do |value|
94
+ if value.is_a?(Hash) || value.is_a?(Array)
95
+ indifferent_params(value)
96
+ else
97
+ value
98
+ end
99
+ end
100
+ end
101
+ end
102
+ def indifferent_hash
103
+ Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
104
+ end
105
+ end
106
+
107
+ module VERSION
108
+ def self.to_s
109
+ load_config
110
+ [@version[:MAJOR], @version[:MINOR], @version[:PATCH]].join('.')
111
+ end
112
+ def self.inspect
113
+ to_s
114
+ end
115
+ def self.load_config
116
+ require 'yaml'
117
+ @version ||= YAML.load_file(File.join(LIB_HOME, '..', '..', 'VERSION.yml'))
118
+ end
119
+ end
120
+
121
+ end
122
+ end
123
+ OT = Onetime unless defined?(OT)
data/onetime.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "onetime"
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Delano Mandelbaum"]
12
+ s.date = "2012-01-06"
13
+ s.description = "CLI tool and Ruby library for onetimesecret.com"
14
+ s.email = "delano@onetimesecret.com"
15
+ s.executables = ["onetime"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt"
18
+ ]
19
+ s.files = [
20
+ "CHANGES.txt",
21
+ "LICENSE.txt",
22
+ "Rakefile",
23
+ "VERSION.yml",
24
+ "bin/onetime",
25
+ "lib/onetime/api.rb",
26
+ "onetime.gemspec"
27
+ ]
28
+ s.homepage = "https://github.com/onetimesecret/onetime-ruby"
29
+ s.require_paths = ["lib"]
30
+ s.rubyforge_project = "onetime"
31
+ s.rubygems_version = "1.8.10"
32
+ s.summary = "CLI tool and Ruby library for onetimesecret.com"
33
+
34
+ if s.respond_to? :specification_version then
35
+ s.specification_version = 3
36
+
37
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
38
+ s.add_runtime_dependency(%q<drydock>, [">= 0.6.9"])
39
+ s.add_runtime_dependency(%q<json>, [">= 1.6.4"])
40
+ else
41
+ s.add_dependency(%q<drydock>, [">= 0.6.9"])
42
+ s.add_dependency(%q<json>, [">= 1.6.4"])
43
+ end
44
+ else
45
+ s.add_dependency(%q<drydock>, [">= 0.6.9"])
46
+ s.add_dependency(%q<json>, [">= 1.6.4"])
47
+ end
48
+ end
49
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onetime
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.3.0
6
+ platform: ruby
7
+ authors:
8
+ - Delano Mandelbaum
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-01-06 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: drydock
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.9
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.6.4
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: CLI tool and Ruby library for onetimesecret.com
38
+ email: delano@onetimesecret.com
39
+ executables:
40
+ - onetime
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE.txt
45
+ files:
46
+ - CHANGES.txt
47
+ - LICENSE.txt
48
+ - Rakefile
49
+ - VERSION.yml
50
+ - bin/onetime
51
+ - lib/onetime/api.rb
52
+ - onetime.gemspec
53
+ homepage: https://github.com/onetimesecret/onetime-ruby
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project: onetime
76
+ rubygems_version: 1.8.10
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: CLI tool and Ruby library for onetimesecret.com
80
+ test_files: []
81
+