credo 1.0.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 by Phil Stewart
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,5 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.pattern = 'test/*_test.rb'
5
+ end
data/lib/credo.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'encryptor'
2
+ require 'highline/import'
3
+
4
+ class Credo
5
+ def initialize(params)
6
+ @params = params
7
+
8
+ @username = @params[:username]
9
+ password = @params.delete(:password)
10
+
11
+ password ||= case params[:prompt]
12
+ when :console
13
+ ask("Enter password: ") { |q| q.echo = false }
14
+ when :test
15
+ 'test'
16
+ end
17
+
18
+ make_key
19
+ password_encrypt(password)
20
+ end
21
+
22
+ def use
23
+ yield @username, password_decrypt
24
+ end
25
+
26
+ private
27
+
28
+ def make_key
29
+ @key = Digest::SHA256.hexdigest(@params[:salt] || (@username.to_s + Random.rand(2 ** 31).to_s))
30
+ end
31
+
32
+ def password_encrypt(password)
33
+ @password_crypted = if password.kind_of?(String) && password.length > 0
34
+ Encryptor.encrypt(password, key: @key)
35
+ else
36
+ nil
37
+ end
38
+ end
39
+
40
+ def password_decrypt
41
+ if @password_crypted.kind_of?(String) && @password_crypted.length > 0
42
+ Encryptor.decrypt(@password_crypted, key: @key)
43
+ else
44
+ nil
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ class Credo
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,48 @@
1
+ require 'test/unit'
2
+ require 'credo'
3
+
4
+ class CredoTest < Test::Unit::TestCase
5
+ def test_use
6
+ credentials = Credo.new(username: 'user', password: 'pass')
7
+ exported_username = nil
8
+ exported_password = nil
9
+
10
+ credentials.use do |username, password|
11
+ exported_username = username
12
+ exported_password = password
13
+ end
14
+
15
+ assert_equal exported_username, 'user'
16
+ assert_equal exported_password, 'pass'
17
+ end
18
+
19
+ def test_missing_password
20
+ credentials = Credo.new(username: 'user')
21
+ exported_password = ""
22
+
23
+ credentials.use do |username, password|
24
+ exported_password = password
25
+ end
26
+
27
+ assert_nil exported_password
28
+ end
29
+
30
+ def test_key_is_random
31
+ creds1 = Credo.new(username: 'user', password: 'pass')
32
+ creds2 = Credo.new(username: 'user', password: 'pass')
33
+
34
+ refute_equal creds1.instance_variable_get(:@key),
35
+ creds2.instance_variable_get(:@key)
36
+ end
37
+
38
+ def test_prompt_mechanism
39
+ credentials = Credo.new(username: 'user', prompt: :test)
40
+ exported_password = nil
41
+
42
+ credentials.use do |username, password|
43
+ exported_password = password
44
+ end
45
+
46
+ assert_equal exported_password, 'test'
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: credo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Phil Stewart
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: encryptor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: highline
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Credo provides a mechanism for storing credentials for situations where
47
+ more desirable mechanisms (i.e. OAuth) are unavailable.
48
+ email:
49
+ - phil.stewart@lichp.co.uk
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/credo/version.rb
55
+ - lib/credo.rb
56
+ - test/credo_test.rb
57
+ - LICENSE
58
+ - Rakefile
59
+ homepage: http://github.com/lichp/credo
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.23
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: A simple way to retain and use credentials
83
+ test_files: []