Commit 24174c61 by mingyuan

saas

parent 0ebdd496
...@@ -7,7 +7,7 @@ GIT ...@@ -7,7 +7,7 @@ GIT
GIT GIT
remote: https://github.com/work-design/rails_com.git remote: https://github.com/work-design/rails_com.git
revision: 8949509c4f26ecf76adecfdbb2e221535ee47ac0 revision: 62d866862c729077c67329a0faf255ebaa49eba2
specs: specs:
rails_com (1.2.9) rails_com (1.2.9)
acme-client acme-client
...@@ -183,6 +183,8 @@ GEM ...@@ -183,6 +183,8 @@ GEM
nio4r (2.5.8) nio4r (2.5.8)
nokogiri (1.12.4-x86_64-darwin) nokogiri (1.12.4-x86_64-darwin)
racc (~> 1.4) racc (~> 1.4)
nokogiri (1.12.4-x86_64-linux)
racc (~> 1.4)
pg (1.2.3) pg (1.2.3)
puma (5.4.0) puma (5.4.0)
nio4r (~> 2.0) nio4r (~> 2.0)
...@@ -250,6 +252,7 @@ GEM ...@@ -250,6 +252,7 @@ GEM
PLATFORMS PLATFORMS
x86_64-darwin-20 x86_64-darwin-20
x86_64-linux
DEPENDENCIES DEPENDENCIES
amazing_print amazing_print
......
require 'net/http'
module Dingtalk::Api module Dingtalk::Api
class Saas < Base class Saas < Base
BASE = 'https://openplatform.dg-work.cn/' BASE = 'https://openplatform.dg-work.cn/'
def token def token
payload = { headers = sign_header('POST', '/gettoken.json')
appkey: app.app_key, r = @client.post 'gettoken.json', nil, headers: headers, base: BASE
appsecret: app.app_secret,
}
r = @client.post 'gettoken.json', payload.to_json, headers: { }, base: BASE
{ {
'access_token' => r.dig('content', 'data', 'accessToken'), 'access_token' => r.dig('content', 'data', 'accessToken'),
'expires_in' => r.dig('content', 'data', 'expiresIn') 'expires_in' => r.dig('content', 'data', 'expiresIn')
} }
end end
def sign_header def sign_header(method, path, params = {})
headers = { headers = {
apiKey: app.app_key, apiKey: app.app_key,
'X-Hmac-Auth-Timestamp': Time.now.to_s(:iso8601), 'X-Hmac-Auth-Timestamp': Time.now.strftime('%Y-%m-%dT%H:%M:%S.%3N%:z'),
'X-Hmac-Auth-Nonce': (Time.now.to_f * 1000).round.to_s + rand(1000..9999).to_s, 'X-Hmac-Auth-Nonce': (Time.now.to_f * 1000).round.to_s + rand(1000..9999).to_s,
'X-Hmac-Auth-Version': '1.0', 'X-Hmac-Auth-Version': '1.0',
'X-Hmac-Auth-IP': RailsDingtalk.config.ip, 'X-Hmac-Auth-IP': RailsDingtalk.config.ip,
'X-Hmac-Auth-MAC': RailsDingtalk.config.mac 'X-Hmac-Auth-MAC': RailsDingtalk.config.mac
} }
result = [
method,
headers[:'X-Hmac-Auth-Timestamp'],
headers[:'X-Hmac-Auth-Nonce'],
path
]
result << params.to_query if params.present?
signature = OpenSSL::HMAC.digest('SHA256', app.app_secret, result.join("\n"))
headers.merge! 'X-Hmac-Auth-Signature': Base64.strict_encode64(signature)
end end
protected protected
def with_access_token(params = {}, tries = 2) def with_access_token(params = {}, tries = 2)
yield params.merge!( yield params
accessKey: app.app_key,
secretKey: app.app_secret
)
rescue => e rescue => e
Rails.logger.debug e.full_message Rails.logger.debug e.full_message
retry unless (tries -= 1).zero? retry unless (tries -= 1).zero?
......
...@@ -22,7 +22,12 @@ module Dingtalk ...@@ -22,7 +22,12 @@ module Dingtalk
headers['Content-Type'] ||= 'application/json' headers['Content-Type'] ||= 'application/json'
url = base + path url = base + path
response = @http.with_headers(headers).post(url, params: params, body: payload) opts = {
params: params
}
opts.merge!(body: payload) if payload.present?
response = @http.with_headers(headers).post(url, **opts)
parse_response(response, options[:as]) parse_response(response, options[:as])
end end
......
...@@ -16,7 +16,7 @@ module Dingtalk ...@@ -16,7 +16,7 @@ module Dingtalk
end end
def generate_user(code) def generate_user(code)
binding.b api.token
end end
end end
......
...@@ -5,10 +5,12 @@ module RailsDingtalk ...@@ -5,10 +5,12 @@ module RailsDingtalk
config.httpx = { config.httpx = {
ssl: { ssl: {
verify_mode: OpenSSL::SSL::VERIFY_NONE verify_mode: OpenSSL::SSL::VERIFY_NONE
},
debug: STDERR,
debug_level: -1
} }
} config.ip = '127.0.0.1'
config.ip = '192.168.0.1' config.mac = '78:7b:8a:dd:94:cc'
config.mac = 'fa:16:3e:27:49:91'
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment