bitid 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +94 -0
- data/Rakefile +9 -0
- data/bitid-ruby.gemspec +25 -0
- data/lib/bitid.rb +55 -0
- data/lib/bitid/version.rb +3 -0
- data/test/test_bitid.rb +79 -0
- metadata +104 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Eric Larchevêque
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# BitID
|
2
|
+
|
3
|
+
This is the ruby implementation of the BitID authentication protocol. Basicaly, what the Gem does is
|
4
|
+
building a message challenge and verifying the signature.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'bitid-ruby'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install bitid-ruby
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### Challenge
|
23
|
+
|
24
|
+
To build a challenge, you need to initialize a `Bitid` object with a `nonce` and a `callback`.
|
25
|
+
|
26
|
+
```
|
27
|
+
bitid = Bitid.new(nonce:@nonce, callback:@callback)
|
28
|
+
```
|
29
|
+
|
30
|
+
`nonce` is an random string associated with the user's session id.
|
31
|
+
`callback` is the url where the wallet will post the challenge's signature.
|
32
|
+
|
33
|
+
Once the `Bitid` object is initialized, you have access to the following methods :
|
34
|
+
|
35
|
+
```
|
36
|
+
bitid.message
|
37
|
+
```
|
38
|
+
|
39
|
+
This is the message to sign (the challenge). For instance :
|
40
|
+
|
41
|
+
```
|
42
|
+
Bitcoin Signed Message:
|
43
|
+
bitid://bitid-demo.herokuapp.com/callback?x=987f20277c015ce7
|
44
|
+
```
|
45
|
+
|
46
|
+
```
|
47
|
+
bitid.qrcode
|
48
|
+
```
|
49
|
+
|
50
|
+
The same message, but on QRcode format (this is actualy an URL pointing to the QRcode image).
|
51
|
+
|
52
|
+
### Verification
|
53
|
+
|
54
|
+
When getting the callback from the wallet, you must initialize a `Bitid` object with the received
|
55
|
+
parameters `address`, `uri`, `signature` as well as the excpected `callback` :
|
56
|
+
|
57
|
+
```
|
58
|
+
bitid = Bitid.new(address:@address, uri:@uri, signature:@signature, callback:@callback)
|
59
|
+
```
|
60
|
+
|
61
|
+
You can after call the following methods :
|
62
|
+
|
63
|
+
```
|
64
|
+
bitid.nonce
|
65
|
+
```
|
66
|
+
|
67
|
+
Return the `nonce`, which would get you the user's session.
|
68
|
+
|
69
|
+
```
|
70
|
+
bitid.uri_valid?
|
71
|
+
```
|
72
|
+
|
73
|
+
Returns `true` if the submitted URI is valid and corresponds to the correct `callback` url.
|
74
|
+
|
75
|
+
```
|
76
|
+
bitid.signature_valid?
|
77
|
+
```
|
78
|
+
|
79
|
+
If returns `true`, then you can authenticate the user's session with `address` (public Bitcoin
|
80
|
+
address used to sign the challenge).
|
81
|
+
|
82
|
+
|
83
|
+
## Author
|
84
|
+
|
85
|
+
Eric Larchevêque
|
86
|
+
elarch@gmail.com
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
1. Fork it
|
91
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
92
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
93
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
94
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bitid-ruby.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bitid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bitid"
|
8
|
+
spec.version = Bitid::VERSION
|
9
|
+
spec.authors = ["Eric Larchevêque"]
|
10
|
+
spec.email = ["elarch@gmail.com"]
|
11
|
+
spec.description = %q{Ruby implementation of the BitID authentication protocol}
|
12
|
+
spec.summary = %q{BitID}
|
13
|
+
spec.homepage = "https://github.com/bitid/bitid-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'bitcoin-cigs'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/lib/bitid.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'cgi'
|
3
|
+
require 'bitcoin-cigs'
|
4
|
+
|
5
|
+
class Bitid
|
6
|
+
|
7
|
+
SCHEME = 'bitid'
|
8
|
+
PARAM_NONCE = 'x'
|
9
|
+
SIGNATURE_HEADER = "Bitcoin Signed Message:\n"
|
10
|
+
|
11
|
+
attr_accessor :nonce, :callback, :signature, :uri
|
12
|
+
|
13
|
+
def initialize hash={}
|
14
|
+
@nonce = hash[:nonce]
|
15
|
+
@callback = URI(hash[:callback])
|
16
|
+
@signature = hash[:signature]
|
17
|
+
@address = hash[:address]
|
18
|
+
@uri = hash[:uri].nil? ? build_uri : URI(hash[:uri])
|
19
|
+
end
|
20
|
+
|
21
|
+
def uri_valid?
|
22
|
+
params = CGI::parse(@uri.query)
|
23
|
+
!@uri.nil? && @uri.scheme == SCHEME && @uri.host == @callback.host && @uri.path == @callback.path && !params[PARAM_NONCE][0].nil?
|
24
|
+
rescue
|
25
|
+
end
|
26
|
+
|
27
|
+
def signature_valid?
|
28
|
+
BitcoinCigs.verify_message(@address, @signature, message)
|
29
|
+
end
|
30
|
+
|
31
|
+
def qrcode
|
32
|
+
"http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=" + CGI::escape(message)
|
33
|
+
end
|
34
|
+
|
35
|
+
def nonce
|
36
|
+
CGI::parse(@uri.query)[PARAM_NONCE][0]
|
37
|
+
end
|
38
|
+
|
39
|
+
def message
|
40
|
+
SIGNATURE_HEADER + @uri.to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
def callback
|
44
|
+
@callback
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def build_uri
|
50
|
+
uri = @callback
|
51
|
+
uri.scheme = SCHEME
|
52
|
+
uri.query = URI.encode_www_form({PARAM_NONCE => @nonce})
|
53
|
+
uri
|
54
|
+
end
|
55
|
+
end
|
data/test/test_bitid.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'bitid'
|
3
|
+
|
4
|
+
class TestBitid < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@nonce = "fe32e61882a71074"
|
8
|
+
@callback = "http://localhost:3000/callback"
|
9
|
+
@uri = "bitid://localhost:3000/callback?x=fe32e61882a71074"
|
10
|
+
@address = "1HpE8571PFRwge5coHiFdSCLcwa7qetcn"
|
11
|
+
@signature = "H1cDvRY+UbKNbwlHuS6rJ9376C7RF7NxYB6fZTNEOQo4/UFXezcK0uv1+3/fejJJAMKrnkGEo1Ue00pWB8Gu9SQ="
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_build_uri
|
15
|
+
bitid = Bitid.new(nonce:@nonce, callback:@callback)
|
16
|
+
|
17
|
+
assert !bitid.uri.nil?
|
18
|
+
assert_equal "bitid", bitid.uri.scheme
|
19
|
+
assert_equal "localhost", bitid.uri.host
|
20
|
+
assert_equal 3000, bitid.uri.port
|
21
|
+
assert_equal "/callback", bitid.uri.path
|
22
|
+
|
23
|
+
params = CGI::parse(bitid.uri.query)
|
24
|
+
assert_equal @nonce, params['x'].first
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_build_qrcode
|
28
|
+
bitid = Bitid.new(nonce:@nonce, callback:@callback)
|
29
|
+
|
30
|
+
uri_encoded = CGI::escape(bitid.message)
|
31
|
+
assert_equal "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=#{uri_encoded}", bitid.qrcode
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_build_message
|
35
|
+
bitid = Bitid.new(nonce:@nonce, callback:@callback)
|
36
|
+
|
37
|
+
assert_match /\ABitcoin Signed Message\:\nbitid\:\/\/localhost\:3000\/callback\?x=/, bitid.message
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_verify_uri
|
41
|
+
bitid = Bitid.new(address:@address, uri:@uri, signature:@signature, callback:@callback)
|
42
|
+
assert bitid.uri_valid?
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_fail_uri_verification_if_bad_uri
|
46
|
+
bitid = Bitid.new(address:@address, uri:'garbage', signature:@signature, callback:@callback)
|
47
|
+
assert !bitid.uri_valid?
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_fail_uri_verification_if_bad_scheme
|
51
|
+
bitid = Bitid.new(address:@address, uri:'http://localhost:3000/callback?x=fe32e61882a71074', signature:@signature, callback:@callback)
|
52
|
+
assert !bitid.uri_valid?
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_fail_uri_verification_if_invalid_callback_url
|
56
|
+
bitid = Bitid.new(address:@address, uri:'site.com/callback?x=fe32e61882a71074', signature:@signature, callback:@callback)
|
57
|
+
assert !bitid.uri_valid?
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_verify_signature
|
61
|
+
bitid = Bitid.new(address:@address, uri:@uri, signature:@signature, callback:@callback)
|
62
|
+
assert bitid.signature_valid?
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_fail_verification_if_invalid_signature
|
66
|
+
bitid = Bitid.new(address:@address, uri:@uri, signature:"garbage", callback:@callback)
|
67
|
+
assert !bitid.signature_valid?
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_fail_verification_if_signature_text_doesnt_match
|
71
|
+
bitid = Bitid.new(address:@address, uri:@uri, signature:"H4/hhdnxtXHduvCaA+Vnf0TM4UqdljTsbdIfltwx9+w50gg3mxy8WgLSLIiEjTnxbOPW9sNRzEfjibZXnWEpde4=", callback:@callback)
|
72
|
+
assert !bitid.signature_valid?
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_extract_nonce
|
76
|
+
bitid = Bitid.new(address:@address, uri:@uri, signature:@signature, callback:@callback)
|
77
|
+
assert_equal "fe32e61882a71074", bitid.nonce
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bitid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eric Larchevêque
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bitcoin-cigs
|
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: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Ruby implementation of the BitID authentication protocol
|
63
|
+
email:
|
64
|
+
- elarch@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- bitid-ruby.gemspec
|
75
|
+
- lib/bitid.rb
|
76
|
+
- lib/bitid/version.rb
|
77
|
+
- test/test_bitid.rb
|
78
|
+
homepage: https://github.com/bitid/bitid-ruby
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.25
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: BitID
|
103
|
+
test_files:
|
104
|
+
- test/test_bitid.rb
|