omniauth-dingding 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '08a27f7614372b4b0a77aa178407a7141320d25006b1c7a5ff7c8c84279fc53c'
4
+ data.tar.gz: b8e96667d34dc7309df94e884736ea201eb59b3f7ec7009d12a544b09c2c6c41
5
+ SHA512:
6
+ metadata.gz: 59b71c90db10c699872b6b161f2fb84b287dc18651b4eb10dd2fd8444211ab0b0705590222a2d242c2509ea6ec9acfc539afd95560095058fc412d60f85ab4b7
7
+ data.tar.gz: e74acddaac22ca4521d2485f1744e7ff2cbdfa91b8bc67134b076e27b0a8a2f1a50c469cc2c45511b7c78a13ac5a1281ba80de26d23251b56f83489c5aa9516b
@@ -0,0 +1,19 @@
1
+ name: Main
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ tags:
7
+ - '**'
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@master
13
+ - name: Set up Ruby
14
+ uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: 2.6
17
+ bundler-cache: true
18
+ - name: Run tests
19
+ run: bundle exec rspec
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,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in omniauth-dingding.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-dingding (0.1.0)
5
+ omniauth (~> 2.0)
6
+ omniauth-oauth2 (~> 1.7.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ byebug (11.1.3)
12
+ diff-lcs (1.4.4)
13
+ faraday (1.5.1)
14
+ faraday-em_http (~> 1.0)
15
+ faraday-em_synchrony (~> 1.0)
16
+ faraday-excon (~> 1.1)
17
+ faraday-httpclient (~> 1.0.1)
18
+ faraday-net_http (~> 1.0)
19
+ faraday-net_http_persistent (~> 1.1)
20
+ faraday-patron (~> 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
+ hashie (4.1.0)
31
+ jwt (2.2.3)
32
+ multi_json (1.15.0)
33
+ multi_xml (0.6.0)
34
+ multipart-post (2.1.1)
35
+ oauth2 (1.4.7)
36
+ faraday (>= 0.8, < 2.0)
37
+ jwt (>= 1.0, < 3.0)
38
+ multi_json (~> 1.3)
39
+ multi_xml (~> 0.5)
40
+ rack (>= 1.2, < 3)
41
+ omniauth (2.0.4)
42
+ hashie (>= 3.4.6)
43
+ rack (>= 1.6.2, < 3)
44
+ rack-protection
45
+ omniauth-oauth2 (1.7.1)
46
+ oauth2 (~> 1.4)
47
+ omniauth (>= 1.9, < 3)
48
+ rack (2.2.3)
49
+ rack-protection (2.1.0)
50
+ rack
51
+ rake (12.3.3)
52
+ rspec (3.10.0)
53
+ rspec-core (~> 3.10.0)
54
+ rspec-expectations (~> 3.10.0)
55
+ rspec-mocks (~> 3.10.0)
56
+ rspec-core (3.10.1)
57
+ rspec-support (~> 3.10.0)
58
+ rspec-expectations (3.10.1)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.10.0)
61
+ rspec-mocks (3.10.2)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.10.0)
64
+ rspec-support (3.10.2)
65
+ ruby2_keywords (0.0.5)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ byebug
72
+ omniauth-dingding!
73
+ rake (~> 12.0)
74
+ rspec (~> 3.0)
75
+
76
+ BUNDLED WITH
77
+ 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,46 @@
1
+ [![Gem Version](https://badge.fury.io/rb/omniauth-dingding.svg)](https://badge.fury.io/rb/omniauth-dingding)
2
+ [![Build Status](https://github.com/jinhucheung/omniauth-dingding/actions/workflows/main.yml/badge.svg)](https://github.com/jinhucheung/omniauth-dingding/actions)
3
+
4
+ # Omniauth Dingding
5
+
6
+ This is the official OmniAuth strategy for authenticating to DingTalk. 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/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'omniauth-dingding'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```
19
+ $ bundle install
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```
25
+ $ gem install omniauth-dingding
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ `OmniAuth::Strategies::Dingding` is simply a Rack middleware. Read the OmniAuth docs for detailed instructions: https://github.com/intridea/omniauth.
31
+
32
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
33
+
34
+ ```ruby
35
+ Rails.application.config.middleware.use OmniAuth::Builder do
36
+ provider :dingding, ENV['DINGDING_APP_ID'], ENV['DINGDING_APP_SECRET']
37
+ end
38
+ ```
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jinhucheung/omniauth-dingding.
43
+
44
+ ## License
45
+
46
+ 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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "omniauth/dingding"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ require 'omniauth-oauth2'
2
+ require 'omniauth-dingding/version'
3
+ require 'omniauth/strategies/dingding'
@@ -0,0 +1,19 @@
1
+ require 'omniauth-dingding/client/third_party_personal'
2
+ require 'omniauth-dingding/client/enterprise_internal'
3
+
4
+ module OmniAuth
5
+ module Dingding
6
+ module Client
7
+ class << self
8
+ def get(client_type)
9
+ case client_type.to_s
10
+ when 'third_party_personal'
11
+ ::OmniAuth::Dingding::Client::ThirdPartyPersonal
12
+ else
13
+ ::OmniAuth::Dingding::Client::EnterpriseInternal
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,78 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'cgi'
4
+ require 'json'
5
+
6
+ module OmniAuth
7
+ module Dingding
8
+ module Client
9
+ class Base < ::OAuth2::Client
10
+ AUTHORIZE_URL = {
11
+ 'qrcode' => '/connect/qrconnect',
12
+ 'account' => '/connect/oauth2/sns_authorize'
13
+ }.freeze
14
+
15
+ GET_USER_INFO_BY_CODE_URL = '/sns/getuserinfo_bycode'
16
+
17
+ GET_USER_ID_BY_UNIONID_URL = '/topapi/user/getbyunionid'
18
+
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,27 @@
1
+ require 'omniauth-dingding/client/base'
2
+
3
+ module OmniAuth
4
+ module Dingding
5
+ module Client
6
+ class EnterpriseInternal < ::OmniAuth::Dingding::Client::Base
7
+ TOKEN_URL = '/gettoken'
8
+
9
+ def token_params
10
+ { appkey: id, appsecret: secret }
11
+ end
12
+
13
+ def get_user_info(params = {})
14
+ resp = get_user_info_by_code(params[:code])
15
+ user_info = resp['user_info'] || {}
16
+ return user_info if user_info['unionid'].to_s.empty?
17
+
18
+ user_id = get_user_id_by_unionid(params[:access_token], user_info['unionid']).dig('result', 'userid')
19
+ return user_info if user_id.to_s.empty?
20
+
21
+ result = get_user_info_by_id(params[:access_token], user_id)['result']
22
+ user_info.merge(result || {})
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ require 'omniauth-dingding/client/base'
2
+
3
+ module OmniAuth
4
+ module Dingding
5
+ module Client
6
+ class ThirdPartyPersonal < ::OmniAuth::Dingding::Client::Base
7
+ TOKEN_URL = '/sns/gettoken'
8
+
9
+ def get_user_info(params = {})
10
+ resp = get_user_info_by_code(params[:code])
11
+ resp['user_info'] || {}
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Dingding
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,64 @@
1
+ require 'omniauth-dingding/client'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Dingding < OmniAuth::Strategies::OAuth2
6
+ option :name, 'dingding'
7
+
8
+ option :client_options, {
9
+ site: 'https://oapi.dingtalk.com',
10
+ # one of qrcode, account, default is qrcode
11
+ authorize_method: :qrcode
12
+ }
13
+
14
+ # one of enterprise_internal, third_party_personal, default is enterprise_internal
15
+ option :client_type, :enterprise_internal
16
+
17
+ # one of snsapi_login, snsapi_auth, default is snsapi_login
18
+ option :authorize_params, scope: 'snsapi_login'
19
+
20
+ uid do
21
+ user_info['openid']
22
+ end
23
+
24
+ info do
25
+ {
26
+ unionid: user_info['unionid'],
27
+ name: user_info['nick'],
28
+ ding_id: user_info['dingId']
29
+ }.merge(user_info)
30
+ end
31
+
32
+ extra do
33
+ { raw_info: user_info }
34
+ end
35
+
36
+ def request_phase
37
+ params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
38
+ params['appid'] = params.delete('client_id')
39
+ redirect client.authorize_url(params)
40
+ end
41
+
42
+ protected
43
+
44
+ def client
45
+ ::OmniAuth::Dingding::Client.get(options.client_type).new(
46
+ options.client_id,
47
+ options.client_secret,
48
+ deep_symbolize(options.client_options)
49
+ )
50
+ end
51
+
52
+ def build_access_token
53
+ verifier = request.params['code']
54
+ client.auth_code.get_token(verifier, { redirect_uri: callback_url }.merge(token_params.to_hash(symbolize_keys: true)), deep_symbolize(options.auth_token_params))
55
+ end
56
+
57
+ def user_info
58
+ @user_info ||= client.get_user_info(code: request.params['code'], access_token: access_token.token)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ OmniAuth.config.add_camelization 'dingding', 'Dingding'
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/omniauth-dingding/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "omniauth-dingding"
5
+ spec.version = OmniAuth::Dingding::VERSION
6
+ spec.authors = ["jimcheung"]
7
+ spec.email = ["hi.jinhu.zhang@gmail.com"]
8
+
9
+ spec.summary = %q{Omniauth strategy for DingTalk}
10
+ spec.description = %q{Wrapper the DingTalk Oauth2 API}
11
+ spec.homepage = "https://github.com/jinhucheung/omniauth-dingding"
12
+ spec.license = "MIT"
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/jinhucheung/omniauth-dingding"
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', '~> 2.0'
27
+ spec.add_dependency 'omniauth-oauth2', '~> 1.7.1'
28
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-dingding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jimcheung
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-01 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: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.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.7.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.1
41
+ description: Wrapper the DingTalk Oauth2 API
42
+ email:
43
+ - hi.jinhu.zhang@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/main.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/omniauth-dingding.rb
59
+ - lib/omniauth-dingding/client.rb
60
+ - lib/omniauth-dingding/client/base.rb
61
+ - lib/omniauth-dingding/client/enterprise_internal.rb
62
+ - lib/omniauth-dingding/client/third_party_personal.rb
63
+ - lib/omniauth-dingding/version.rb
64
+ - lib/omniauth/strategies/dingding.rb
65
+ - omniauth-dingding.gemspec
66
+ homepage: https://github.com/jinhucheung/omniauth-dingding
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/jinhucheung/omniauth-dingding
71
+ source_code_uri: https://github.com/jinhucheung/omniauth-dingding
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.0.8
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Omniauth strategy for DingTalk
91
+ test_files: []