wechat_app 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +50 -0
- data/README.md +83 -0
- data/Rakefile +10 -0
- data/lib/wechat_app.rb +6 -0
- data/lib/wechat_app/login.rb +27 -0
- data/lib/wechat_app/user_info.rb +32 -0
- data/lib/wechat_app/version.rb +3 -0
- data/wechat_app.gemspec +26 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ec5a190943b97f8049209cf7d6107f63eae5938f96ad6a0f240d56c8d21d2626
|
4
|
+
data.tar.gz: 974b02b87fdc81e5d56a4ae392719112eede62c172f77ffe938c74cbed9076a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6e5e240a7dbff5902a04d26d64247a2b96d856ba0d37fb6358a9140138b67919f27223712cb7db70a8d6df3742ed346d66fdc933ea938d1d0fd817a0a41cf76
|
7
|
+
data.tar.gz: 962c67f449d282543741d6f9796d9f7715df7073d2a95d4b71a445adbcafd292497ffd37f23fc6eefadea68869fe4ab8f5169843a69538819c2be2f8e5b50817
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
wechat_app (0.1.0)
|
5
|
+
rest-client (~> 2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.5.2)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
crack (0.4.3)
|
13
|
+
safe_yaml (~> 1.0.0)
|
14
|
+
domain_name (0.5.20170404)
|
15
|
+
unf (>= 0.0.5, < 1.0.0)
|
16
|
+
hashdiff (0.3.7)
|
17
|
+
http-cookie (1.0.3)
|
18
|
+
domain_name (~> 0.5)
|
19
|
+
mime-types (3.1)
|
20
|
+
mime-types-data (~> 3.2015)
|
21
|
+
mime-types-data (3.2016.0521)
|
22
|
+
minitest (5.11.3)
|
23
|
+
netrc (0.11.0)
|
24
|
+
public_suffix (3.0.2)
|
25
|
+
rake (10.5.0)
|
26
|
+
rest-client (2.0.2)
|
27
|
+
http-cookie (>= 1.0.2, < 2.0)
|
28
|
+
mime-types (>= 1.16, < 4.0)
|
29
|
+
netrc (~> 0.8)
|
30
|
+
safe_yaml (1.0.4)
|
31
|
+
unf (0.1.4)
|
32
|
+
unf_ext
|
33
|
+
unf_ext (0.0.7.5)
|
34
|
+
webmock (3.3.0)
|
35
|
+
addressable (>= 2.3.6)
|
36
|
+
crack (>= 0.3.2)
|
37
|
+
hashdiff
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
bundler (~> 1.16)
|
44
|
+
minitest (~> 5.0)
|
45
|
+
rake (~> 10.0)
|
46
|
+
webmock (~> 3.3.0)
|
47
|
+
wechat_app!
|
48
|
+
|
49
|
+
BUNDLED WITH
|
50
|
+
1.16.1
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# WechatApp
|
2
|
+
|
3
|
+
[微信小程序](https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html)相关API库
|
4
|
+
|
5
|
+
## 安装
|
6
|
+
|
7
|
+
在Gemfile中添加:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'wechat_app'
|
11
|
+
```
|
12
|
+
|
13
|
+
然后运行:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
或者:
|
18
|
+
|
19
|
+
$ gem install wechat_app
|
20
|
+
|
21
|
+
## 使用
|
22
|
+
|
23
|
+
### code 换取 session_key
|
24
|
+
|
25
|
+
参考文档: https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html#wxloginobject
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
|
29
|
+
WechatApp::Login.code2session_info('code', 'appid', 'appsecret')
|
30
|
+
# 具体返回结构参考微信文档
|
31
|
+
# => { "openid": "openid", "session_key": "session_key" }
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
### 用户数据的签名验证和加解密
|
36
|
+
|
37
|
+
参考文档: https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html
|
38
|
+
|
39
|
+
#### 数据签名校验
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
|
43
|
+
WechatApp::UserInfo.data_valid?(raw_data, session_key, signature)
|
44
|
+
# => true
|
45
|
+
|
46
|
+
```
|
47
|
+
|
48
|
+
#### 加密数据解密(获取用户信息)
|
49
|
+
|
50
|
+
``` ruby
|
51
|
+
|
52
|
+
WechatApp::UserInfo.decrypt_data(encrypted_data, session_key, iv)
|
53
|
+
# =>{
|
54
|
+
# "openId": "OPENID",
|
55
|
+
# "nickName": "NICKNAME",
|
56
|
+
# "gender": GENDER,
|
57
|
+
# "city": "CITY",
|
58
|
+
# "province": "PROVINCE",
|
59
|
+
# "country": "COUNTRY",
|
60
|
+
# "avatarUrl": "AVATARURL",
|
61
|
+
# "unionId": "UNIONID",
|
62
|
+
# "watermark":
|
63
|
+
# {
|
64
|
+
# "appid":"APPID",
|
65
|
+
# "timestamp":TIMESTAMP
|
66
|
+
# }
|
67
|
+
# }
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
## Development
|
72
|
+
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
74
|
+
|
75
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wechat_app. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
80
|
+
|
81
|
+
## Code of Conduct
|
82
|
+
|
83
|
+
Everyone interacting in the WechatApp project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/wechat_app/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lib/wechat_app.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module WechatApp
|
4
|
+
module Login
|
5
|
+
class Code2SessionInfoError < StandardError; end
|
6
|
+
|
7
|
+
SESSION_KEY_URL = "https://api.weixin.qq.com/sns/jscode2session"
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def code2session_info(code, wxa_appid, wxa_secret)
|
11
|
+
req_params = {
|
12
|
+
appid: wxa_appid,
|
13
|
+
secret: wxa_secret,
|
14
|
+
js_code: code,
|
15
|
+
grant_type: 'authorization_code'
|
16
|
+
}
|
17
|
+
|
18
|
+
res = JSON.parse(RestClient.get(SESSION_KEY_URL, { params: req_params }))
|
19
|
+
if res['errcode'].nil?
|
20
|
+
res
|
21
|
+
else
|
22
|
+
raise Code2SessionInfoError.new("get session info fail: #{res}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module WechatApp
|
2
|
+
module UserInfo
|
3
|
+
class DecryptedDataFormatError < StandardError; end
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def data_valid?(raw_data, session_key, signature)
|
7
|
+
sign_str = "#{raw_data}#{session_key}"
|
8
|
+
Digest::SHA1.hexdigest(sign_str) == signature
|
9
|
+
end
|
10
|
+
|
11
|
+
def decrypt_data(encrypted_data, session_key, iv)
|
12
|
+
encrypted = Base64.strict_decode64(encrypted_data)
|
13
|
+
decipher = OpenSSL::Cipher::AES128.new(:CBC)
|
14
|
+
decipher.decrypt
|
15
|
+
decipher.key = Base64.strict_decode64(session_key)
|
16
|
+
decipher.iv = Base64.strict_decode64(iv)
|
17
|
+
|
18
|
+
decrypted_data = decipher.update(encrypted) + decipher.final
|
19
|
+
|
20
|
+
begin
|
21
|
+
JSON.parse(decrypted_data)
|
22
|
+
rescue JSON::ParserError => e
|
23
|
+
msg = <<~MSG
|
24
|
+
decrypted_data parse error: #{e.message}
|
25
|
+
decrypted_data: #{decrypted_data}
|
26
|
+
MSG
|
27
|
+
raise DecryptedDataFormatError.new(msg)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/wechat_app.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "wechat_app/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "wechat_app"
|
7
|
+
spec.version = WechatApp::VERSION
|
8
|
+
spec.authors = ["aczero"]
|
9
|
+
spec.email = ["lzh.scut@hotmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Library for wechat application API}
|
12
|
+
spec.description = %q{Library for wechat application(微信小程序) API, wechat docs: https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html}
|
13
|
+
spec.homepage = "https://github.com/ACzero/wechat_app"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rest-client", "~> 2.0"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
24
|
+
spec.add_development_dependency "webmock", "~> 3.3"
|
25
|
+
spec.license = 'MIT'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wechat_app
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aczero
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.3'
|
83
|
+
description: 'Library for wechat application(微信小程序) API, wechat docs: https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html'
|
84
|
+
email:
|
85
|
+
- lzh.scut@hotmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/wechat_app.rb
|
97
|
+
- lib/wechat_app/login.rb
|
98
|
+
- lib/wechat_app/user_info.rb
|
99
|
+
- lib/wechat_app/version.rb
|
100
|
+
- wechat_app.gemspec
|
101
|
+
homepage: https://github.com/ACzero/wechat_app
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.7.3
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Library for wechat application API
|
125
|
+
test_files: []
|