omniauth-mpwechat-oauth2 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +86 -0
- data/lib/omniauth-mpwechat-oauth2.rb +2 -0
- data/lib/omniauth/strategies/mpwechat.rb +70 -0
- data/lib/omniauth/strategies/wechat_qiye.rb +80 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a0bf3fe03a3878b0e865b4e2e9668ea732af7129
|
4
|
+
data.tar.gz: 2f7fddf3760cba167ca3b50e083371cba412b8a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7bb9ddf432c3f78355cac059a78d829c2d2fd8db3c54242a9cd02e0f9bc8c5674d89a9fd043d8bf70669de00099fdcde863043b140dcd3629e7e09c1b3dfa01a
|
7
|
+
data.tar.gz: 9a8d940852b4fdc5c2348e0a4765c0223036e86769f2f1f5b9c9c10cccea805ffdb549a437b67b65a1a7e1e014b8ab65c42abc0ba1effb463f767cf6eb6b0ce5
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
Omniauth-mpwechat-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-mpwechat-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/mpwechat`
|
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 Mpwechat < OmniAuth::Strategies::OAuth2
|
6
|
+
option :name, "mpwechat"
|
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
|
+
response = access_token.get("/sns/userinfo", :params => {"openid" => @uid}, parse: :text)
|
50
|
+
@raw_info = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
|
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,80 @@
|
|
1
|
+
require "omniauth-oauth2"
|
2
|
+
|
3
|
+
module OmniAuth
|
4
|
+
module Strategies
|
5
|
+
class WechatQiye < OmniAuth::Strategies::OAuth2
|
6
|
+
option :name, "wechat_qiye"
|
7
|
+
|
8
|
+
option :client_options, {
|
9
|
+
:site => "https://qyapi.weixin.qq.com",
|
10
|
+
authorize_url: "https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect",
|
11
|
+
token_url: "/cgi-bin/gettoken",
|
12
|
+
token_method: :get,
|
13
|
+
connection_opts: {
|
14
|
+
ssl: { verify: false }
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
option :authorize_params, {scope: "snsapi_userinfo"}
|
19
|
+
option :token_params, {parse: :json}
|
20
|
+
|
21
|
+
uid do
|
22
|
+
raw_info['userid']
|
23
|
+
end
|
24
|
+
|
25
|
+
info do
|
26
|
+
{
|
27
|
+
userid: raw_info['userid'],
|
28
|
+
name: raw_info['name'],
|
29
|
+
department: raw_info['department'],
|
30
|
+
gender: raw_info['gender'],
|
31
|
+
weixinid: raw_info['weixinid'],
|
32
|
+
avatar: raw_info['avatar'],
|
33
|
+
status: raw_info['status'],
|
34
|
+
extattr: raw_info['extattr']
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
extra do
|
39
|
+
{ raw_info: raw_info }
|
40
|
+
end
|
41
|
+
|
42
|
+
def request_phase
|
43
|
+
params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
|
44
|
+
params["appid"] = params.delete("client_id")
|
45
|
+
redirect client.authorize_url(params)
|
46
|
+
end
|
47
|
+
|
48
|
+
def raw_info
|
49
|
+
# step 2: get userid via code and access_token
|
50
|
+
@code ||= access_token[:code]
|
51
|
+
|
52
|
+
# step 3: get user info via userid
|
53
|
+
@uid ||= begin
|
54
|
+
access_token.options[:mode] = :query
|
55
|
+
response = access_token.get('/cgi-bin/user/getuserinfo', :params => {'code' => @code}, parse: :json)
|
56
|
+
response.parsed['UserId']
|
57
|
+
end
|
58
|
+
|
59
|
+
@raw_info ||= begin
|
60
|
+
access_token.options[:mode] = :query
|
61
|
+
response = access_token.get("/cgi-bin/user/get", :params => {"userid" => @uid}, parse: :json)
|
62
|
+
response.parsed
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
def build_access_token
|
68
|
+
# step 0: wechat respond code
|
69
|
+
code = request.params['code']
|
70
|
+
|
71
|
+
# step 1: get access token
|
72
|
+
params = {
|
73
|
+
'corpid' => client.id,
|
74
|
+
'corpsecret' => client.secret,
|
75
|
+
}.merge(token_params.to_hash(symbolize_keys: true))
|
76
|
+
client.get_token(params, deep_symbolize(options.auth_token_params.merge({code: code})))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-mpwechat-oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Skinnyworm
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-18 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-mpwechat-oauth2.rb
|
64
|
+
- lib/omniauth/strategies/mpwechat.rb
|
65
|
+
- lib/omniauth/strategies/wechat_qiye.rb
|
66
|
+
homepage: https://github.com/aisliuyifei/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: 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.2.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: Omniauth strategy for mp wechat(weixin)
|
90
|
+
test_files: []
|