omniauth-wechat-oauth2 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8ce6f400f236ade461b6de6f0475e29d6efa2663
4
- data.tar.gz: 7406fddc873587f98f976085265bccee6c450d4c
2
+ SHA256:
3
+ metadata.gz: 6887df3d9cb9e1d0af5f77a80ceb353ed99e45c4aded5802b94f8ff06c2b44b7
4
+ data.tar.gz: 45081ea29f1423808a236d5d708ac21b0b76aa86fa6032add3f06de1b1251168
5
5
  SHA512:
6
- metadata.gz: 324edff8a03b06a2eec788c5a612efef7ca2099be123fe721f6d77995a94a59f9a35e2adc5a52599d21cd1a00d880a5e7522155d1fa447e0cdecd67c91f3645e
7
- data.tar.gz: a2cdacfde5eb3c2f0d87a7a5f30bde685315d242f6dbc5c9247a43a42502b60996b2d7702adb4031f9f8f62fc4564181afa391977a46a31b38479f8004bb6dea
6
+ metadata.gz: 91ca3a9a3ff98631767e7f54f16206a08ca488fe5a0a24709ad9c8f07e4e3a1483960326a256f7b36acaefae293f01e2cd57a672d9fe3d69205bc3d3be575a51
7
+ data.tar.gz: 7a0d38a8a9e51213e3d7981a45b599cc12b5f35ce8f85da86e04acb9c5b2b8c91084c6971beb1fe87ad834376e03875c94566f8725ad1cadc7583fc680a34a13
data/README.md CHANGED
@@ -1,11 +1,21 @@
1
1
  Omniauth-wechat-oauth2
2
2
  ======================
3
3
 
