shixian-omniauth-wechat-oauth2 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 03a9288ec04e9a150a4bbc48d7396c02ba1a9ec6731f02a1ec070b4fc5170da9
4
+ data.tar.gz: 35e02686ca02b72e9b817c8789a36b92cd4fa219a3918ee7b6a1c73ada28cbf7
5
+ SHA512:
6
+ metadata.gz: f7bde7e94dcfa347d368651577ba23fa08bac07a2ec4d5d946d41471c34b878159680f533d2de6cfd6ec10bad490d47ab6cb44a644495f9cf702302d09673b4e
7
+ data.tar.gz: 3336a1ae018deaa95e73966598766ce95b324e0eea4170e2f3fcc5eb8737f3bf973593e26fbcfb8f3a55dd85eda7a0a7567996c3f87a4850eb60f8da1951ec9d
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ Omniauth-wechat-oauth2
2
+ ======================
3
+
4
+ [![Build Status](https://travis-ci.org/skinnyworm/omniauth-wechat-oauth2.svg)](https://travis-ci.org/skinnyworm/omniauth-wechat-oauth2) [![Gem Version](https://badge.fury.io/rb/omniauth-wechat-oauth2.png)](http://badge.fury.io/rb/omniauth-wechat-oauth2)
5
+
6
+ Wechat OAuth2 Strategy for OmniAuth 1.0.
7
+
8
+ You need to get a wechat API key at: http://mp.weixin.qq.com
9
+
10
+ Wechat oauth2 specification can be found at: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
11
+
12
+ ## Installation
13
+
14
+ Add to your `Gemfile`:
15
+
16
+ ```ruby
17
+ gem "omniauth-wechat-oauth2"
18
+ ```
19
+
20
+ Then `bundle install`.
21
+
22
+
23
+ ## Usage
24
+
25
+ Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
26
+
27
+ ```ruby
28
+ Rails.application.config.middleware.use OmniAuth::Builder do
29
+ provider :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"]
30
+ end
31
+ ```
32
+
33
+ You can now access the OmniAuth Wechat OAuth2 URL: `/auth/wechat`
34
+
35
+ ## Configuration
36
+
37
+ You can configure several options, which you pass in to the `provider` method via a hash:
38
+
39
+ * `scope`: Default is "snsapi_userinfo". It can either be *snsapi_base* or *snsapi_userinfo*. When scope is "snsapi_userinfo", after wechat user is authenticated, app can query userinfo using the acquired access_token.
40
+
41
+ For devise user, you can set up scope in your devise.rb as following.
42
+
43
+ ```ruby
44
+ config.omniauth :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"],
45
+ :authorize_params => {:scope => "snsapi_base"}
46
+ ```
47
+
48
+ ## Auth Hash
49
+
50
+ Here's an example of an authentication hash available in the callback by accessing `request.env["omniauth.auth"]`:
51
+
52
+ ```ruby
53
+ {
54
+ :provider => "wechat",
55
+ :uid => "123456789",
56
+ :info => {
57
+ nickname: "Nickname",
58
+ sex: 1,
59
+ province: "Changning",
60
+ city: "Shanghai",
61
+ country: "China",
62
+ headimgurl: "http://image_url"
63
+ },
64
+ :credentials => {
65
+ :token => "token",
66
+ :refresh_token => "another_token",
67
+ :expires_at => 7200,
68
+ :expires => true
69
+ },
70
+ :extra => {
71
+ :raw_info => {
72
+ openid: "openid"
73
+ nickname: "Nickname",
74
+ sex: 1,
75
+ province: "Changning",
76
+ city: "Shanghai",
77
+ country: "China",
78
+ headimgurl: "http://image_url"
79
+ }
80
+ }
81
+ }
82
+ ```
83
+
84
+
85
+
86
+
@@ -0,0 +1,70 @@
1
+ require "omniauth-oauth2"
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Wechat < OmniAuth::Strategies::OAuth2
6
+ option :name, "wechat"
7
+
8
+ option :client_options, {
9
+ site: "https://api.weixin.qq.com",
10
+ authorize_url: "https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect",
11
+ token_url: "/sns/oauth2/access_token",
12
+ token_method: :get
13
+ }
14
+
15
+ option :authorize_params, {scope: "snsapi_userinfo"}
16
+
17
+ option :token_params, {parse: :json}
18
+
19
+ uid do
20
+ raw_info['unionid']
21
+ end
22
+
23
+ info do
24
+ {
25
+ nickname: raw_info['nickname'],
26
+ sex: raw_info['sex'],
27
+ province: raw_info['province'],
28
+ city: raw_info['city'],
29
+ country: raw_info['country'],
30
+ headimgurl: raw_info['headimgurl']
31
+ }
32
+ end
33
+
34
+ extra do
35
+ {raw_info: raw_info}
36
+ end
37
+
38
+ def request_phase
39
+ params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
40
+ params["appid"] = params.delete("client_id")
41
+ redirect client.authorize_url(params)
42
+ end
43
+
44
+ def raw_info
45
+ @uid ||= access_token["openid"]
46
+ @raw_info ||= begin
47
+ access_token.options[:mode] = :query
48
+ if access_token["scope"] == "snsapi_userinfo"
49
+ response = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :text)
50
+ @raw_info = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
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,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 ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shixian-omniauth-wechat-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Skinnyworm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-07-05 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
+ description: Using OAuth2 to authenticate wechat user when web resources being viewed
56
+ within wechat(weixin) client.
57
+ email: askinnyworm@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/omniauth-wechat-oauth2.rb
64
+ - lib/omniauth/strategies/wechat.rb
65
+ - lib/omniauth/strategies/wechat_qiye.rb
66
+ homepage: https://github.com/shixiancom/omniauth-wechat-oauth2
67
+ licenses: []
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.9.3
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.23
83
+ requirements:
84
+ - none
85
+ rubygems_version: 3.5.18
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Omniauth strategy for wechat(weixin)
89
+ test_files: []