omniauth-wx 0.1.1

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: ab139ac488db643d12839db06f8f9170ad2a5127ad20af2d30df24389c188390
4
+ data.tar.gz: 9d9eed3d570db4c95083c731adfbcff534ccebf71a0659b340ce8db1f11a04b7
5
+ SHA512:
6
+ metadata.gz: f85ea2aa8c47f452c2a0520c5e5bd5f39a2d6be66faee03d1480be929bd3f773874ca42440fed3cf01cab0627ba8ee2e3679aee60c04710ae188b30b5b381482
7
+ data.tar.gz: 100f7086449a99e65a0ed708d638c806228b610833d8990e5351cc53c9b5ddf08e680bc142439936cc546cc4825b848a366dc240cc0c0c8b53818bcfdf924b13
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,72 @@
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['openid']
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
+ def callback_url
38
+ full_host + script_name + callback_path + query_string
39
+ end
40
+ def request_phase
41
+ params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
42
+ params["appid"] = params.delete("client_id")
43
+ redirect client.authorize_url(params)
44
+ end
45
+
46
+ def raw_info
47
+ @uid ||= access_token["openid"]
48
+ @raw_info ||= begin
49
+ access_token.options[:mode] = :query
50
+ if access_token["scope"] == "snsapi_userinfo"
51
+ response = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :text)
52
+ @raw_info = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
53
+ else
54
+ @raw_info = {"openid" => @uid }
55
+ end
56
+ end
57
+ end
58
+
59
+ protected
60
+ def build_access_token
61
+ params = {
62
+ 'appid' => client.id,
63
+ 'secret' => client.secret,
64
+ 'code' => request.params['code'],
65
+ 'grant_type' => 'authorization_code'
66
+ }.merge(token_params.to_hash(symbolize_keys: true))
67
+ client.get_token(params, deep_symbolize(options.auth_token_params))
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1 @@
1
+ require "omniauth/strategies/wechat"
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-wx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Skinnyworm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-13 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
+ homepage: https://github.com/skinnyworm/omniauth-wechat-oauth2
66
+ licenses: []
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.9.3
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 1.8.23
82
+ requirements:
83
+ - none
84
+ rubygems_version: 3.0.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Omniauth strategy for wechat(weixin)
88
+ test_files: []