Class: BitJWT::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/bitjwt/protocol.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#headerObject (readonly)

Returns the value of attribute header.



3
4
5
# File 'lib/bitjwt/protocol.rb', line 3

def header
  @header
end

#payloadObject (readonly)

Returns the value of attribute payload.



3
4
5
# File 'lib/bitjwt/protocol.rb', line 3

def payload
  @payload
end

#signatureObject (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_headerObject



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_payloadObject



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_encodedObject



19
20
21
# File 'lib/bitjwt/protocol.rb', line 19

def header_encoded
  Util.base64url_encode(header)
end

#header_payload_encodedObject



27
28
29
# File 'lib/bitjwt/protocol.rb', line 27

def header_payload_encoded
  "#{header_encoded}.#{payload_encoded}"
end

#header_to_hObject



11
12
13
# File 'lib/bitjwt/protocol.rb', line 11

def header_to_h
  JSON.parse(header)
end

#payload_encodedObject



23
24
25
# File 'lib/bitjwt/protocol.rb', line 23

def payload_encoded
  Util.base64url_encode(payload)
end

#payload_to_hObject



15
16
17
# File 'lib/bitjwt/protocol.rb', line 15

def payload_to_h
  JSON.parse(payload)
end

#send(url, method) ⇒ Object

Raises:



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_encodedObject



31
32
33
# File 'lib/bitjwt/protocol.rb', line 31

def signature_encoded
  Util.base64url_encode(signature)
end

#verifyObject



60
61
62
# File 'lib/bitjwt/protocol.rb', line 60

def verify
  Crypto.verify(header_payload_encoded, signature, header_to_h['kid'])
end