omniauth-wechat-oauth2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ce6f400f236ade461b6de6f0475e29d6efa2663
4
+ data.tar.gz: 7406fddc873587f98f976085265bccee6c450d4c
5
+ SHA512:
6
+ metadata.gz: 324edff8a03b06a2eec788c5a612efef7ca2099be123fe721f6d77995a94a59f9a35e2adc5a52599d21cd1a00d880a5e7522155d1fa447e0cdecd67c91f3645e
7
+ data.tar.gz: a2cdacfde5eb3c2f0d87a7a5f30bde685315d242f6dbc5c9247a43a42502b60996b2d7702adb4031f9f8f62fc4564181afa391977a46a31b38479f8004bb6dea
@@ -0,0 +1,84 @@
1
+ Omniauth-wechat-oauth2
2
+ ======================
3
+
4
+ Wechat OAuth2 Strategy for OmniAuth 1.0.
5
+
6
+ You need to get a wechat API key at: http://mp.weixin.qq.com
7
+
8
+ Wechat oauth2 specification can be found at: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
9
+
10
+ ## Installation
11
+
12
+ Add to your `Gemfile`:
13
+
14
+ ```ruby
15
+ gem "omniauth-wechat-oauth2"
16
+ ```
17
+
18
+ Then `bundle install`.
19
+
20
+
21
+ ## Usage
22
+
23
+ Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
24
+
25
+ ```ruby
26
+ Rails.application.config.middleware.use OmniAuth::Builder do
27
+ provider :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"]
28
+ end
29
+ ```
30
+
31
+ You can now access the OmniAuth Wechat OAuth2 URL: `/auth/wechat`
32
+
33
+ ## Configuration
34
+
35
+ You can configure several options, which you pass in to the `provider` method via a hash:
36
+
37
+ * `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.
38
+
39
+ For devise user, you can set up scope in your devise.rb as following.
40
+
41
+ ```ruby
42
+ config.omniauth :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"],
43
+ :authorize_params => {:scope => "snsapi_base"}
44
+ ```
45
+
46
+ ## Auth Hash
47
+
48
+ Here's an example of an authentication hash available in the callback by accessing `request.env["omniauth.auth"]`:
49
+
50
+ ```ruby
51
+ {
52
+ :provider => "wechat",
53
+ :uid => "123456789",
54
+ :info => {
55
+ nickname: "Nickname",
56
+ sex: 1,
57
+ province: "Changning",
58
+ city: "Shanghai",
59
+ country: "China",
60
+ headimgurl: "http://image_url"
61
+ },
62
+ :credentials => {
63
+ :token => "token",
64
+ :refresh_token => "another_token",
65
+ :expires_at => 7200,
66
+ :expires => true
67
+ },
68
+ :extra => {
69
+ :raw_info => {
70
+ openid: "openid"
71
+ nickname: "Nickname",
72
+ sex: 1,
73
+ province: "Changning",
74
+ city: "Shanghai",
75
+ country: "China",
76
+ headimgurl: "http://image_url"
77
+ }
78
+ }
79
+ }
80
+ ```
81
+
82
+
83
+
84
+
@@ -0,0 +1 @@
1
+ require "omniauth/strategies/wechat"
@@ -0,0 +1,69 @@
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
+
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
+ @raw_info = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :json).parsed
50
+ else
51
+ @raw_info = {"openid" => @uid }
52
+ end
53
+ end
54
+ end
55
+
56
+ protected
57
+ def build_access_token
58
+ params = {
59
+ 'appid' => client.id,
60
+ 'secret' => client.secret,
61
+ 'code' => request.params['code'],
62
+ 'grant_type' => 'authorization_code'
63
+ }.merge(token_params.to_hash(symbolize_keys: true))
64
+ client.get_token(params, deep_symbolize(options.auth_token_params))
65
+ end
66
+
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 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: 2014-03-27 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
+ rubyforge_project:
85
+ rubygems_version: 2.2.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Omniauth strategy for wechat(weixin)
89
+ test_files: []