Commit 0094fa1e by mingyuan

sass

parent 5471c943
......@@ -28,7 +28,7 @@ module Dingtalk::Api
def post(path, params: {}, headers: {}, base: base_host, **payload)
with_access_token('post', path, params, headers, payload) do |processed_params, processed_headers|
@client.post path, payload, headers: processed_headers, params: processed_params, base: base
@client.post path, payload.to_json, headers: processed_headers, params: processed_params, base: base
end
end
......
module Dingtalk::Api
module Inner::Saas
BASE = 'https://openplatform.dg-work.cn/'
BASE = 'https://openplatform.dg-work.cn'
def getuserinfo(code)
payload = {
auth_code: code
}
headers = sign_access_header('POST', '/gettoken.json', payload)
r = post 'rpc/oauth2/dingtalk_app_user.json', headers: headers, base: BASE
r = post '/rpc/oauth2/dingtalk_app_user.json', auth_code: code, base: BASE
binding.b
if r.is_a? Hash
r['result']
r.dig('content', 'data')
else
r
end
......
module Dingtalk::Api
class Saas < Base
include Inner::Saas
BASE = 'https://openplatform.dg-work.cn/'
BASE = 'https://openplatform.dg-work.cn'
def token
headers = sign_header('POST', '/gettoken.json')
......@@ -12,12 +12,19 @@ module Dingtalk::Api
}
end
def post(path, params: {}, headers: {}, base: base_host, **payload)
with_access_token('post', path, params, headers, payload) do |processed_params, processed_headers|
@client.post path, payload.to_query, headers: processed_headers, params: processed_params, base: base
end
end
protected
def with_access_token(method, path, params = {}, headers = {}, payload = {}, tries = 2)
app.refresh_access_token unless app.access_token_valid?
processed_params = params.merge!(access_token: app.access_token)
processed_headers = sign_header(method, path, processed_params, payload)
yield processed_params, processed_headers
payload.merge!(access_token: app.access_token)
#params.merge!(access_token: app.access_token)
processed_headers = sign_header(method, path, params, payload)
yield params, processed_headers
rescue => e
Rails.logger.debug e.full_message
app.refresh_access_token
......@@ -27,8 +34,8 @@ module Dingtalk::Api
def sign_header(method, path, params = {}, payload = {})
headers = {
apiKey: app.app_key,
'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-Timestamp': Time.now.strftime('%Y-%m-%dT%H:%M:%S.%6N%:z'),
'X-Hmac-Auth-Nonce': '16314248251027993', #(Time.now.to_f * 1000).round.to_s + rand(1000..9999).to_s,
'X-Hmac-Auth-Version': '1.0',
'X-Hmac-Auth-IP': RailsDingtalk.config.ip,
'X-Hmac-Auth-MAC': RailsDingtalk.config.mac
......@@ -40,7 +47,7 @@ module Dingtalk::Api
headers[:'X-Hmac-Auth-Nonce'],
path
]
result << params.merge(payload).to_query if params.present?
result << params.merge(payload).to_query if params.present? || payload.present?
signature = OpenSSL::HMAC.digest('SHA256', app.app_secret, result.join("\n"))
headers.merge! 'X-Hmac-Auth-Signature': Base64.strict_encode64(signature)
end
......
......@@ -23,11 +23,14 @@ module Dingtalk
url = base + path
opts = {
params: params
params: params,
headers: headers
}
opts.merge!(body: payload.to_json) if payload.present?
opts.merge!(body: payload) if payload.present?
response = @http.with_headers(headers).post(url, **opts)
binding.b
parse_response(response, options[:as])
end
......
......@@ -17,7 +17,6 @@ module Dingtalk
def generate_user(code)
r = api.getuserinfo(code)
binding.b
end
end
......
......@@ -5,9 +5,7 @@ module RailsDingtalk
config.httpx = {
ssl: {
verify_mode: OpenSSL::SSL::VERIFY_NONE
},
debug: STDERR,
debug_level: -1
}
}
config.ip = '127.0.0.1'
config.mac = '78:7b:8a:dd:94:cc'
......
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