class BitJWT::Protocol

Attributes

header[R]
payload[R]
signature[R]

Public Class Methods

build_request(crypto, payload = {}) click to toggle source
# File lib/bitjwt/protocol.rb, line 65
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
new(header, payload, signature = nil) click to toggle source
# File lib/bitjwt/protocol.rb, line 5
def initialize(header, payload, signature = nil)
  @header = header
  @payload = payload
  @signature = signature
end

Private Class Methods

default_header() click to toggle source
# File lib/bitjwt/protocol.rb, line 76
def default_header
  {
    'alg' => 'CUSTOM-BITCOIN-SIGN',
    'kid' => '',
    'typ' => 'JWT'
  }
end
default_payload() click to toggle source
# File lib/bitjwt/protocol.rb, line 84
def default_payload
  {
    'aud'  => '',
    'data' => {},
    'exp'  => Time.now.to_f + 3600,
    'iat'  => Time.now.to_f
  }
end

Public Instance Methods

build_response(response) click to toggle source
# File lib/bitjwt/protocol.rb, line 53
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) click to toggle source
# File lib/bitjwt/protocol.rb, line 35
def build_signature(crypto)
  @signature ||= crypto.sign(header_payload_encoded)
end
header_encoded() click to toggle source
# File lib/bitjwt/protocol.rb, line 19
def header_encoded
  Util.base64url_encode(header)
end
header_payload_encoded() click to toggle source
# File lib/bitjwt/protocol.rb, line 27
def header_payload_encoded
  "#{header_encoded}.#{payload_encoded}"
end
header_to_h() click to toggle source
# File lib/bitjwt/protocol.rb, line 11
def header_to_h
  JSON.parse(header)
end
payload_encoded() click to toggle source
# File lib/bitjwt/protocol.rb, line 23
def payload_encoded
  Util.base64url_encode(payload)
end
payload_to_h() click to toggle source
# File lib/bitjwt/protocol.rb, line 15
def payload_to_h
  JSON.parse(payload)
end
send(url, method, raw_response = false) click to toggle source
# File lib/bitjwt/protocol.rb, line 39
def send(url, method, raw_response = false)
  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)
  return response.body if raw_response
  build_response(response.body)
end
signature_encoded() click to toggle source
# File lib/bitjwt/protocol.rb, line 31
def signature_encoded
  Util.base64url_encode(signature)
end
verify() click to toggle source
# File lib/bitjwt/protocol.rb, line 61
def verify
  Crypto.verify(header_payload_encoded, signature, header_to_h['kid'])
end