blockstack 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b061ec50aaa10a3c6feb8939111acf4c28c6cc7
4
+ data.tar.gz: 9dce6bc3757ca33baf262eb859797078c83a8f57
5
+ SHA512:
6
+ metadata.gz: c8c0e48cb1966c604863fee585cf1b41b70ec93658024c66ac9858a5290a57e4f7ae0b843e5ca82ba19cf81064d6ef5fb4e51aa2ba7d45f5e4f8d15bf83aa306
7
+ data.tar.gz: 556edb8cce317d6067bd2bb876828f63ac72556b70c574a04dc2c9edc578eb2aa97777e1d11679417c64f11fe36d131164cfe94078b6286e291bc20172a5be4b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blockstack.gemspec
4
+ gemspec
5
+ gem 'jwt', git: 'https://github.com/larrysalibra/ruby-jwt.git', branch: 'add-secp256k1-support'
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2014 Appartisan Limited
2
+ Copyright (c) 2017 Blockstack Inc
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Blockstack
2
+
3
+ The Blockstack ruby library for identity and authentication
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'blockstack'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install blockstack
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/blockstack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blockstack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blockstack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blockstack"
8
+ spec.version = Blockstack::VERSION
9
+ spec.authors = ["Larry Salibra"]
10
+ spec.email = ["rubygems@larrysalibra.com"]
11
+ spec.summary = "The Blockstack ruby library for identity and authentication"
12
+ spec.homepage = "https://github.com/larrysalibra/blockstack-ruby"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.13"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "webmock"
26
+ spec.add_development_dependency "vcr"
27
+
28
+ spec.add_dependency "jwt"
29
+ spec.add_dependency "bitcoin"
30
+ spec.add_dependency "faraday"
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ module Blockstack
2
+ VERSION = "0.5.0"
3
+ end
data/lib/blockstack.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "blockstack/version"
2
+ require "bitcoin"
3
+ require "faraday"
4
+ require "jwt"
5
+
6
+ module Blockstack
7
+ USER_AGENT = "blockstack-ruby #{VERSION}"
8
+ ALGORITHM = "ES256K"
9
+ REQUIRED_CLAIMS = %w(iss iat jti exp username profile publicKeys)
10
+ LEEWAY = 300 # seconds
11
+ VALID_WITHIN = 300 # seconds
12
+ BLOCKSTACK_API = "http://localhost:6270"
13
+
14
+ class InvalidAuthResponse < StandardError; end
15
+
16
+ def self.verify_auth_response(auth_token)
17
+ # decode & verify token without checking signature so we can extract
18
+ # public keys
19
+ public_key = nil
20
+ verify = false
21
+ decoded_tokens = JWT.decode auth_token, public_key, verify, algorithm: ALGORITHM
22
+ decoded_token = decoded_tokens[0]
23
+ puts decoded_token
24
+ REQUIRED_CLAIMS.each do |field|
25
+ raise InvalidAuthResponse.new("Missing required '#{field}' claim.") if !decoded_token.key?(field.to_s)
26
+ end
27
+ raise InvalidAuthResponse.new("Missing required 'iat' claim.") if !decoded_token["iat"]
28
+ raise InvalidAuthResponse.new("'iat' timestamp claim is skewed too far from present.") if (Time.now.to_i - decoded_token["iat"]).abs > VALID_WITHIN
29
+
30
+ public_keys = decoded_token['publicKeys']
31
+
32
+ raise InvalidAuthResponse.new("Invalid publicKeys array: only 1 key is supported") unless public_keys.length == 1
33
+
34
+ compressed_hex_public_key = public_keys[0]
35
+ bignum = OpenSSL::BN.new(compressed_hex_public_key, 16)
36
+ group = OpenSSL::PKey::EC::Group.new 'secp256k1'
37
+ public_key = OpenSSL::PKey::EC::Point.new(group, bignum)
38
+ ecdsa_key = OpenSSL::PKey::EC.new 'secp256k1'
39
+ ecdsa_key.public_key = public_key
40
+ verify = true
41
+
42
+ # decode & verify signature
43
+ decoded_tokens = JWT.decode auth_token, ecdsa_key, verify, algorithm: ALGORITHM, exp_leeway: LEEWAY
44
+ decoded_token = decoded_tokens[0]
45
+
46
+ raise InvalidAuthResponse.new("Public keys don't match issuer address") unless self.public_keys_match_issuer?(decoded_token)
47
+
48
+ raise InvalidAuthResponse.new("Public keys don't match owner of claimed username") unless self.public_keys_match_username?(BLOCKSTACK_API, decoded_token)
49
+ rescue JWT::VerificationError => error
50
+ raise InvalidAuthResponse.new("Signature on JWT is invalid")
51
+ rescue JWT::DecodeError => error
52
+ raise InvalidAuthResponse.new("Unable to decode JWT")
53
+ rescue RuntimeError => error
54
+ raise InvalidAuthResponse.new(error.message)
55
+ end
56
+
57
+ def self.get_did_type(decentralized_id)
58
+ did_parts = decentralized_id.split(':')
59
+ raise 'Decentralized IDs must have 3 parts' if did_parts.length != 3
60
+ raise 'Decentralized IDs must start with "did"' if did_parts[0].downcase != 'did'
61
+ did_parts[1].downcase
62
+ end
63
+
64
+ def self.get_address_from_did(decentralized_id)
65
+ did_type = get_did_type(decentralized_id)
66
+ return nil if did_type != 'btc-addr'
67
+ decentralized_id.split(':')[2]
68
+ end
69
+
70
+ def self.public_keys_match_issuer?(decoded_token)
71
+ public_keys = decoded_token['publicKeys']
72
+ address_from_issuer = get_address_from_did(decoded_token['iss'])
73
+
74
+ raise 'Multiple public keys are not supported' unless public_keys.count == 1
75
+
76
+ address_from_public_keys = Bitcoin::pubkey_to_address public_keys.first
77
+
78
+ address_from_issuer == address_from_public_keys
79
+ end
80
+
81
+ def self.public_keys_match_username?(blockstack_api, decoded_token)
82
+ username = decoded_token["username"]
83
+ return true if username.nil?
84
+
85
+ response = Faraday.get "#{blockstack_api}/v1/names/#{username}"
86
+ json = JSON.parse response.body
87
+
88
+ raise "Issuer claimed username that doesn't exist" if response.status == 404
89
+ raise "Unable to verify issuer's claimed username" if response.status != 200
90
+
91
+ name_owning_address = json['address']
92
+ address_from_issuer = get_address_from_did decoded_token['iss']
93
+ name_owning_address == address_from_issuer
94
+ end
95
+
96
+ protected
97
+
98
+ def self.faraday
99
+ connection = Faraday.new
100
+ connection.headers[:user_agent] = USER_AGENT
101
+ connection
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blockstack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Larry Salibra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jwt
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bitcoin
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: faraday
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - rubygems@larrysalibra.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".travis.yml"
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - bin/console
139
+ - bin/setup
140
+ - blockstack.gemspec
141
+ - lib/blockstack.rb
142
+ - lib/blockstack/version.rb
143
+ homepage: https://github.com/larrysalibra/blockstack-ruby
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.6.8
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: The Blockstack ruby library for identity and authentication
167
+ test_files: []