-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathwechat.rb
57 lines (45 loc) · 1.44 KB
/
wechat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
require 'base64'
require 'openssl/cipher'
require 'wechat/api_loader'
require 'wechat/api'
require 'wechat/mp_api'
require 'wechat/corp_api'
require 'wechat/helpers'
require 'action_controller/wechat_responder'
module Wechat
autoload :Message, 'wechat/message'
autoload :Responder, 'wechat/responder'
autoload :Cipher, 'wechat/cipher'
autoload :ControllerApi, 'wechat/controller_api'
class AccessTokenExpiredError < StandardError; end
class InvalidCredentialError < StandardError; end
class ResponseError < StandardError
attr_reader :error_code
def initialize(errcode, errmsg)
@error_code = errcode
super "#{errmsg}(#{error_code})"
end
end
def self.config(account = :default)
ApiLoader.config(account)
end
def self.api(account = :default)
@wechat_apis ||= {}
@wechat_apis[account.to_sym] ||= ApiLoader.with(account: account)
end
def self.reload_config!
ApiLoader.reload_config!
end
def self.decrypt(encrypted_data, session_key, ivector)
cipher = OpenSSL::Cipher.new('AES-128-CBC')
cipher.decrypt
cipher.key = Base64.decode64(session_key)
cipher.iv = Base64.decode64(ivector)
decrypted_data = Base64.decode64(encrypted_data)
JSON.parse(cipher.update(decrypted_data) + cipher.final)
rescue StandardError => e
{ errcode: 41003, errmsg: e.message }
end
end
ActionView::Base.include Wechat::Helpers if defined? ActionView::Base