oauth2_dingtalk 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3d012bf98b1dc7ca8cbbf7c43f1869415754d1a
4
- data.tar.gz: 1fb0e703a6a8187d52c3d246f8a80041ed5c909e
3
+ metadata.gz: f094f2ae0851b8d3ef012277cd7b19604caa24f5
4
+ data.tar.gz: 554102e2502536878ac3bbecd23ff668f5a41776
5
5
  SHA512:
6
- metadata.gz: 5aeaf74e1bb4c37b155d9c8444a2eb9f57d4ba13076c2e1648fdf2b38b4d770cccc1c1645cd60d6d3d9930b5cb9f3246ebe547f786653170036061968d09379b
7
- data.tar.gz: dec0b555d7877dd764ca489b97d6a90487a65db673ba6e3a3a4fd022ca506195d30733f0bdb31f06a7927615c945165fa53b45529d2d9d31d3579fc5b26b8910
6
+ metadata.gz: 0df169d104797d520f06e6174a2fe58545c5564daa02805c1564e77672e94868ab85eba446954707c0f339fe2554dc41bb47e204541dd6b5e68d276c792ee441
7
+ data.tar.gz: a5dee3785d82e17a08228f202f53f28c7dc9382bbd1a1809e2a5f263e8b207c51a2719e0d6ff2a32813c276496c5d966d511541bc6a4e19e1ff67fd4af3c36fe
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --format documentation
1
+ --format progress
2
2
  --color
data/README.md CHANGED
@@ -1,36 +1,52 @@
1
- # Oauth2Dingtalk
1
+ [![Gem Version](https://badge.fury.io/rb/oauth2_dingtalk.svg)](https://badge.fury.io/rb/oauth2_dingtalk)
2
2
 
3
- Gem 的主要是使用钉钉扫码登录 Gitlab。
3
+ # Omniauth DingTalk Strategies
4
4
 
5
- ## Usage
5
+ Strategy to authenticate with DingTalk via OAuth2 in OmniAuth.
6
6
 
7
- 1. 和其他的 Oauth2 Gem 一样,在 Gemfile 里面添加:
8
- ```
9
- gem 'oauth2_dingtalk'
10
- ```
7
+ Get your API key at: http://open-dev.dingtalk.com/ Note the appId and the appSecret.
8
+
9
+ For more details, read the DingTalk docs: https://open-doc.dingtalk.com/docs/doc.htm?spm=0.0.0.0.oVQWJc&treeId=168&articleId=104878&docType=1
11
10
 
12
- 2. 然后在`config/initializers`里添加文件`dingding.rb`:
11
+ ## Installation
12
+
13
+ Add to your `Gemfile`:
13
14
 
14
15
  ```ruby
15
- # config/initializers/dingding.rb
16
- Rails.application.config.middleware.use OmniAuth::Builder do
17
- provider :dingding, "Your_OAuth_App_ID", "Your_OAuth_App_Secret"
18
- end
16
+ gem 'oauth2_dingtalk'
19
17
  ```
20
18
 
21
- 3. 然后可以看 example 文件里面的例子,如果是在 Rails 项目里面使用的话,可以在路由那边添加:
19
+ Then `bundle install`.
20
+
21
+ ## appId & appSecret
22
+
23
+ Go to: https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.o96KrM&treeId=168&articleId=104882&docType=1
24
+
25
+ ## Usage
26
+
27
+ Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
22
28
 
23
29
  ```ruby
24
- get '/auth/:provider/callback', 'sessions#create'
30
+ Rails.application.config.middleware.use OmniAuth::Builder do
31
+ provider :dingding, "Your_OAuth_App_ID", "Your_OAuth_App_Secret"
32
+ end
25
33
  ```
26
34
 
27
- 或者其他的方式。
35
+ You can now access the OmniAuth DingTalk OAuth2 URL: /auth/dingding
28
36
 
29
- 4. 最主要的是
30
- ```
31
- auth = request.env["omniauth.auth"]
32
- auth["provider"] # dingding
33
- auth["uid"] # 用户在当前开放应用内的唯一标识
34
- ```
37
+ For more examples please check out example/config.ru
35
38
 
36
- 可参考:http://railscasts.com/episodes/241-simple-omniauth?autoplay=true
39
+ ## Auth Hash
40
+
41
+ Here's an example of an authentication hash available in the callback by accessing `request.env['omniauth.auth']`:
42
+
43
+ ```ruby
44
+ {
45
+ "provider": "dingding",
46
+ "uid": "uid",
47
+ "info": {
48
+ "name": "liukun",
49
+ "ding_id": "ding_id"
50
+ }
51
+ }
52
+ ```
@@ -1,3 +1,3 @@
1
1
  module Oauth2Dingtalk
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -10,17 +10,20 @@ module OmniAuth
10
10
  authorize_url: '/connect/qrconnect',
11
11
  token_url: '/sns/gettoken',
12
12
  persistent_url: '/sns/get_persistent_code',
13
+ sns_token: '/sns/get_sns_token',
14
+ user_info: '/sns/getuserinfo',
13
15
  token_method: :get
14
16
 
15
17
  option :authorize_params, scope: 'snsapi_login'
16
18
 
17
19
  uid do
18
- raw_info['openid']
20
+ raw_info['user_info']['unionid']
19
21
  end
20
22
 
21
23
  info do
22
24
  {
23
- unionid: raw_info['unionid']
25
+ name: raw_info['user_info']['nick'],
26
+ ding_id: raw_info['user_info']['dingId']
24
27
  }
25
28
  end
26
29
 
@@ -35,11 +38,9 @@ module OmniAuth
35
38
  end
36
39
 
37
40
  def raw_info
38
- @raw_info ||=
39
- access_token.post(options.client_options[:persistent_url] + "?access_token=#{access_token.token}") do |req|
40
- req.headers['Content-Type'] = 'application/json'
41
- req.body = "{\"tmp_auth_code\":\"#{request.params['code']}\"}"
42
- end.parsed
41
+ return persistent_code if persistent_code['errcode'] != 0
42
+ return sns_token if sns_token['errcode'] != 0
43
+ user_info
43
44
  end
44
45
 
45
46
  protected
@@ -51,6 +52,28 @@ module OmniAuth
51
52
  }
52
53
  client.get_token(params)
53
54
  end
55
+
56
+ def persistent_code
57
+ @persistent_code ||=
58
+ access_token.post(options.client_options.persistent_url + "?access_token=#{access_token.token}") do |req|
59
+ req.headers['Content-Type'] = 'application/json'
60
+ req.body = "{\"tmp_auth_code\":\"#{request.params['code']}\"}"
61
+ end.parsed
62
+ end
63
+
64
+ def sns_token
65
+ @sns_token ||=
66
+ access_token.post(options.client_options.sns_token + "?access_token=#{access_token.token}") do |req|
67
+ req.headers['Content-Type'] = 'application/json'
68
+ req.body = "{\"openid\":\"#{@persistent_code['openid']}\",
69
+ \"persistent_code\":\"#{@persistent_code['persistent_code']}\"}"
70
+ end.parsed
71
+ end
72
+
73
+ def user_info
74
+ @user_info ||=
75
+ access_token.get(options.client_options.user_info + "?sns_token=#{@sns_token['sns_token']}").parsed
76
+ end
54
77
  end
55
78
  end
56
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2_dingtalk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - liukun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-13 00:00:00.000000000 Z
11
+ date: 2017-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler