lib/omniauth/strategies/dingding.rb in oauth2_dingtalk-0.2.0 vs lib/omniauth/strategies/dingding.rb in oauth2_dingtalk-0.2.2
- old
+ new
@@ -1,29 +1,33 @@
require 'omniauth-oauth2'
+require 'useragent'
module OmniAuth
module Strategies
class Dingding < OmniAuth::Strategies::OAuth2
option :name, 'dingding'
option :client_options,
site: 'https://oapi.dingtalk.com',
- authorize_url: '/connect/qrconnect',
+ authorize_url: '/:placeholder',
+ scan_code_authorize_url: '/connect/qrconnect',
+ account_authorize_url: '/connect/oauth2/sns_authorize',
token_url: '/sns/gettoken',
persistent_url: '/sns/get_persistent_code',
sns_token: '/sns/get_sns_token',
user_info: '/sns/getuserinfo',
token_method: :get
option :authorize_params, scope: 'snsapi_login'
uid do
- raw_info['user_info']['unionid']
+ raw_info['user_info']['openid']
end
info do
{
+ unionid: raw_info['user_info']['unionid'],
name: raw_info['user_info']['nick'],
ding_id: raw_info['user_info']['dingId']
}
end
@@ -32,11 +36,11 @@
end
def request_phase
params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params)
params['appid'] = params.delete('client_id')
- redirect client.authorize_url(params)
+ redirect scan_code_or_account_verify(client.authorize_url(params))
end
def raw_info
return persistent_code if persistent_code['errcode'] != 0
return sns_token if sns_token['errcode'] != 0
@@ -71,9 +75,15 @@
end
def user_info
@user_info ||=
access_token.get(options.client_options.user_info + "?sns_token=#{@sns_token['sns_token']}").parsed
+ end
+
+ def scan_code_or_account_verify(url)
+ url.sub!('/:placeholder',
+ UserAgent.parse(request.env['HTTP_USER_AGENT']).mobile? ?
+ options.client_options.account_authorize_url : options.client_options.scan_code_authorize_url)
end
end
end
end