omniauth-open-wechat-oauth2 1.0.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 898204931fda39d598afa5167f614ec00d1a906c
4
+ data.tar.gz: 8bb7018958785517be7e0463cd431cab7b0723cd
5
+ SHA512:
6
+ metadata.gz: 91d3556864d848465f01503e8a9447a0e6ff144e0f8f39f5cc40ab918e3d3321453612cba22e88386f4753101f6ef9dbc7c319a675eac25493ca7f7b8511b3d8
7
+ data.tar.gz: 80509ae0d755a1d1c6a5349d2799ac7acef68e7c70e1f99f3cdad04a2416723aa5884ba3869d9906dc9a1123f3088f95ce88be1ff64f5f877a9c6ee1e16d8cac
data/.gitignore ADDED
@@ -0,0 +1,46 @@
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
23
+
24
+ # ===
25
+ # vim
26
+ # ===
27
+ # https://github.com/github/gitignore/blob/master/Global/vim.gitignore
28
+ .*.sw[a-z]
29
+ *.un~
30
+ Session.vim
31
+ .netrwhist
32
+
33
+ # ========
34
+ # textmate
35
+ # ========
36
+ # https://github.com/github/gitignore/blob/master/Global/TextMate.gitignore
37
+ *.tmproj
38
+ *.tmproject
39
+ tmtags
40
+
41
+ # =======
42
+ # sublime
43
+ # =======
44
+ # https://github.com/github/gitignore/blob/master/Global/SublimeText.gitignore
45
+ # SublimeText project files
46
+ *.sublime-workspace
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ if ENV['NOGFW']
2
+ source 'https://rubygems.org'
3
+ else
4
+ source 'https://ruby.taobao.org'
5
+ end
6
+
7
+ # Specify your gem's dependencies in omniauth-open-wechat-oauth2.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Special Leung
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.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # omniauth-open-wechat-oauth2
2
+
3
+ Using OAuth2 to authenticate wechat user in web application.
4
+ Base on [https://github.com/skinnyworm/omniauth-wechat-oauth2](https://github.com/skinnyworm/omniauth-wechat-oauth2)
5
+
6
+ Open Wechat Document: [https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN](https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'omniauth-open-wechat-oauth2'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install omniauth-open-wechat-oauth2
21
+
22
+ ## Usage
23
+
24
+ Add provider to `config/initializers/omniauth.rb`
25
+
26
+ ```ruby
27
+ Rails.application.config.middleware.use OmniAuth::Builder do
28
+ provider :open_wechat, 'open_wechat_app_id', 'open_wechat_app_secret'
29
+ end
30
+ ```
31
+
32
+ Access the OmniAuth Open Wechat OAuth2 URL: /auth/open_wechat
33
+
34
+ ## Auth Hash
35
+
36
+ A example of `request.env["omniauth.auth"]` :
37
+
38
+ ```ruby
39
+ {
40
+ :provider => "open_wechat",
41
+ :uid => "unionid",
42
+ :info => {
43
+ openid: "openid",
44
+ nickname: "Nickname",
45
+ sex: 1,
46
+ province: "Changning",
47
+ city: "Shanghai",
48
+ country: "China",
49
+ headimgurl: "http://image_url"
50
+ },
51
+ :credentials => {
52
+ :token => "token",
53
+ :refresh_token => "another_token",
54
+ :expires_at => 7200,
55
+ :expires => true
56
+ },
57
+ :extra => {
58
+ :raw_info => {
59
+ openid: "openid"
60
+ nickname: "Nickname",
61
+ sex: 1,
62
+ province: "Changning",
63
+ city: "Shanghai",
64
+ country: "China",
65
+ headimgurl: "http://image_url",
66
+ unionid: "xxxxxxxxx"
67
+ }
68
+ }
69
+ }
70
+ ```
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it ( https://github.com/[my-github-username]/omniauth-open-wechat-oauth2/fork )
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.3
@@ -0,0 +1 @@
1
+ require "omniauth/strategies/open_wechat"
@@ -0,0 +1,70 @@
1
+ require "omniauth-oauth2"
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class OpenWechat < OmniAuth::Strategies::OAuth2
6
+ option :name, "open_wechat"
7
+
8
+ option :client_options, {
9
+ site: "https://api.weixin.qq.com",
10
+ authorize_url: "https://open.weixin.qq.com/connect/qrconnect#wechat_redirect",
11
+ token_url: "/sns/oauth2/access_token",
12
+ token_method: :get
13
+ }
14
+
15
+ option :authorize_params, {scope: "snsapi_login"}
16
+
17
+ option :token_params, {parse: :json}
18
+
19
+ uid do
20
+ raw_info['unionid']
21
+ end
22
+
23
+ info do
24
+ {
25
+ openid: raw_info["openid"],
26
+ nickname: raw_info['nickname'],
27
+ sex: raw_info['sex'],
28
+ province: raw_info['province'],
29
+ city: raw_info['city'],
30
+ country: raw_info['country'],
31
+ headimgurl: raw_info['headimgurl']
32
+ }
33
+ end
34
+
35
+ extra do
36
+ {raw_info: raw_info}
37
+ end
38
+
39
+ def request_phase
40
+ params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
41
+ params["appid"] = params.delete("client_id")
42
+ redirect client.authorize_url(params)
43
+ end
44
+
45
+ def raw_info
46
+ @uid ||= access_token["openid"]
47
+ @raw_info ||= begin
48
+ access_token.options[:mode] = :query
49
+ if access_token["scope"] && access_token["scope"].include?("snsapi_login")
50
+ @raw_info = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :json).parsed
51
+ else
52
+ @raw_info = {"openid" => @uid }
53
+ end
54
+ end
55
+ end
56
+
57
+ protected
58
+ def build_access_token
59
+ params = {
60
+ 'appid' => client.id,
61
+ 'secret' => client.secret,
62
+ 'code' => request.params['code'],
63
+ 'grant_type' => 'authorization_code'
64
+ }.merge(token_params.to_hash(symbolize_keys: true))
65
+ client.get_token(params, deep_symbolize(options.auth_token_params))
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ version = File.read(File.expand_path('../VERSION', __FILE__)).strip
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "omniauth-open-wechat-oauth2"
8
+ spec.version = version
9
+ spec.authors = ["Special Leung"]
10
+ spec.email = ["specialcyci@gmail.com"]
11
+ spec.summary = 'Omniauth strategy for open wechat(weixin), https://open.weixin.qq.com/'
12
+ spec.description = 'Using OAuth2 to authenticate wechat user in web application.'
13
+ spec.homepage = "https://github.com/mycolorway/omniauth-open-wechat-oauth2"
14
+ spec.license = "MIT"
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_dependency 'omniauth', '~> 1.0'
22
+ spec.add_dependency 'omniauth-oauth2', '~> 1.0'
23
+ spec.add_development_dependency 'rspec', '~> 2.7'
24
+ spec.add_development_dependency "bundler", '~> 1.6'
25
+ spec.add_development_dependency "rake", '~> 10.4'
26
+ end
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::OpenWechat do
4
+ let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}, :scheme=>"http", :url=>"localhost") }
5
+ let(:app) { ->{[200, {}, ["Hello."]]}}
6
+ let(:client){OAuth2::Client.new('appid', 'secret')}
7
+
8
+ subject do
9
+ OmniAuth::Strategies::OpenWechat.new(app, 'appid', 'secret', @options || {}).tap do |strategy|
10
+ allow(strategy).to receive(:request) {
11
+ request
12
+ }
13
+ end
14
+ end
15
+
16
+ before do
17
+ OmniAuth.config.test_mode = true
18
+ end
19
+
20
+ after do
21
+ OmniAuth.config.test_mode = false
22
+ end
23
+
24
+ describe '#client_options' do
25
+ specify 'has site' do
26
+ expect(subject.client.site).to eq('https://api.weixin.qq.com')
27
+ end
28
+
29
+ specify 'has authorize_url' do
30
+ expect(subject.client.options[:authorize_url]).to eq('https://open.weixin.qq.com/connect/qrconnect#wechat_redirect')
31
+ end
32
+
33
+ specify 'has token_url' do
34
+ expect(subject.client.options[:token_url]).to eq('/sns/oauth2/access_token')
35
+ end
36
+ end
37
+
38
+ describe "#authorize_params" do
39
+ specify "default scope is snsapi_login" do
40
+ expect(subject.authorize_params[:scope]).to eq("snsapi_login")
41
+ end
42
+ end
43
+
44
+ describe "#token_params" do
45
+ specify "token response should be parsed as json" do
46
+ expect(subject.token_params[:parse]).to eq(:json)
47
+ end
48
+ end
49
+
50
+ describe 'state' do
51
+ specify 'should set state params for request as a way to verify CSRF' do
52
+ expect(subject.authorize_params['state']).not_to be_nil
53
+ expect(subject.authorize_params['state']).to eq(subject.session['omniauth.state'])
54
+ end
55
+ end
56
+
57
+
58
+ describe "#request_phase" do
59
+ specify "redirect uri includes'appid','redirect_uri','response_type','scope','state'and'wechat_redirect'fragment" do
60
+ callback_url = "http://exammple.com/callback"
61
+
62
+ subject.stub(:callback_url=>callback_url)
63
+ subject.should_receive(:redirect).with do |redirect_url|
64
+ uri = URI.parse(redirect_url)
65
+ expect(uri.fragment).to eq("wechat_redirect")
66
+ params = CGI::parse(uri.query)
67
+ expect(params["appid"]).to eq(['appid'])
68
+ expect(params["redirect_uri"]).to eq([callback_url])
69
+ expect(params["response_type"]).to eq(['code'])
70
+ expect(params["scope"]).to eq(['snsapi_login'])
71
+ expect(params["state"]).to eq([subject.session['omniauth.state']])
72
+ end
73
+
74
+ subject.request_phase
75
+ end
76
+ end
77
+
78
+ describe "#build_access_token" do
79
+ specify "request includes'appid','secret','code','grant_type'and will parse response as json"do
80
+ subject.stub(:client => client, :request=>double("request", params:{"code"=>"server_code"}))
81
+ client.should_receive(:get_token).with({
82
+ "appid" => "appid",
83
+ "secret" => "secret",
84
+ "code" => "server_code",
85
+ "grant_type" => "authorization_code",
86
+ :parse => :json
87
+ },{})
88
+ subject.send(:build_access_token)
89
+ end
90
+ end
91
+
92
+ describe "#raw_info" do
93
+ let(:access_token) { OAuth2::AccessToken.from_hash(client, {}) }
94
+ before { subject.stub(:access_token => access_token) }
95
+
96
+ context "when scope is snsapi_base" do
97
+ let(:access_token) { OAuth2::AccessToken.from_hash(client, {
98
+ "openid"=>"openid",
99
+ "scope"=>"snsapi_base",
100
+ "access_token"=>"access_token"
101
+ })}
102
+
103
+ specify "only have openid" do
104
+ expect(subject.raw_info).to eq("openid" => "openid")
105
+ end
106
+ end
107
+
108
+ context "when scope is snsapi_userinfo" do
109
+ let(:access_token) { OAuth2::AccessToken.from_hash(client, {
110
+ "openid"=>"openid",
111
+ "scope"=>"snsapi_userinfo",
112
+ "access_token"=>"access_token"
113
+ })}
114
+
115
+ specify "will query for user info" do
116
+ response_hash = {
117
+ "openid" => "OPENID",
118
+ "nickname" => "NICKNAME",
119
+ "sex" => "1",
120
+ "province" => "PROVINCE",
121
+ "city" => "CITY",
122
+ "country" => "COUNTRY",
123
+ "headimgurl" => "header_image_url",
124
+ "privilege" => [ "PRIVILEGE1" "PRIVILEGE2"]
125
+ }
126
+
127
+ client.should_receive(:request).with do |verb, path, opts|
128
+ expect(verb).to eq(:get)
129
+ expect(path).to eq("/sns/userinfo")
130
+ expect(opts[:params]).to eq("openid"=> "openid", "access_token"=> "access_token")
131
+ expect(opts[:parse]).to eq(:json)
132
+ end.and_return(double("response", parsed:response_hash))
133
+
134
+ expect(subject.raw_info).to eq(response_hash)
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+
142
+
143
+ end
@@ -0,0 +1,2 @@
1
+ require 'omniauth-open-wechat-oauth2'
2
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-open-wechat-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Special Leung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.4'
83
+ description: Using OAuth2 to authenticate wechat user in web application.
84
+ email:
85
+ - specialcyci@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - VERSION
96
+ - lib/omniauth-open-wechat-oauth2.rb
97
+ - lib/omniauth/strategies/open_wechat.rb
98
+ - omniauth-open-wechat-oauth2.gemspec
99
+ - spec/omniauth/strategies/open_wechat_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/mycolorway/omniauth-open-wechat-oauth2
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.4.8
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Omniauth strategy for open wechat(weixin), https://open.weixin.qq.com/
125
+ test_files:
126
+ - spec/omniauth/strategies/open_wechat_spec.rb
127
+ - spec/spec_helper.rb