wechat-omniauth-web 0.0.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
+ SHA1:
3
+ metadata.gz: 793179f6a75c3701ec60d1d286f7affbafbefe80
4
+ data.tar.gz: 13ad991d27c81f377f6efd73b140f86a063fbaef
5
+ SHA512:
6
+ metadata.gz: b77c74e47c277796a01f67d60553894ef23eb2b859fab0d64dd89ef7c28fee951360078ed28a909a0c04704f4af9e00201a73a933f1b1cad356a44baa9fe178d
7
+ data.tar.gz: 58ef5cbb35d5d7072e4e109b1bd6a7d6e19e7a7ed0e4755ce060378dcf6ee2c57efce299997766af54fb539765027458d5dd0b3ad2aa345317ece5e18dbf4b32
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['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
+ callback_url = "#{callback_url}?auth_hash=#{params['auth_hash']}"
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
+ @raw_info = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :json).parsed
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 @@
1
+ require "omniauth/strategies/wechat"
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wechat-omniauth-web
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - teng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 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: login with wechat account by scanning qrcode with phone
56
+ email: congteng45@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - lib/omniauth/strategies/wechat.rb
63
+ - lib/wechat-omniauth-web.rb
64
+ homepage: https://github.com/tengcong/wechat-omniauth
65
+ licenses: []
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.9.3
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.8.23
81
+ requirements:
82
+ - none
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: login with wechat account by scanning qrcode with phone
88
+ test_files: []
89
+ has_rdoc: