oauth2 support

This commit is contained in:
Leven
2022-12-16 11:57:40 +08:00
parent 4ed8c1e0e2
commit 6b90cd3be7
7 changed files with 114 additions and 12 deletions

View File

@@ -84,3 +84,25 @@ class DingDingOps(AppKeyClient):
except (KeyError, IndexError) as k_error:
return False, 'get_user_detail_by_user_id: %s' % str(k_error)
def get_user_detail(self, code, home_url):
"""
临时授权码换取userinfo
"""
_status, user_id = self.get_user_id_by_code(code)
# 判断 user_id 在本企业钉钉/微信中是否存在
if not _status:
context = {
'msg': '获取userid失败错误信息{}'.format(user_id),
'button_click': "window.location.href='%s'" % home_url,
'button_display': "返回主页"
}
return False, context, user_id
detail_status, user_info = self.get_user_detail_by_user_id(user_id)
if not detail_status:
context = {
'msg': '获取用户信息失败,错误信息:{}'.format(user_info),
'button_click': "window.location.href='%s'" % home_url,
'button_display': "返回主页"
}
return False, context, user_info
return True, user_id, user_info

View File

@@ -15,7 +15,11 @@ def format2username(account):
:param account 用户账号可以是邮箱、DOMAIN\\username、username格式。
:return: username
"""
if account:
if account is None:
return False, NameError(
"传入的用户账号为空!".format(account))
try:
mail_compile = re.compile(r'(.*)@(.*)')
domain_compile = re.compile(r'(.*)\\(.*)')
@@ -25,8 +29,8 @@ def format2username(account):
return True, re.fullmatch(domain_compile, account).group(2)
else:
return True, account.lower()
else:
return False, NameError("{}格式化失败注意account用户账号是邮箱或DOMAIN\\username或username格式".format(account))
except Exception as e:
return False, NameError("格式化失败注意account用户账号是邮箱或DOMAIN\\username或username格式,错误信息[{}]".format(account, e))
def get_user_is_active(user_info):

View File

@@ -20,6 +20,10 @@ else:
from conf.local_settings import *
CORP_API_TYPE = {
'GET_USER_TICKET_OAUTH2': ['/cgi-bin/auth/getuserinfo?access_token=ACCESS_TOKEN', 'GET'],
'GET_USER_INFO_OAUTH2': ['/cgi-bin/auth/getuserdetail?access_token=ACCESS_TOKEN', 'POST'],
'GET_ACCESS_TOKEN': ['/cgi-bin/gettoken', 'GET'],
'USER_CREATE': ['/cgi-bin/user/create?access_token=ACCESS_TOKEN', 'POST'],
'USER_GET': ['/cgi-bin/user/get?access_token=ACCESS_TOKEN', 'GET'],
@@ -139,6 +143,66 @@ class WeWorkOps(AbstractApi):
except Exception as e:
return False, "get_user_detail_by_user_id: {}".format(e)
def get_user_ticket_by_code_with_oauth2(self, code):
try:
return True, self.http_call(
CORP_API_TYPE['GET_USER_TICKET_OAUTH2'],
{
'code': code,
})
except ApiException as e:
return False, "get_user_ticket_by_code_with_oauth2: {}-{}".format(e.errCode, e.errMsg)
except Exception as e:
return False, "get_user_ticket_by_code_with_oauth2: {}".format(e)
def get_user_info_by_ticket_with_oauth2(self, user_ticket):
try:
return True, self.http_call(
CORP_API_TYPE['GET_USER_TICKET_OAUTH2'],
{
'user_ticket': user_ticket
})
except ApiException as e:
return False, "get_user_info_by_ticket_with_oauth2: {}-{}".format(e.errCode, e.errMsg)
except Exception as e:
return False, "get_user_info_by_ticket_with_oauth2: {}".format(e)
def get_user_detail(self, code, home_url):
"""
临时授权码换取userinfo
"""
_status, ticket_data = self.get_user_ticket_by_code_with_oauth2(code)
print('ticket_data ----------- ', ticket_data)
# 判断 user_ticket 是否存在
if not _status:
context = {
'msg': '获取userid失败错误信息{}'.format(ticket_data),
'button_click': "window.location.href='%s'" % home_url,
'button_display': "返回主页"
}
return False, context, ticket_data
user_id = ticket_data.get('userid')
if ticket_data.get('user_ticket') is None:
context = {
'msg': '获取用户Ticket失败当前扫码用户[{}]可能未加入企业!'.format(user_id),
'button_click': "window.location.href='%s'" % home_url,
'button_display': "返回主页"
}
return False, context, user_id
# 通过user_ticket获取企业微信用户详情信息
detail_status, user_info = self.get_user_info_by_ticket_with_oauth2(ticket_data.get('user_ticket'))
print(user_info)
if not detail_status:
context = {
'msg': '获取用户信息失败,错误信息:{}'.format(user_id),
'button_click': "window.location.href='%s'" % home_url,
'button_display': "返回主页"
}
return False, context
return True, user_id, user_info
if __name__ == '__main__':
wx = WeWorkOps()