bonio-omniauth-wechat-oauth2 0.1.1

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: 2ed8340e903e18f5f2372f573cac2ac742a2b34d
4
+ data.tar.gz: 394fb2e4864fa999267bc7b6a01e69e02895a3be
5
+ SHA512:
6
+ metadata.gz: 0a5e9efcf8144c889911680d29ecc052d90ec305bc3c0200b1e71d67f34d5efd8f37dda8e3995cf8b6c8975a1c153f5517bd6121ba039b733a4db23363fb82a0
7
+ data.tar.gz: 6e5f22bb547f42ea0fca2cfd232a940019f30dd0304d7c555e0b979493b78b9a0e680f52e00a805d487d1309879d63b4d77a90efec1376ab777facda0eaefaa5
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Omniauth::Openweixin::Oauth2
2
+
3
+ Omniauth Strategy for https://open.weixin.qq.com/
4
+
5
+ 微信开放平台的Omniauth Strategy
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem "omniauth-wechat-oauth2", git: 'git@github.com:yangsr/omniauth-wechat-oauth2.git'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
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
+ If you want to specify the callback url:
23
+ ```ruby
24
+ Rails.application.config.middleware.use OmniAuth::Builder do
25
+ provider :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"],
26
+ :authorize_params => {:redirect_uri => "http://www.example.com/auth/wechat/callback"}
27
+ end
28
+ ```
29
+ You can now access the OmniAuth Wechat OAuth2 URL: `/auth/wechat`
30
+
31
+ ## Credits
32
+
33
+ Skinnyworm, If you want the Omniauth Strategy for http://mp.weixin.qq.com, Click [here](https://github.com/skinnyworm/omniauth-wechat-oauth2)
34
+
35
+ ## Notice
36
+ There is some difference between https://open.weixin.qq.com/ and http://mp.weixin.qq.com, plz choose the appropriate gem to use.
37
+
38
+ 微信公众平台和微信开放平台的OAuth流程略有不同,请选择合适的gem使用。
39
+
40
+ ## About Unionid
41
+
42
+ UnionID机制,官方的解释是:
43
+
44
+ > 通过获取用户基本信息接口,开发者可通过OpenID来获取用户基本信息,而如果开发者拥有多个公众号,可使用以下办法通过UnionID机制来在多公众号之间进行用户帐号互通。只要是同一个微信开放平台帐号下的公众号,用户的UnionID是唯一的。换句话说,同一用户,对同一个微信开放平台帐号下的不同应用,UnionID是相同的。
45
+
46
+ 此前的OpenID机制,每个微信号对应每个公众号只有唯一的OpenID,所以不同微信公众号之间是不能共享用户的,现在有了UnionID就可以了。
@@ -0,0 +1 @@
1
+ require "omniauth/strategies/wechat"
@@ -0,0 +1,72 @@
1
+ require "omniauth-oauth2"
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Wechat < OmniAuth::Strategies::OAuth2
6
+ # Give your strategy a name.
7
+ option :name, "wechat"
8
+
9
+ # This is where you pass the options you would pass when
10
+ # initializing your consumer from the OAuth gem.
11
+ option :client_options, {
12
+ site: "https://api.weixin.qq.com",
13
+ authorize_url: "https://open.weixin.qq.com/connect/qrconnect#wechat_redirect",
14
+ token_url: "/sns/oauth2/access_token",
15
+ token_method: :get
16
+ }
17
+
18
+ option :authorize_params, {scope: "snsapi_login"}
19
+
20
+ option :token_params, {parse: :json}
21
+
22
+ # These are called after authentication has succeeded. If
23
+ # possible, you should try to set the UID without making
24
+ # additional calls (if the user id is returned with the token
25
+ # or as a URI parameter). This may not be possible with all
26
+ # providers.
27
+ uid { raw_info['openid'] }
28
+
29
+ info do
30
+ {
31
+ nickname: raw_info['nickname'],
32
+ sex: raw_info['sex'],
33
+ province: raw_info['province'],
34
+ city: raw_info['city'],
35
+ country: raw_info['country'],
36
+ headimgurl: raw_info['headimgurl'],
37
+ unionid: raw_info['unionid'],
38
+ }
39
+ end
40
+
41
+ extra do
42
+ {raw_info: raw_info}
43
+ end
44
+
45
+ def request_phase
46
+ params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
47
+ params["appid"] = params.delete("client_id")
48
+ redirect client.authorize_url(params)
49
+ end
50
+
51
+ def raw_info
52
+ @uid ||= access_token["openid"]
53
+ @raw_info ||= begin
54
+ access_token.options[:mode] = :query
55
+ response = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :text)
56
+ @raw_info = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
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
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bonio-omniauth-wechat-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Skinnyworm, yangsr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-09 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:
58
+ - askinnyworm@gmail.com, ysr1023@foxmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - README.md
64
+ - lib/omniauth-wechat-oauth2.rb
65
+ - lib/omniauth/strategies/wechat.rb
66
+ homepage: https://github.com/BonioTw/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: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.5
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Omniauth strategy for open.weixin.qq.com
89
+ test_files: []