Class: OmniAuth::Dingtalk::Client::Base

Inherits:
OAuth2::Client
  • Object
show all
Defined in:
lib/omniauth-dingtalk/client/base.rb

Direct Known Subclasses

EnterpriseInternal, ThirdPartyPersonal

Constant Summary collapse

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'

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, options = {}, &block) ⇒ Base

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
# File 'lib/omniauth-dingtalk/client/base.rb', line 21

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

Instance Method Details

#get_token(params, access_token_opts = {}, extract_access_token = options[:extract_access_token]) ⇒ Object



72
73
74
# File 'lib/omniauth-dingtalk/client/base.rb', line 72

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

#get_user_id_by_unionid(access_token, unionid) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/omniauth-dingtalk/client/base.rb', line 44

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

#get_user_info(params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/omniauth-dingtalk/client/base.rb', line 60

def (params = {})
  raise NotImplementedError
end

#get_user_info_by_code(code) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/omniauth-dingtalk/client/base.rb', line 31

def (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}&timestamp=#{t}&signature=#{sign}"

  request(:post, url,
    headers: { 'Content-Type' => 'application/json' },
    body: { tmp_auth_code: code }.to_json
  ).parsed
end

#get_user_info_by_id(access_token, id) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/omniauth-dingtalk/client/base.rb', line 52

def (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

#token_paramsObject



68
69
70
# File 'lib/omniauth-dingtalk/client/base.rb', line 68

def token_params
  { appid: id, appsecret: secret }
end

#token_urlObject



64
65
66
# File 'lib/omniauth-dingtalk/client/base.rb', line 64

def token_url
  self.class.const_get(:TOKEN_URL) rescue nil
end