username_password 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ == UsernamePassword
2
+
3
+ * http://github.com/baconpat/username_password
4
+
5
+ == DESCRIPTION:
6
+
7
+ Simple library for command line tools to request a username and password. If
8
+ an "auth_file" is specified the input will be written to an encrypted file
9
+ and will be read on future invocations - meaning the user does not have
10
+ to type in their username/password every time.
11
+
12
+ This was specifically written to simplify writing and running simple scripts
13
+ and command line tools that access a web service sitting behind basic HTTP
14
+ authentication.
15
+
16
+ == SYNOPSIS:
17
+
18
+ require 'username_password'
19
+ credentials = UsernamePassword.get
20
+
21
+ In irb:
22
+ > require 'username_password'
23
+ > UsernamePassword.get
24
+ Username: joe224
25
+ Password: secret
26
+ => {:password=>"secret", :username=>"joe224"}
27
+
28
+ Specify a file where the username/password can be written to to prevent having
29
+ to type them in every time a script is run
30
+
31
+ require 'username_password'
32
+ UsernamePassword.auth_file = "/tmp/authfile"
33
+ UsernamePassword.get
34
+
35
+ == LICENSE:
36
+
37
+ The MIT License
38
+
39
+ Copyright (c) 2010 Patrick Bacon
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining a copy
42
+ of this software and associated documentation files (the "Software"), to deal
43
+ in the Software without restriction, including without limitation the rights
44
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
45
+ copies of the Software, and to permit persons to whom the Software is
46
+ furnished to do so, subject to the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be included in
49
+ all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
57
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module UsernamePassword
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ require 'highline'
2
+ require 'ezcrypto'
3
+
4
+ module UsernamePassword
5
+ @@input_io = $stdin
6
+ @@output_io = $stderr
7
+ @@auth_file = nil
8
+
9
+ # Set the IO object to use for input
10
+ def self.input=(io)
11
+ @@input_io = io
12
+ end
13
+
14
+ # Set the IO object to use for output
15
+ def self.output=(io)
16
+ @@output_io = io
17
+ end
18
+
19
+ # Set the location of a file to save the username/password (encrypted) so the user
20
+ # won't have to type it in every time
21
+ def self.auth_file=(path)
22
+ @@auth_file = path
23
+ end
24
+
25
+ # Asks the user for a username/password. If an auth_file has been specified
26
+ # the results will be saved to that file and the user will not have to
27
+ # type the information in again.
28
+ def self.get
29
+ return read_file if @@auth_file && File.exists?(@@auth_file)
30
+
31
+ highline = HighLine.new(@@input_io, @@output_io)
32
+ username = highline.ask("Username: ")
33
+ password = highline.ask("Password: ") { |q| q.echo = false }
34
+
35
+ result = {:username => username, :password => password}
36
+ save_file(result) if @@auth_file
37
+ result
38
+ end
39
+
40
+ private
41
+ def self.save_file(hash) #:nodoc:#
42
+ File.open(@@auth_file, 'w') { |file| file.write encryption.encrypt64(YAML.dump(hash)) }
43
+ end
44
+
45
+ def self.read_file #:nodoc:#
46
+ YAML.load(encryption.decrypt64(File.read(@@auth_file)))
47
+ end
48
+
49
+ def self.encryption #:nodoc:#
50
+ EzCrypto::Key.with_password(%|(FrDx&M(44;{TxU]RtYk69Khma6Li2KFCU:V6jj*YizpM4GiRd|, "salty")
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+ require 'rspec'
3
+ require 'username_password'
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ describe UsernamePassword do
6
+ let(:input) { StringIO.new }
7
+ let(:output) { StringIO.new }
8
+ let(:auth_file) { File.join(Dir.tmpdir, "uptestfile") }
9
+ subject { UsernamePassword }
10
+
11
+ before do
12
+ UsernamePassword.input = input
13
+ UsernamePassword.output = output
14
+ end
15
+
16
+ after do
17
+ UsernamePassword.auth_file = nil
18
+ FileUtils.rm_f(auth_file) if File.exists?(auth_file)
19
+ end
20
+
21
+ it "asks for a username and password" do
22
+ input << "joe224\n"
23
+ input << "secret\n"
24
+ input.rewind
25
+
26
+ result = UsernamePassword.get
27
+ result[:username].should == "joe224"
28
+ result[:password].should == "secret"
29
+
30
+ output.string.should == "Username: Password: \n"
31
+ end
32
+
33
+ it "can save the entered username and password to a file that is used in later calls" do
34
+ UsernamePassword.auth_file = auth_file
35
+
36
+ input << "joe224\n"
37
+ input << "secret\n"
38
+ input.rewind
39
+ UsernamePassword.get
40
+
41
+ # Prevent writing to output
42
+ output.close
43
+
44
+ 3.times do
45
+ UsernamePassword.get.should == {:username => 'joe224', :password => 'secret'}
46
+ end
47
+ end
48
+
49
+ it 'does not save the username password in plaintext' do
50
+ UsernamePassword.auth_file = auth_file
51
+ input << "joe224\n"
52
+ input << "secret\n"
53
+ input.rewind
54
+ UsernamePassword.get
55
+
56
+ file_contents = File.read(auth_file)
57
+ file_contents.should_not =~ /joe224/
58
+ file_contents.should_not =~ /secret/
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "username_password/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "username_password"
7
+ s.version = UsernamePassword::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Patrick Bacon"]
10
+ s.email = ["bacon@atomicobject.com"]
11
+ s.summary = %q{Simple authentication gem to ask user for username/password and save to a file if desired}
12
+ s.description = %q{Simple authentication gem to ask user for username/password and save to a file if desired}
13
+ s.homepage = "http://github.com/baconpat/username_password"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "highline", "~> 1"
21
+ s.add_dependency "ezcrypto"
22
+
23
+ s.add_development_dependency "rspec", "~> 2"
24
+ s.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: username_password
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Patrick Bacon
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-02 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 1
28
+ segments:
29
+ - 1
30
+ version: "1"
31
+ type: :runtime
32
+ name: highline
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ name: ezcrypto
47
+ prerelease: false
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ hash: 7
56
+ segments:
57
+ - 2
58
+ version: "2"
59
+ type: :development
60
+ name: rspec
61
+ prerelease: false
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ type: :development
74
+ name: rake
75
+ prerelease: false
76
+ version_requirements: *id004
77
+ description: Simple authentication gem to ask user for username/password and save to a file if desired
78
+ email:
79
+ - bacon@atomicobject.com
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - README.rdoc
90
+ - Rakefile
91
+ - lib/username_password.rb
92
+ - lib/username_password/version.rb
93
+ - spec/spec_helper.rb
94
+ - spec/username_password_spec.rb
95
+ - username_password.gemspec
96
+ has_rdoc: true
97
+ homepage: http://github.com/baconpat/username_password
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options: []
102
+
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.3.7
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Simple authentication gem to ask user for username/password and save to a file if desired
130
+ test_files:
131
+ - spec/spec_helper.rb
132
+ - spec/username_password_spec.rb