omniauth_oauth2_wechat 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e02d34d591afe68ab8ba8e447663fe9d1860670
4
+ data.tar.gz: dbae0fc29ad7191730c45b0810a3add56f19c71c
5
+ SHA512:
6
+ metadata.gz: 969b17c4caf9ce2b59002c1ecb3010fc7c348f819e245ed4e764689865e9755e29f675f65211789c6d6ada16e0dc5e34e7bedbfac1c7a48677228209a58b4fe3
7
+ data.tar.gz: 9caa0f45b335dc0bcd050ab793f31edc0550248a7b63e8941ecd21b485ddae6ad93bcccc470d2dc7cad6bd8b325b0419ee59a5b87a925d330bb508999719031e
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ ## Installation
2
+
3
+ Add to your `Gemfile`:
4
+
5
+ ```ruby
6
+ gem "omniauth_oauth2_wechat"
7
+ ```
8
+
9
+ Then `bundle install`.
10
+
11
+
12
+ ## Usage
13
+
14
+ Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
15
+
16
+ ```ruby
17
+ Rails.application.config.middleware.use OmniAuth::Builder do
18
+ provider :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"]
19
+ end
20
+ ```
21
+
22
+ You can now access the OmniAuth Wechat OAuth2 URL: `/auth/wechat`
23
+
24
+ ## Configuration
25
+
26
+ You can configure several options, which you pass in to the `provider` method via a hash:
27
+
28
+ * `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.
29
+
30
+ For devise user, you can set up scope in your devise.rb as following.
31
+
32
+ ```ruby
33
+ config.omniauth :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"],
34
+ :authorize_params => {:scope => "snsapi_base"}
35
+ ```
36
+
37
+ ## Auth Hash
38
+
39
+ Here's an example of an authentication hash available in the callback by accessing `request.env["omniauth.auth"]`:
40
+
41
+ ```ruby
42
+ {
43
+ :provider => "wechat",
44
+ :uid => "123456789",
45
+ :info => {
46
+ nickname: "Nickname",
47
+ sex: 1,
48
+ province: "Changning",
49
+ city: "Shanghai",
50
+ country: "China",
51
+ headimgurl: "http://image_url"
52
+ },
53
+ :credentials => {
54
+ :token => "token",
55
+ :refresh_token => "another_token",
56
+ :expires_at => 7200,
57
+ :expires => true
58
+ },
59
+ :extra => {
60
+ :raw_info => {
61
+ openid: "openid"
62
+ nickname: "Nickname",
63
+ sex: 1,
64
+ province: "Changning",
65
+ city: "Shanghai",
66
+ country: "China",
67
+ headimgurl: "http://image_url"
68
+ }
69
+ }
70
+ }
71
+ ```
72
+
73
+
74
+
75
+
data/lib/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0
@@ -0,0 +1,73 @@
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
+ unionid: raw_info['unionid'],
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"] == "snsapi_userinfo"
50
+ response = access_token.get("/sns/userinfo", :params => {"openid" => @uid, "lang" => "zh_CN"}, parse: :text)
51
+ @raw_info = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
52
+ else
53
+ @raw_info = {"openid" => @uid }
54
+ @raw_info.merge!("unionid" => access_token["unionid"]) if access_token["unionid"]
55
+ @raw_info
56
+ end
57
+ end
58
+ end
59
+
60
+ protected
61
+ def build_access_token
62
+ params = {
63
+ 'appid' => client.id,
64
+ 'secret' => client.secret,
65
+ 'code' => request.params['code'],
66
+ 'grant_type' => 'authorization_code'
67
+ }.merge(token_params.to_hash(symbolize_keys: true))
68
+ client.get_token(params, deep_symbolize(options.auth_token_params))
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1 @@
1
+ require "omniauth/strategies/wechat"
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth_oauth2_wechat
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - sharp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-31 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: liuqiang_0701@163.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/VERSION
64
+ - lib/omniauth/strategies/wechat.rb
65
+ - lib/omniauth_oauth2_wechat.rb
66
+ homepage: https://github.com/sharp/omniauth_oauth2_wechat
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
+ rubyforge_project:
86
+ rubygems_version: 2.6.4
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Omniauth strategy for wechat(weixin)
90
+ test_files: []