1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# frozen_string_literal: true
require 'openssl'
require 'base64'
require 'cgi'
require 'json'
module OmniAuth
module Dingtalk
module Client
class Base < ::OAuth2::Client
AUTHORIZE_URL = {
'qrcode' => '/connect/qrconnect',
'account' => '/connect/oauth2/sns_authorize'
}.freeze
GET_USER_INFO_BY_CODE_URL = '/sns/getuserinfo_bycode'
GET_USER_ID_BY_UNIONID_URL = '/topapi/user/getbyunionid'
GET_USER_INFO_BY_ID_URL = '/topapi/v2/user/get'
def initialize(client_id, client_secret, options = {}, &block)
opts = {
authorize_url: AUTHORIZE_URL.fetch(options[:authorize_method].to_s, AUTHORIZE_URL['qrcode']),
token_url: token_url,
token_method: :get
}.merge(options)
super(client_id, client_secret, opts, &block)
end
def get_user_info_by_code(code)
t = (Time.now.to_f * 1000).to_i.to_s
raw_sign = Base64.encode64(OpenSSL::HMAC.digest('SHA256', secret, t)).strip
sign = CGI.escape(raw_sign)
url = "#{GET_USER_INFO_BY_CODE_URL}?accessKey=#{id}×tamp=#{t}&signature=#{sign}"
request(:post, url,
headers: { 'Content-Type' => 'application/json' },
body: { tmp_auth_code: code }.to_json
).parsed
end
def get_user_id_by_unionid(access_token, unionid)
request(:post, GET_USER_ID_BY_UNIONID_URL,
headers: { 'Content-Type' => 'application/json' },
body: { unionid: unionid }.to_json,
params: { access_token: access_token }
).parsed
end
def get_user_info_by_id(access_token, id)
request(:post, GET_USER_INFO_BY_ID_URL,
headers: { 'Content-Type' => 'application/json' },
body: { userid: id }.to_json,
params: { access_token: access_token }
).parsed
end
def get_user_info(params = {})
raise NotImplementedError
end
def token_url
self.class.const_get(:TOKEN_URL) rescue nil
end
def token_params
{ appid: id, appsecret: secret }
end
def get_token(params, access_token_opts = {}, extract_access_token = options[:extract_access_token])
super(token_params.merge(params), access_token_opts, extract_access_token)
end
end
end
end
end
|