Class: BitJWT::Protocol
- Inherits:
-
Object
- Object
- BitJWT::Protocol
- Defined in:
- lib/bitjwt/protocol.rb
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
Class Method Summary collapse
Instance Method Summary collapse
- #build_response(response) ⇒ Object
- #build_signature(crypto) ⇒ Object
- #header_encoded ⇒ Object
- #header_payload_encoded ⇒ Object
- #header_to_h ⇒ Object
-
#initialize(header, payload, signature = nil) ⇒ Protocol
constructor
A new instance of Protocol.
- #payload_encoded ⇒ Object
- #payload_to_h ⇒ Object
- #send(url, method) ⇒ Object
- #signature_encoded ⇒ Object
- #verify ⇒ Object
Constructor Details
#initialize(header, payload, signature = nil) ⇒ Protocol
5 6 7 8 9 |
# File 'lib/bitjwt/protocol.rb', line 5 def initialize(header, payload, signature = nil) @header = header @payload = payload @signature = signature end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
3 4 5 |
# File 'lib/bitjwt/protocol.rb', line 3 def header @header end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
3 4 5 |
# File 'lib/bitjwt/protocol.rb', line 3 def payload @payload end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
3 4 5 |
# File 'lib/bitjwt/protocol.rb', line 3 def signature @signature end |
Class Method Details
.build_request(crypto, payload = {}) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/bitjwt/protocol.rb', line 64 def self.build_request(crypto, payload = {}) header = default_header.merge({ 'kid' => crypto.bitcoin_address }) payload = default_payload.merge(payload) bitjws = new(header.to_json, payload.to_json) bitjws.build_signature(crypto) bitjws end |
.default_header ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/bitjwt/protocol.rb', line 75 def default_header { 'alg' => 'CUSTOM-BITCOIN-SIGN', 'kid' => '', 'typ' => 'JWT' } end |
.default_payload ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/bitjwt/protocol.rb', line 83 def default_payload { 'aud' => '', 'data' => {}, 'exp' => Time.now.to_f + 3600, 'iat' => Time.now.to_f } end |
Instance Method Details
#build_response(response) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/bitjwt/protocol.rb', line 52 def build_response(response) header, payload, signature = response.split('.') header_decoded = Base64.decode64(header) payload_decoded = Base64.decode64(payload) signature_decoded = Base64.decode64(signature) self.class.new(header_decoded, payload_decoded, signature_decoded) end |
#build_signature(crypto) ⇒ Object
35 36 37 |
# File 'lib/bitjwt/protocol.rb', line 35 def build_signature(crypto) @signature ||= crypto.sign(header_payload_encoded) end |
#header_encoded ⇒ Object
19 20 21 |
# File 'lib/bitjwt/protocol.rb', line 19 def header_encoded Util.base64url_encode(header) end |
#header_payload_encoded ⇒ Object
27 28 29 |
# File 'lib/bitjwt/protocol.rb', line 27 def header_payload_encoded "#{header_encoded}.#{payload_encoded}" end |
#header_to_h ⇒ Object
11 12 13 |
# File 'lib/bitjwt/protocol.rb', line 11 def header_to_h JSON.parse(header) end |
#payload_encoded ⇒ Object
23 24 25 |
# File 'lib/bitjwt/protocol.rb', line 23 def payload_encoded Util.base64url_encode(payload) end |
#payload_to_h ⇒ Object
15 16 17 |
# File 'lib/bitjwt/protocol.rb', line 15 def payload_to_h JSON.parse(payload) end |
#send(url, method) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bitjwt/protocol.rb', line 39 def send(url, method) connection = Excon.new(url, omit_default_port: true) response = connection.request(path: payload_to_h['aud'], method: method, headers: { 'Content-Type' => 'application/jose', 'User-Agent' => 'bitjwt_client' }, body: "#{header_payload_encoded}.#{signature_encoded}") raise ProtocolError.new(response.status, response.body) unless (200..299).cover?(response.status) build_response(response.body) end |
#signature_encoded ⇒ Object
31 32 33 |
# File 'lib/bitjwt/protocol.rb', line 31 def signature_encoded Util.base64url_encode(signature) end |