Module: MixinSdk

Defined in:
lib/mixin_sdk.rb,
lib/mixin_sdk/version.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



28
29
30
# File 'lib/mixin_sdk.rb', line 28

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



32
33
34
# File 'lib/mixin_sdk.rb', line 32

def configure
  yield(configuration)
end

.decrypt_pin(msg) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mixin_sdk.rb', line 97

def decrypt_pin(msg)
  msg = Base64.strict_decode64 msg
  pin_token = Base64.decode64(configuration.pin_token)
  private_key = OpenSSL::PKey::RSA.new(configuration.private_key)
  iv = msg[0..15]
  cipher = msg[16..47]
  aes_key = JOSE::JWA::PKCS1::rsaes_oaep_decrypt('SHA256', pin_token, private_key, configuration.session_id)
  alg = "AES-256-CBC"
  decode_cipher = OpenSSL::Cipher.new(alg)
  decode_cipher.decrypt
  decode_cipher.iv = iv
  decode_cipher.key = aes_key
  plain = decode_cipher.update(cipher)
  return plain
end

.encrypt_pin(pin_code) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mixin_sdk.rb', line 67

def encrypt_pin(pin_code)
  pin_token = Base64.decode64(configuration.pin_token)
  private_key = OpenSSL::PKey::RSA.new(configuration.private_key)
  aes_key = JOSE::JWA::PKCS1::rsaes_oaep_decrypt('SHA256', pin_token, private_key, configuration.session_id)
  now_time = Time.now.to_i
  zero_time = now_time % 0x100
  one_time = (now_time % 0x10000) >> 8
  two_time = (now_time % 0x1000000) >> 16
  three_time = (now_time % 0x100000000) >> 24
  time_string = zero_time.chr + one_time.chr + two_time.chr + three_time.chr + "\0\0\0\0"
  encrypt_content = pin_code + time_string + time_string
  pad_count = 16 - encrypt_content.length % 16

  if pad_count > 0
    padded_content = encrypt_content + pad_count.chr * pad_count
  else
    padded_content = encrypt_content
  end

  alg = "AES-256-CBC"
  aes = OpenSSL::Cipher.new(alg)
  iv = OpenSSL::Cipher.new(alg).random_iv
  aes.encrypt
  aes.key = aes_key
  aes.iv = iv
  cipher = aes.update(padded_content)
  msg = iv + cipher
  return Base64.strict_encode64 msg
end

.jwt_token(method_name, uri, body) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mixin_sdk.rb', line 36

def jwt_token(method_name, uri, body)
  now_time = Time.now.to_i
  sign = method_name == "POST" ? "POST/#{uri}#{body}" : "GET/#{uri}"
  payload = {
    uid: configuration.client_id,
    sid: configuration.session_id,
    iat: now_time,
    exp: now_time + 3600,
    jti: SecureRandom.uuid,
    sig: Digest::SHA256.hexdigest(sign)
  }
  rsa_private = OpenSSL::PKey::RSA.new(configuration.private_key)
  JWT.encode(payload, rsa_private, 'RS512')
end

.mixin(method_type, method_name, body = '') ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mixin_sdk.rb', line 51

def mixin(method_type, method_name, body = '')
  method_type = method_type.upcase
  return p "没有此请求类型" unless ["GET", "POST"].include?(method_type)

  url = 'https://api.mixin.one/' + method_name
  token = jwt_token(method_type, method_name, body == '' ? '' : body)

  if method_type == "GET"
    result = HTTParty.get(url, headers:{'Authorization' => 'Bearer ' + token, 'Content-Type' => 'application/json'})
  else
    result = HTTParty.post(url, headers:{'Authorization' => 'Bearer '+ token, 'Content-Type' => 'application/json'}, body: body)
  end
  response = result.parsed_response
  response["error"] ? response["error"]["description"] : response["data"]
end