wechat-auth_client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23cf922b0b116f41af1df62a72d3bab7854a36c7
4
+ data.tar.gz: c8eb03c16d0aebdb4dbea691fa8f5dc405b93da7
5
+ SHA512:
6
+ metadata.gz: 14969df975aa33baf257a420cd0f6d306f9c2695edc2b21e760faca831649c33c85e28b01e10ec37f91ca3a873bbc7816dae21245c5d7433eca28a59b9ad4483
7
+ data.tar.gz: 3e0bec8335e5d1a3697ce411d21594d968989d43c45999fef28017121afd35f184d5e93cbeb087104f2541b83ab12a1fd1966d9fdbcc293756733da855f10467
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wechat-auth_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Yuan Cheung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yuan Cheung
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ # Wechat::AuthClient
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'wechat-auth_client', git: 'https://github.com/zhangyuan/wechat-auth_client'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ ## Usage
14
+
15
+ TODO: Write usage instructions here
16
+
17
+ ## Contributing
18
+
19
+ 1. Fork it ( https://github.com/zhangyuan/wechat-auth_client/fork )
20
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
21
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
22
+ 4. Push to the branch (`git push origin my-new-feature`)
23
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,41 @@
1
+ require "wechat/auth_client/version"
2
+ require "cgi"
3
+
4
+ module Wechat
5
+ class AuthClient
6
+ class AccessToken
7
+ ATTRIBUTES = %w(access_token expires_in refresh_token openid scope errcode errmsg).freeze
8
+ attr_reader *ATTRIBUTES
9
+
10
+ def initialize(options = {})
11
+ ATTRIBUTES.each do |name|
12
+ instance_variable_set("@#{name}", options[name])
13
+ end
14
+ end
15
+ end
16
+
17
+ attr_reader :appid, :secret
18
+
19
+ def initialize(appid, secret)
20
+ @appid, @secret = appid, secret
21
+ end
22
+
23
+ def authorize_url(redirect_uri, state, options = {})
24
+ scope = options[:scope] ||= "snsapi_base"
25
+ "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{CGI.escape redirect_uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect"
26
+ end
27
+
28
+ def get_token(code)
29
+ response = connection.get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{appid}&secret=#{secret}&code=#{code}&grant_type=authorization_code")
30
+ access_token = AccessToken.new MultiJson.load(response.body)
31
+ end
32
+
33
+ def connection
34
+ @connection ||= begin
35
+ conn = Faraday.new do |faraday|
36
+ faraday.adapter Faraday.default_adapter
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Wechat
2
+ class AuthClient
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require "bundler"
3
+ require 'bundler/setup'
4
+ Bundler.require
5
+
6
+ require "faraday"
7
+ require "multi_json"
8
+ require 'webmock/rspec'
9
+
10
+ require File.expand_path("../../lib/wechat/auth_client", __FILE__)
@@ -0,0 +1,52 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ RSpec.describe Wechat::AuthClient do
4
+ it 'should generate authorize_url' do
5
+ client = Wechat::AuthClient.new("theappid", "thesecret")
6
+ authorize_url = client.authorize_url("http://example.com/wechat", "999")
7
+ expected_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=theappid&redirect_uri=http%3A%2F%2Fexample.com%2Fwechat&response_type=code&scope=snsapi_base&state=999#wechat_redirect"
8
+ expect(authorize_url).to eq(expected_url)
9
+ end
10
+
11
+ context "correct params" do
12
+ it 'should get token' do
13
+ client = Wechat::AuthClient.new("theappid", "thesecret")
14
+ body = %q(
15
+ {
16
+ "access_token":"ACCESS_TOKEN",
17
+ "expires_in":7200,
18
+ "refresh_token":"REFRESH_TOKEN",
19
+ "openid":"OPENID",
20
+ "scope":"SCOPE"
21
+ }
22
+ )
23
+ stub_request(:get, "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{client.appid}&secret=#{client.secret}&code=thecode&grant_type=authorization_code").to_return(body: body)
24
+
25
+ access_token = client.get_token("thecode")
26
+
27
+ expect(access_token.access_token).to eq("ACCESS_TOKEN")
28
+ expect(access_token.expires_in).to eq(7200)
29
+ expect(access_token.refresh_token).to eq("REFRESH_TOKEN")
30
+ expect(access_token.openid).to eq("OPENID")
31
+ expect(access_token.scope).to eq("SCOPE")
32
+ end
33
+ end
34
+
35
+ context "invalid params" do
36
+ it 'should get error' do
37
+ client = Wechat::AuthClient.new("theappid", "thesecret")
38
+ body = %q(
39
+ {
40
+ "errcode": 40029,
41
+ "errmsg": "invalid code"
42
+ }
43
+ )
44
+ stub_request(:get, "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{client.appid}&secret=#{client.secret}&code=thecode&grant_type=authorization_code").to_return(body: body)
45
+
46
+ access_token = client.get_token("thecode")
47
+
48
+ expect(access_token.errcode).to eq(40029)
49
+ expect(access_token.errmsg).to eq("invalid code")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wechat/auth_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wechat-auth_client"
8
+ spec.version = Wechat::AuthClient::VERSION
9
+ spec.authors = ["Yuan Cheung"]
10
+ spec.email = ["evyuancheung@gmail.com"]
11
+ spec.summary = %q{WeChat OAuth Client}
12
+ spec.description = %q{Simple WeChat OAuth Client}
13
+ spec.license = "MIT"
14
+ spec.homepage = "https://github.com/zhangyuan/wechat-auth_client"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "faraday", "~> 0.9"
22
+ spec.add_runtime_dependency "multi_json", "~> 1.2"
23
+ spec.add_development_dependency "rake", "~> 10.1"
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "webmock", "~> 1.18"
27
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wechat-auth_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuan Cheung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
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.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.18'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.18'
97
+ description: Simple WeChat OAuth Client
98
+ email:
99
+ - evyuancheung@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/wechat/auth_client.rb
112
+ - lib/wechat/auth_client/version.rb
113
+ - spec/spec_helper.rb
114
+ - spec/wechat/auth_client_spec.rb
115
+ - wechat-auth_client.gemspec
116
+ homepage: https://github.com/zhangyuan/wechat-auth_client
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: WeChat OAuth Client
140
+ test_files:
141
+ - spec/spec_helper.rb
142
+ - spec/wechat/auth_client_spec.rb