omniauth-dingtalk-oauth2 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7cc2eaabeef1b8731ca13cdf7987e0e8a5cd02c9e1cc2510d5e4c9e6e476c296
4
+ data.tar.gz: 9ef3f856e17fe00e03eed568d7662f24c780e4fc2a779b162f68d2076721a46a
5
+ SHA512:
6
+ metadata.gz: 0f0e2d01e7a12560d9897adef1acb348cd7bc1fb021425547fe88efa9c2582f7b7e41e566110612eb0dc43f329dce9082ac0fa789b9e35d10f9022af16928e33
7
+ data.tar.gz: c97739e9f2def1ecac2e2be28b1e32443cf8a01d4786994651158a205a67b893fb9c7316e01e617ecd97b58dcd83d94a60e4c7080628a1fd90d803cb354eb014
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-dingtalk-oauth2 (1.0.0)
5
+ omniauth-oauth2
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (11.1.3)
11
+ diff-lcs (1.4.4)
12
+ faraday (1.8.0)
13
+ faraday-em_http (~> 1.0)
14
+ faraday-em_synchrony (~> 1.0)
15
+ faraday-excon (~> 1.1)
16
+ faraday-httpclient (~> 1.0.1)
17
+ faraday-net_http (~> 1.0)
18
+ faraday-net_http_persistent (~> 1.1)
19
+ faraday-patron (~> 1.0)
20
+ faraday-rack (~> 1.0)
21
+ multipart-post (>= 1.2, < 3)
22
+ ruby2_keywords (>= 0.0.4)
23
+ faraday-em_http (1.0.0)
24
+ faraday-em_synchrony (1.0.0)
25
+ faraday-excon (1.1.0)
26
+ faraday-httpclient (1.0.1)
27
+ faraday-net_http (1.0.1)
28
+ faraday-net_http_persistent (1.2.0)
29
+ faraday-patron (1.0.0)
30
+ faraday-rack (1.0.0)
31
+ hashie (4.1.0)
32
+ jwt (2.3.0)
33
+ multi_json (1.15.0)
34
+ multi_xml (0.6.0)
35
+ multipart-post (2.1.1)
36
+ oauth2 (1.4.7)
37
+ faraday (>= 0.8, < 2.0)
38
+ jwt (>= 1.0, < 3.0)
39
+ multi_json (~> 1.3)
40
+ multi_xml (~> 0.5)
41
+ rack (>= 1.2, < 3)
42
+ omniauth (2.0.4)
43
+ hashie (>= 3.4.6)
44
+ rack (>= 1.6.2, < 3)
45
+ rack-protection
46
+ omniauth-oauth2 (1.7.2)
47
+ oauth2 (~> 1.4)
48
+ omniauth (>= 1.9, < 3)
49
+ rack (2.2.3)
50
+ rack-protection (2.1.0)
51
+ rack
52
+ rake (12.3.3)
53
+ rspec (3.10.0)
54
+ rspec-core (~> 3.10.0)
55
+ rspec-expectations (~> 3.10.0)
56
+ rspec-mocks (~> 3.10.0)
57
+ rspec-core (3.10.1)
58
+ rspec-support (~> 3.10.0)
59
+ rspec-expectations (3.10.1)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.10.0)
62
+ rspec-mocks (3.10.2)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.10.0)
65
+ rspec-support (3.10.2)
66
+ ruby2_keywords (0.0.5)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ byebug
73
+ omniauth-dingtalk-oauth2!
74
+ rake (~> 12.0)
75
+ rspec (~> 3.0)
76
+
77
+ BUNDLED WITH
78
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 jimcheung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Omniauth DingTalk
2
+
3
+ This project forked from [https://github.com/jinhucheung/omniauth-dingding](https://github.com/jinhucheung/omniauth-dingding).
4
+
5
+ To use it, you'll need to sign up for an OAuth2 Application ID and Secret on the [DingTalk Applications Page](https://open-dev.dingtalk.com/).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'omniauth-dingtalk-oauth2'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle install
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ `OmniAuth::Strategies::Dingtalk` is simply a Rack middleware. Read the OmniAuth docs for detailed instructions: https://github.com/intridea/omniauth.
24
+
25
+ Here's a quick example, 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 :dingtalk, ENV['DINGTALK_APP_ID'], ENV['DINGTALK_APP_SECRET']
30
+ end
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/gitlab-jh/jh-team/omniauth-dingtalk.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "omniauth/dingtalk"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+ set -vx
6
+
7
+ bundle install
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-dingtalk/client'
4
+
5
+ module OmniAuth
6
+ module Strategies
7
+ class Dingtalk < OmniAuth::Strategies::OAuth2
8
+ option :name, 'dingtalk'
9
+
10
+ option :client_options, {
11
+ site: 'https://oapi.dingtalk.com',
12
+ # one of qrcode, account, default is qrcode
13
+ authorize_method: :qrcode
14
+ }
15
+
16
+ # one of enterprise_internal, third_party_personal, default is enterprise_internal
17
+ option :client_type, :enterprise_internal
18
+
19
+ # one of snsapi_login, snsapi_auth, default is snsapi_login
20
+ option :authorize_params, scope: 'snsapi_login'
21
+
22
+ uid do
23
+ user_info['openid']
24
+ end
25
+
26
+ info do
27
+ {
28
+ unionid: user_info['unionid'],
29
+ ding_id: user_info['dingId'],
30
+ name: user_info['nick'],
31
+ username: user_info['nick']
32
+ }.merge(user_info)
33
+ end
34
+
35
+ extra do
36
+ { raw_info: user_info }
37
+ end
38
+
39
+ def request_phase
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
+ protected
46
+
47
+ def client
48
+ ::OmniAuth::Dingtalk::Client.get(options.client_type).new(
49
+ options.client_id,
50
+ options.client_secret,
51
+ deep_symbolize(options.client_options)
52
+ )
53
+ end
54
+
55
+ def build_access_token
56
+ verifier = request.params['code']
57
+ access_token_options = { redirect_uri: callback_url }.merge(token_params.to_hash(symbolize_keys: true))
58
+ client.auth_code.get_token(verifier, access_token_options, deep_symbolize(options.auth_token_params))
59
+ end
60
+
61
+ def user_info
62
+ @user_info ||= client.get_user_info(code: request.params['code'], access_token: access_token.token)
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ OmniAuth.config.add_camelization 'dingtalk', 'Dingtalk'
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'cgi'
6
+ require 'json'
7
+
8
+ module OmniAuth
9
+ module Dingtalk
10
+ module Client
11
+ class Base < ::OAuth2::Client
12
+ AUTHORIZE_URL = {
13
+ 'qrcode' => '/connect/qrconnect',
14
+ 'account' => '/connect/oauth2/sns_authorize'
15
+ }.freeze
16
+
17
+ GET_USER_INFO_BY_CODE_URL = '/sns/getuserinfo_bycode'
18
+ GET_USER_ID_BY_UNIONID_URL = '/topapi/user/getbyunionid'
19
+ GET_USER_INFO_BY_ID_URL = '/topapi/v2/user/get'
20
+
21
+ def initialize(client_id, client_secret, options = {}, &block)
22
+ opts = {
23
+ authorize_url: AUTHORIZE_URL.fetch(options[:authorize_method].to_s, AUTHORIZE_URL['qrcode']),
24
+ token_url: token_url,
25
+ token_method: :get
26
+ }.merge(options)
27
+
28
+ super(client_id, client_secret, opts, &block)
29
+ end
30
+
31
+ def get_user_info_by_code(code)
32
+ t = (Time.now.to_f * 1000).to_i.to_s
33
+ raw_sign = Base64.encode64(OpenSSL::HMAC.digest('SHA256', secret, t)).strip
34
+ sign = CGI.escape(raw_sign)
35
+
36
+ url = "#{GET_USER_INFO_BY_CODE_URL}?accessKey=#{id}&timestamp=#{t}&signature=#{sign}"
37
+
38
+ request(:post, url,
39
+ headers: { 'Content-Type' => 'application/json' },
40
+ body: { tmp_auth_code: code }.to_json
41
+ ).parsed
42
+ end
43
+
44
+ def get_user_id_by_unionid(access_token, unionid)
45
+ request(:post, GET_USER_ID_BY_UNIONID_URL,
46
+ headers: { 'Content-Type' => 'application/json' },
47
+ body: { unionid: unionid }.to_json,
48
+ params: { access_token: access_token }
49
+ ).parsed
50
+ end
51
+
52
+ def get_user_info_by_id(access_token, id)
53
+ request(:post, GET_USER_INFO_BY_ID_URL,
54
+ headers: { 'Content-Type' => 'application/json' },
55
+ body: { userid: id }.to_json,
56
+ params: { access_token: access_token }
57
+ ).parsed
58
+ end
59
+
60
+ def get_user_info(params = {})
61
+ raise NotImplementedError
62
+ end
63
+
64
+ def token_url
65
+ self.class.const_get(:TOKEN_URL) rescue nil
66
+ end
67
+
68
+ def token_params
69
+ { appid: id, appsecret: secret }
70
+ end
71
+
72
+ def get_token(params, access_token_opts = {}, extract_access_token = options[:extract_access_token])
73
+ super(token_params.merge(params), access_token_opts, extract_access_token)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-dingtalk/client/base'
4
+
5
+ module OmniAuth
6
+ module Dingtalk
7
+ module Client
8
+ class EnterpriseInternal < ::OmniAuth::Dingtalk::Client::Base
9
+ TOKEN_URL = '/gettoken'
10
+
11
+ def token_params
12
+ { appkey: id, appsecret: secret }
13
+ end
14
+
15
+ def get_user_info(params = {})
16
+ resp = get_user_info_by_code(params[:code])
17
+ user_info = resp['user_info'] || {}
18
+ return user_info if user_info['unionid'].to_s.empty?
19
+
20
+ user_id = get_user_id_by_unionid(params[:access_token], user_info['unionid']).dig('result', 'userid')
21
+ return user_info if user_id.to_s.empty?
22
+
23
+ result = get_user_info_by_id(params[:access_token], user_id)['result']
24
+ user_info.merge(result || {})
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-dingtalk/client/base'
4
+
5
+ module OmniAuth
6
+ module Dingtalk
7
+ module Client
8
+ class ThirdPartyPersonal < ::OmniAuth::Dingtalk::Client::Base
9
+ TOKEN_URL = '/sns/gettoken'
10
+
11
+ def get_user_info(params = {})
12
+ resp = get_user_info_by_code(params[:code])
13
+ resp['user_info'] || {}
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-dingtalk/client/third_party_personal'
4
+ require 'omniauth-dingtalk/client/enterprise_internal'
5
+
6
+ module OmniAuth
7
+ module Dingtalk
8
+ module Client
9
+ def self.get(client_type)
10
+ case client_type.to_s
11
+ when 'third_party_personal'
12
+ ::OmniAuth::Dingtalk::Client::ThirdPartyPersonal
13
+ else
14
+ ::OmniAuth::Dingtalk::Client::EnterpriseInternal
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module Dingtalk
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-oauth2'
4
+ require 'omniauth-dingtalk/version'
5
+ require 'omniauth/strategies/dingtalk'
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/omniauth-dingtalk/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "omniauth-dingtalk-oauth2"
5
+ spec.version = OmniAuth::Dingtalk::VERSION
6
+ spec.authors = ["JiHu(GitLab)"]
7
+ spec.email = ["dev@jihulab.com"]
8
+
9
+ spec.summary = %q{Omniauth strategy for DingTalk}
10
+ spec.description = %q{Wrapper the DingTalk Oauth2 API}
11
+ spec.homepage = "https://gitlab.com/gitlab-jh/jh-team/omniauth-dingtalk"
12
+ spec.license = "MIT"
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://gitlab.com/gitlab-jh/jh-team/omniauth-dingtalk"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'omniauth-oauth2', '~> 1.7.1'
27
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-dingtalk-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - JiHu(GitLab)
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.1
27
+ description: Wrapper the DingTalk Oauth2 API
28
+ email:
29
+ - dev@jihulab.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/omniauth-dingtalk-oauth2.rb
44
+ - lib/omniauth-dingtalk/client.rb
45
+ - lib/omniauth-dingtalk/client/base.rb
46
+ - lib/omniauth-dingtalk/client/enterprise_internal.rb
47
+ - lib/omniauth-dingtalk/client/third_party_personal.rb
48
+ - lib/omniauth-dingtalk/version.rb
49
+ - lib/omniauth/strategies/dingtalk.rb
50
+ - omniauth-dingtalk-oauth2.gemspec
51
+ homepage: https://gitlab.com/gitlab-jh/jh-team/omniauth-dingtalk
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://gitlab.com/gitlab-jh/jh-team/omniauth-dingtalk
56
+ source_code_uri: https://gitlab.com/gitlab-jh/jh-team/omniauth-dingtalk
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.4
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Omniauth strategy for DingTalk
76
+ test_files: []