multipass 1.1.3

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,20 @@
1
+ Copyright (c) 2009 rick olson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,12 @@
1
+ MultiPass
2
+ =========
3
+
4
+ Bare bones implementation of encoding and decoding MultiPass values for SSO.
5
+
6
+ MultiPasses are json hashes encrypted with strong AES encryption. They are typically
7
+ passed as cookie values, URL params, or HTTP header values, depending on how
8
+ the individual service chooses to implement it.
9
+
10
+ The idea is that if a site wants to automatically create a local user based
11
+ on the login credentials of another site, it will look for a MultiPass. This
12
+ MultiPass can contain the user's email address, name, etc.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 3
4
+ :major: 1
data/lib/multipass.rb ADDED
@@ -0,0 +1,94 @@
1
+ require 'time'
2
+
3
+ class MultiPass
4
+ class Invalid < StandardError
5
+ @@message = "The MultiPass token is invalid."
6
+
7
+ def message
8
+ @@message
9
+ end
10
+
11
+ alias to_s message
12
+ end
13
+
14
+ class ExpiredError < Invalid
15
+ @@message = "The MultiPass token has expired."
16
+ end
17
+ class JSONError < Invalid
18
+ @@message = "The decrypted MultiPass token is not valid JSON."
19
+ end
20
+ class DecryptError < Invalid
21
+ @@message = "The MultiPass token was not able to be decrypted."
22
+ end
23
+
24
+ def self.encode(site_key, api_key, options = {})
25
+ new(site_key, api_key).encode(options)
26
+ end
27
+
28
+ def self.decode(site_key, api_key, data)
29
+ new(site_key, api_key).decode(data)
30
+ end
31
+
32
+ def initialize(site_key, api_key)
33
+ @site_key = site_key
34
+ @api_key = api_key
35
+ if !Object.const_defined?(:EzCrypto)
36
+ require 'ezcrypto'
37
+ end
38
+ @crypto_key = EzCrypto::Key.with_password(@site_key, @api_key)
39
+ end
40
+
41
+ # Encrypts the given hash into a multipass string.
42
+ def encode(options = {})
43
+ options[:expires] = case options[:expires]
44
+ when Fixnum then Time.at(options[:expires]).to_s
45
+ when Time, DateTime, Date then options[:expires].to_s
46
+ else options[:expires].to_s
47
+ end
48
+ @crypto_key.encrypt64(options.to_json)
49
+ end
50
+
51
+ # Decrypts the given multipass string and parses it as JSON. Then, it checks
52
+ # for a valid expiration date.
53
+ def decode(data)
54
+ json = @crypto_key.decrypt64(data)
55
+
56
+ if json.nil?
57
+ raise MultiPass::DecryptError
58
+ end
59
+
60
+ options = decode_json(json)
61
+
62
+ if !options.is_a?(Hash)
63
+ raise MultiPass::JSONError
64
+ end
65
+
66
+ options.keys.each do |key|
67
+ options[key.to_sym] = options.delete(key)
68
+ end
69
+
70
+ if options[:expires].nil? || Time.now.utc > Time.parse(options[:expires])
71
+ raise MultiPass::ExpiredError
72
+ end
73
+
74
+ options
75
+ rescue OpenSSL::CipherError
76
+ raise MultiPass::DecryptError
77
+ end
78
+
79
+ private
80
+ if Object.const_defined?(:ActiveSupport)
81
+ def decode_json(s)
82
+ ActiveSupport::JSON.decode(s)
83
+ rescue ActiveSupport::JSON::ParseError
84
+ raise MultiPass::JSONError
85
+ end
86
+ else
87
+ require 'json'
88
+ def decode_json(s)
89
+ JSON.parse(s)
90
+ rescue JSON::ParserError
91
+ raise MultiPass::JSONError
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,57 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'ezcrypto'
5
+ require 'multipass'
6
+
7
+ class MultiPassTest < Test::Unit::TestCase
8
+ def setup
9
+ @date = Time.now + 1234
10
+ @input = {:expires => @date, :email => 'ricky@bobby.com'}
11
+ @output = @input.merge(:expires => @input[:expires].to_s)
12
+ @key = EzCrypto::Key.with_password('example', 'abc')
13
+ @mp = MultiPass.new('example', 'abc')
14
+ end
15
+
16
+ def test_encodes_multipass
17
+ expected = @key.encrypt64(@output.to_json)
18
+ assert_equal expected, @mp.encode(@input)
19
+ end
20
+
21
+ def test_encodes_multipass_with_class_method
22
+ expected = @key.encrypt64(@output.to_json)
23
+ assert_equal expected, MultiPass.encode('example', 'abc', @input)
24
+ end
25
+
26
+ def test_decodes_multipass
27
+ encoded = @mp.encode(@input)
28
+ assert_equal @input, @mp.decode(encoded)
29
+ end
30
+
31
+ def test_decodes_multipass_with_class_method
32
+ encoded = @mp.encode(@input)
33
+ assert_equal @input, MultiPass.decode('example', 'abc', encoded)
34
+ end
35
+
36
+ def test_invalidates_bad_string
37
+ assert_raises MultiPass::DecryptError do
38
+ @mp.decode("abc")
39
+ end
40
+ end
41
+
42
+ def test_invalidates_bad_json
43
+ assert_raises MultiPass::JSONError do
44
+ @mp.decode(@key.encrypt64("abc"))
45
+ end
46
+ assert_raises MultiPass::JSONError do
47
+ @mp.decode(@key.encrypt64("{a"))
48
+ end
49
+ end
50
+
51
+ def test_invalidates_old_expiration
52
+ encrypted = @key.encrypt64(@input.merge(:expires => (Time.now - 1)).to_json)
53
+ assert_raises MultiPass::ExpiredError do
54
+ @mp.decode(encrypted)
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multipass
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - rick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-20 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ezcrypto
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: technoweenie@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - LICENSE
34
+ files:
35
+ - VERSION.yml
36
+ - lib/multipass.rb
37
+ - test/multipass_test.rb
38
+ - README
39
+ - LICENSE
40
+ has_rdoc: true
41
+ homepage: http://github.com/entp/multipass
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --inline-source
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Bare bones implementation of encoding and decoding MultiPass values for SSO.
69
+ test_files: []
70
+