4
- Wechat OAuth2 Strategy for OmniAuth 1.0.
4
+ [![Gem Version](https://img.shields.io/gem/v/omniauth-wechat-oauth2.svg)][gem]
5
+ [![Security Check](https://hakiri.io/github/NeverMin/omniauth-wechat-oauth2/master.svg)][security]
6
+ [![Build Status](https://travis-ci.org/NeverMin/omniauth-wechat-oauth2.svg)][travis]
5
7
 
6
- You need to get a wechat API key at: http://mp.weixin.qq.com
8
+ [gem]: https://rubygems.org/gems/omniauth-wechat-oauth2
9
+ [security]: https://hakiri.io/github/NeverMin/omniauth-wechat-oauth2/master
10
+ [travis]: https://travis-ci.org/NeverMin/omniauth-wechat-oauth2
7
11
 
8
- Wechat oauth2 specification can be found at: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
12
+
13
+ Wechat OAuth2 Strategy for OmniAuth 1.0.
14
+
15
+ You need to get a wechat API key at: https://mp.weixin.qq.com
16
+
17
+ * Wechat oauth2 specification can be found at: https://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
18
+ * Wechat Qiye oauth2 specification can be found at: http://qydev.weixin.qq.com/wiki/index.php?title=OAuth验证接口
9
19
 
10
20
  ## Installation
11
21
 
@@ -57,7 +67,8 @@ Here's an example of an authentication hash available in the callback by accessi
57
67
  province: "Changning",
58
68
  city: "Shanghai",
59
69
  country: "China",
60
- headimgurl: "http://image_url"
70
+ headimgurl: "http://image_url",
71
+ unionid: 'unionid'
61
72
  },
62
73
  :credentials => {
63
74
  :token => "token",
@@ -73,12 +84,55 @@ Here's an example of an authentication hash available in the callback by accessi
73
84
  province: "Changning",
74
85
  city: "Shanghai",
75
86
  country: "China",
76
- headimgurl: "http://image_url"
87
+ headimgurl: "http://image_url",
88
+ unionid: 'unionid'
77
89
  }
78
90
  }
79
91
  }
80
92
  ```
81
93
 
94
+ ## Wechat Qiye OAuth2
82
95
 
96
+ Wechat Qiey usage and configuration are the same with normal account above.
97
+
98
+ ```ruby
99
+ config.omniauth :wechat_qiye, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"],
100
+ :authorize_params => {:scope => "snsapi_base"}
101
+ ```
83
102
 
103
+ Auth hash `request.env["omniauth.auth"]`
84
104
 
105
+ ```ruby
106
+ {
107
+ :provider => "wechat_qiye",
108
+ :uid => "123456789",
109
+ :info => {
110
+ userid: "userid",
111
+ name: "name",
112
+ department: [2],
113
+ gender: "1",
114
+ weixinid: "weixinid",
115
+ avatar: "avatar",
116
+ status: 1,
117
+ extattr: {"foo" => "bar"}
118
+ },
119
+ :credentials => {
120
+ :token => "token",
121
+ :refresh_token => "another_token",
122
+ :expires_at => 7200,
123
+ :expires => true
124
+ },
125
+ :extra => {
126
+ :raw_info => {
127
+ userid: "userid",
128
+ name: "name",
129
+ department: [2],
130
+ gender: "1",
131
+ weixinid: "weixinid",
132
+ avatar: "avatar",
133
+ status: 1,
134
+ extattr: {"foo" => "bar"}}
135
+ }
136
+ }
137
+ }
138
+ ```
@@ -1 +1 @@
1
- require "omniauth/strategies/wechat"
1
+ require "omniauth/wechat"
@@ -7,15 +7,19 @@ module OmniAuth
7
7
 
8
8
  option :client_options, {
9
9
  site: "https://api.weixin.qq.com",
10
- authorize_url: "https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect",
10
+ authorize_url: "https://open.weixin.qq.com/connect/qrconnect?#wechat_redirect",
11
11
  token_url: "/sns/oauth2/access_token",
12
12
  token_method: :get
13
13
  }
14
14
 
15
- option :authorize_params, {scope: "snsapi_userinfo"}
15
+ option :authorize_params, {scope: "snsapi_login"}
16
16
 
17
17
  option :token_params, {parse: :json}
18
18
 
19
+ def callback_url
20
+ full_host + script_name + callback_path
21
+ end
22
+
19
23
  uid do
20
24
  raw_info['openid']
21
25
  end
@@ -27,7 +31,9 @@ module OmniAuth
27
31
  province: raw_info['province'],
28
32
  city: raw_info['city'],
29
33
  country: raw_info['country'],
30
- headimgurl: raw_info['headimgurl']
34
+ headimgurl: raw_info['headimgurl'],
35
+ image: raw_info['headimgurl'],
36
+ unionid: raw_info['unionid']
31
37
  }
32
38
  end
33
39
 
@@ -36,8 +42,9 @@ module OmniAuth
36
42
  end
37
43
 
38
44
  def request_phase
39
- params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
45
+ params = client.auth_code.authorize_params.merge(authorize_params)
40
46
  params["appid"] = params.delete("client_id")
47
+ params["redirect_uri"] = callback_url
41
48
  redirect client.authorize_url(params)
42
49
  end
43
50
 
@@ -45,25 +52,27 @@ module OmniAuth
45
52
  @uid ||= access_token["openid"]
46
53
  @raw_info ||= begin
47
54
  access_token.options[:mode] = :query
48
- if access_token["scope"] == "snsapi_userinfo"
49
- @raw_info = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :json).parsed
55
+ if access_token["scope"]&.include?("snsapi_login")
56
+ access_token.get("/sns/userinfo", :params => { "openid" => @uid, "lang" => "zh_CN" }, parse: :json).parsed
50
57
  else
51
- @raw_info = {"openid" => @uid }
58
+ { "openid" => @uid }
52
59
  end
53
60
  end
61
+ @raw_info
54
62
  end
55
63
 
56
64
  protected
57
65
  def build_access_token
58
66
  params = {
59
- 'appid' => client.id,
60
- 'secret' => client.secret,
61
- 'code' => request.params['code'],
62
- 'grant_type' => 'authorization_code'
67
+ 'appid' => client.id,
68
+ 'secret' => client.secret,
69
+ 'code' => request.params['code'],
70
+ 'grant_type' => 'authorization_code',
71
+ 'redirect_uri' => callback_url
63
72
  }.merge(token_params.to_hash(symbolize_keys: true))
64
73
  client.get_token(params, deep_symbolize(options.auth_token_params))
65
74
  end
66
75
 
67
76
  end
68
77
  end
69
- end
78
+ end
@@ -0,0 +1,80 @@
1
+ require "omniauth-oauth2"
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class WechatQiye < OmniAuth::Strategies::OAuth2
6
+ option :name, "wechat_qiye"
7
+
8
+ option :client_options, {
9
+ :site => "https://qyapi.weixin.qq.com",
10
+ authorize_url: "https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect",
11
+ token_url: "/cgi-bin/gettoken",
12
+ token_method: :get,
13
+ connection_opts: {
14
+ ssl: { verify: false }
15
+ }
16
+ }
17
+
18
+ option :authorize_params, {scope: "snsapi_userinfo"}
19
+ option :token_params, {parse: :json}
20
+
21
+ uid do
22
+ raw_info['userid']
23
+ end
24
+
25
+ info do
26
+ {
27
+ userid: raw_info['userid'],
28
+ name: raw_info['name'],
29
+ department: raw_info['department'],
30
+ gender: raw_info['gender'],
31
+ weixinid: raw_info['weixinid'],
32
+ avatar: raw_info['avatar'],
33
+ status: raw_info['status'],
34
+ extattr: raw_info['extattr']
35
+ }
36
+ end
37
+
38
+ extra do
39
+ { raw_info: raw_info }
40
+ end
41
+
42
+ def request_phase
43
+ params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
44
+ params["appid"] = params.delete("client_id")
45
+ redirect client.authorize_url(params)
46
+ end
47
+
48
+ def raw_info
49
+ # step 2: get userid via code and access_token
50
+ @code ||= access_token[:code]
51
+
52
+ # step 3: get user info via userid
53
+ @uid ||= begin
54
+ access_token.options[:mode] = :query
55
+ response = access_token.get('/cgi-bin/user/getuserinfo', :params => {'code' => @code}, parse: :json)
56
+ response.parsed['UserId']
57
+ end
58
+
59
+ @raw_info ||= begin
60
+ access_token.options[:mode] = :query
61
+ response = access_token.get("/cgi-bin/user/get", :params => {"userid" => @uid}, parse: :json)
62
+ response.parsed
63
+ end
64
+ end
65
+
66
+ protected
67
+ def build_access_token
68
+ # step 0: wechat respond code
69
+ code = request.params['code']
70
+
71
+ # step 1: get access token
72
+ params = {
73
+ 'corpid' => client.id,
74
+ 'corpsecret' => client.secret,
75
+ }.merge(token_params.to_hash(symbolize_keys: true))
76
+ client.get_token(params, deep_symbolize(options.auth_token_params.merge({code: code})))
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,2 @@
1
+ require "omniauth/strategies/wechat"
2
+ require "omniauth/strategies/wechat_qiye"
metadata CHANGED
@@ -1,60 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-wechat-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - Skinnyworm
8
- autorequire:
7
+ - Alex Hu
8
+ - Never Min
9
+ - Eric Guo
10
+ autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2014-03-27 00:00:00.000000000 Z
13
+ date: 2020-06-11 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: omniauth
15
17
  requirement: !ruby/object:Gem::Requirement
16
18
  requirements:
17
- - - ~>
19
+ - - ">="
18
20
  - !ruby/object:Gem::Version
19
- version: '1.0'
21
+ version: 1.3.2
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
23
25
  requirements:
24
- - - ~>
26
+ - - ">="
25
27
  - !ruby/object:Gem::Version
26
- version: '1.0'
28
+ version: 1.3.2
27
29
  - !ruby/object:Gem::Dependency
28
30
  name: omniauth-oauth2
29
31
  requirement: !ruby/object:Gem::Requirement
30
32
  requirements:
31
- - - ~>
33
+ - - ">="
32
34
  - !ruby/object:Gem::Version
33
- version: '1.0'
35
+ version: 1.1.1
34
36
  type: :runtime
35
37
  prerelease: false
36
38
  version_requirements: !ruby/object:Gem::Requirement
37
39
  requirements:
38
- - - ~>
40
+ - - ">="
39
41
  - !ruby/object:Gem::Version
40
- version: '1.0'
42
+ version: 1.1.1
41
43
  - !ruby/object:Gem::Dependency
42
44
  name: rspec
43
45
  requirement: !ruby/object:Gem::Requirement
44
46
  requirements:
45
- - - ~>
47
+ - - "~>"
46
48
  - !ruby/object:Gem::Version
47
- version: '2.7'
49
+ version: '2.99'
48
50
  type: :development
49
51
  prerelease: false
50
52
  version_requirements: !ruby/object:Gem::Requirement
51
53
  requirements:
52
- - - ~>
54
+ - - "~>"
53
55
  - !ruby/object:Gem::Version
54
- version: '2.7'
56
+ version: '2.99'
55
57
  description: Using OAuth2 to authenticate wechat user when web resources being viewed
56
58
  within wechat(weixin) client.
57
- email: askinnyworm@gmail.com
59
+ email:
60
+ - askinnyworm@gmail.com
61
+ - Never.Min@gmail.com
62
+ - eric@cloud-mes.com
58
63
  executables: []
59
64
  extensions: []
60
65
  extra_rdoc_files: []
@@ -62,28 +67,30 @@ files:
62
67
  - README.md
63
68
  - lib/omniauth-wechat-oauth2.rb
64
69
  - lib/omniauth/strategies/wechat.rb
65
- homepage: https://github.com/skinnyworm/omniauth-wechat-oauth2
66
- licenses: []
70
+ - lib/omniauth/strategies/wechat_qiye.rb
71
+ - lib/omniauth/wechat.rb
72
+ homepage: https://github.com/nevermin/omniauth-wechat-oauth2
73
+ licenses:
74
+ - MIT
67
75
  metadata: {}
68
- post_install_message:
76
+ post_install_message:
69
77
  rdoc_options: []
70
78
  require_paths:
71
79
  - lib
72
80
  required_ruby_version: !ruby/object:Gem::Requirement
73
81
  requirements:
74
- - - '>='
82
+ - - ">="
75
83
  - !ruby/object:Gem::Version
76
- version: 1.9.3
84
+ version: 2.4.0
77
85
  required_rubygems_version: !ruby/object:Gem::Requirement
78
86
  requirements:
79
- - - '>='
87
+ - - ">="
80
88
  - !ruby/object:Gem::Version
81
- version: 1.8.23
89
+ version: '0'
82
90
  requirements:
83
91
  - none
84
- rubyforge_project:
85
- rubygems_version: 2.2.1
86
- signing_key:
92
+ rubygems_version: 3.1.4
93
+ signing_key:
87
94
  specification_version: 4
88
95
  summary: Omniauth strategy for wechat(weixin)
89
96
  test_files: []