|
|
|
|
@ -29,55 +29,133 @@ class BaseOauthManager(metaclass=ABCMeta):
|
|
|
|
|
ICON_NAME = None
|
|
|
|
|
|
|
|
|
|
def __init__(self, access_token=None, openid=None):
|
|
|
|
|
"""
|
|
|
|
|
初始化OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
access_token (str, optional): 访问令牌
|
|
|
|
|
openid (str, optional): 用户唯一标识
|
|
|
|
|
"""
|
|
|
|
|
self.access_token = access_token
|
|
|
|
|
self.openid = openid
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_access_token_set(self):
|
|
|
|
|
"""检查访问令牌是否已设置"""
|
|
|
|
|
return self.access_token is not None
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_authorized(self):
|
|
|
|
|
"""检查是否已授权(访问令牌和openid都已设置)"""
|
|
|
|
|
return self.is_access_token_set and self.access_token is not None and self.openid is not None
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def get_authorization_url(self, nexturl='/'):
|
|
|
|
|
"""
|
|
|
|
|
获取授权URL
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
nexturl (str): 授权后跳转的URL
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 授权页面URL
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def get_access_token_by_code(self, code):
|
|
|
|
|
"""
|
|
|
|
|
通过授权码获取访问令牌
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): 授权码
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 访问令牌
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def get_oauth_userinfo(self):
|
|
|
|
|
"""
|
|
|
|
|
获取OAuth用户信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def get_picture(self, metadata):
|
|
|
|
|
"""
|
|
|
|
|
从元数据中获取用户头像
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metadata (str): 用户元数据JSON字符串
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 头像URL
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def do_get(self, url, params, headers=None):
|
|
|
|
|
"""
|
|
|
|
|
发送GET请求
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
url (str): 请求URL
|
|
|
|
|
params (dict): 请求参数
|
|
|
|
|
headers (dict, optional): 请求头
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 响应文本
|
|
|
|
|
"""
|
|
|
|
|
rsp = requests.get(url=url, params=params, headers=headers)
|
|
|
|
|
logger.info(rsp.text)
|
|
|
|
|
return rsp.text
|
|
|
|
|
|
|
|
|
|
def do_post(self, url, params, headers=None):
|
|
|
|
|
"""
|
|
|
|
|
发送POST请求
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
url (str): 请求URL
|
|
|
|
|
params (dict): 请求参数
|
|
|
|
|
headers (dict, optional): 请求头
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 响应文本
|
|
|
|
|
"""
|
|
|
|
|
rsp = requests.post(url, params, headers=headers)
|
|
|
|
|
logger.info(rsp.text)
|
|
|
|
|
return rsp.text
|
|
|
|
|
|
|
|
|
|
def get_config(self):
|
|
|
|
|
"""
|
|
|
|
|
获取OAuth配置信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthConfig: OAuth配置对象
|
|
|
|
|
"""
|
|
|
|
|
value = OAuthConfig.objects.filter(type=self.ICON_NAME)
|
|
|
|
|
return value[0] if value else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WBOauthManager(BaseOauthManager):
|
|
|
|
|
"""新浪微博OAuth管理器"""
|
|
|
|
|
AUTH_URL = 'https://api.weibo.com/oauth2/authorize'
|
|
|
|
|
TOKEN_URL = 'https://api.weibo.com/oauth2/access_token'
|
|
|
|
|
API_URL = 'https://api.weibo.com/2/users/show.json'
|
|
|
|
|
ICON_NAME = 'weibo'
|
|
|
|
|
|
|
|
|
|
def __init__(self, access_token=None, openid=None):
|
|
|
|
|
"""
|
|
|
|
|
初始化新浪微博OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
access_token (str, optional): 访问令牌
|
|
|
|
|
openid (str, optional): 用户唯一标识
|
|
|
|
|
"""
|
|
|
|
|
config = self.get_config()
|
|
|
|
|
self.client_id = config.appkey if config else ''
|
|
|
|
|
self.client_secret = config.appsecret if config else ''
|
|
|
|
|
@ -89,6 +167,15 @@ class WBOauthManager(BaseOauthManager):
|
|
|
|
|
openid=openid)
|
|
|
|
|
|
|
|
|
|
def get_authorization_url(self, nexturl='/'):
|
|
|
|
|
"""
|
|
|
|
|
获取新浪微博授权URL
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
nexturl (str): 授权后跳转的URL
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 新浪微博授权页面URL
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'response_type': 'code',
|
|
|
|
|
@ -98,7 +185,18 @@ class WBOauthManager(BaseOauthManager):
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
def get_access_token_by_code(self, code):
|
|
|
|
|
"""
|
|
|
|
|
通过授权码获取新浪微博访问令牌
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): 授权码
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
OAuthAccessTokenException: 获取访问令牌失败时抛出异常
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'client_secret': self.client_secret,
|
|
|
|
|
@ -117,6 +215,12 @@ class WBOauthManager(BaseOauthManager):
|
|
|
|
|
raise OAuthAccessTokenException(rsp)
|
|
|
|
|
|
|
|
|
|
def get_oauth_userinfo(self):
|
|
|
|
|
"""
|
|
|
|
|
获取新浪微博用户信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
"""
|
|
|
|
|
if not self.is_authorized:
|
|
|
|
|
return None
|
|
|
|
|
params = {
|
|
|
|
|
@ -142,12 +246,26 @@ class WBOauthManager(BaseOauthManager):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_picture(self, metadata):
|
|
|
|
|
"""
|
|
|
|
|
从新浪微博元数据中获取用户头像
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metadata (str): 用户元数据JSON字符串
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 头像URL
|
|
|
|
|
"""
|
|
|
|
|
datas = json.loads(metadata)
|
|
|
|
|
return datas['avatar_large']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProxyManagerMixin:
|
|
|
|
|
"""代理管理混入类,用于支持HTTP代理"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
初始化代理设置
|
|
|
|
|
"""
|
|
|
|
|
if os.environ.get("HTTP_PROXY"):
|
|
|
|
|
self.proxies = {
|
|
|
|
|
"http": os.environ.get("HTTP_PROXY"),
|
|
|
|
|
@ -157,23 +275,53 @@ class ProxyManagerMixin:
|
|
|
|
|
self.proxies = None
|
|
|
|
|
|
|
|
|
|
def do_get(self, url, params, headers=None):
|
|
|
|
|
"""
|
|
|
|
|
发送带代理的GET请求
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
url (str): 请求URL
|
|
|
|
|
params (dict): 请求参数
|
|
|
|
|
headers (dict, optional): 请求头
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 响应文本
|
|
|
|
|
"""
|
|
|
|
|
rsp = requests.get(url=url, params=params, headers=headers, proxies=self.proxies)
|
|
|
|
|
logger.info(rsp.text)
|
|
|
|
|
return rsp.text
|
|
|
|
|
|
|
|
|
|
def do_post(self, url, params, headers=None):
|
|
|
|
|
"""
|
|
|
|
|
发送带代理的POST请求
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
url (str): 请求URL
|
|
|
|
|
params (dict): 请求参数
|
|
|
|
|
headers (dict, optional): 请求头
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 响应文本
|
|
|
|
|
"""
|
|
|
|
|
rsp = requests.post(url, params, headers=headers, proxies=self.proxies)
|
|
|
|
|
logger.info(rsp.text)
|
|
|
|
|
return rsp.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
"""Google OAuth管理器"""
|
|
|
|
|
AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth'
|
|
|
|
|
TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token'
|
|
|
|
|
API_URL = 'https://www.googleapis.com/oauth2/v3/userinfo'
|
|
|
|
|
ICON_NAME = 'google'
|
|
|
|
|
|
|
|
|
|
def __init__(self, access_token=None, openid=None):
|
|
|
|
|
"""
|
|
|
|
|
初始化Google OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
access_token (str, optional): 访问令牌
|
|
|
|
|
openid (str, optional): 用户唯一标识
|
|
|
|
|
"""
|
|
|
|
|
config = self.get_config()
|
|
|
|
|
self.client_id = config.appkey if config else ''
|
|
|
|
|
self.client_secret = config.appsecret if config else ''
|
|
|
|
|
@ -185,6 +333,15 @@ class GoogleOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
openid=openid)
|
|
|
|
|
|
|
|
|
|
def get_authorization_url(self, nexturl='/'):
|
|
|
|
|
"""
|
|
|
|
|
获取Google授权URL
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
nexturl (str): 授权后跳转的URL
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: Google授权页面URL
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'response_type': 'code',
|
|
|
|
|
@ -195,6 +352,18 @@ class GoogleOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
def get_access_token_by_code(self, code):
|
|
|
|
|
"""
|
|
|
|
|
通过授权码获取Google访问令牌
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): 授权码
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 访问令牌
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
OAuthAccessTokenException: 获取访问令牌失败时抛出异常
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'client_secret': self.client_secret,
|
|
|
|
|
@ -216,6 +385,12 @@ class GoogleOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
raise OAuthAccessTokenException(rsp)
|
|
|
|
|
|
|
|
|
|
def get_oauth_userinfo(self):
|
|
|
|
|
"""
|
|
|
|
|
获取Google用户信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
"""
|
|
|
|
|
if not self.is_authorized:
|
|
|
|
|
return None
|
|
|
|
|
params = {
|
|
|
|
|
@ -241,17 +416,34 @@ class GoogleOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_picture(self, metadata):
|
|
|
|
|
"""
|
|
|
|
|
从Google元数据中获取用户头像
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metadata (str): 用户元数据JSON字符串
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 头像URL
|
|
|
|
|
"""
|
|
|
|
|
datas = json.loads(metadata)
|
|
|
|
|
return datas['picture']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GitHubOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
"""GitHub OAuth管理器"""
|
|
|
|
|
AUTH_URL = 'https://github.com/login/oauth/authorize'
|
|
|
|
|
TOKEN_URL = 'https://github.com/login/oauth/access_token'
|
|
|
|
|
API_URL = 'https://api.github.com/user'
|
|
|
|
|
ICON_NAME = 'github'
|
|
|
|
|
|
|
|
|
|
def __init__(self, access_token=None, openid=None):
|
|
|
|
|
"""
|
|
|
|
|
初始化GitHub OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
access_token (str, optional): 访问令牌
|
|
|
|
|
openid (str, optional): 用户唯一标识
|
|
|
|
|
"""
|
|
|
|
|
config = self.get_config()
|
|
|
|
|
self.client_id = config.appkey if config else ''
|
|
|
|
|
self.client_secret = config.appsecret if config else ''
|
|
|
|
|
@ -263,6 +455,15 @@ class GitHubOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
openid=openid)
|
|
|
|
|
|
|
|
|
|
def get_authorization_url(self, next_url='/'):
|
|
|
|
|
"""
|
|
|
|
|
获取GitHub授权URL
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
next_url (str): 授权后跳转的URL
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: GitHub授权页面URL
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'response_type': 'code',
|
|
|
|
|
@ -273,6 +474,18 @@ class GitHubOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
def get_access_token_by_code(self, code):
|
|
|
|
|
"""
|
|
|
|
|
通过授权码获取GitHub访问令牌
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): 授权码
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 访问令牌
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
OAuthAccessTokenException: 获取访问令牌失败时抛出异常
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'client_secret': self.client_secret,
|
|
|
|
|
@ -292,7 +505,12 @@ class GitHubOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
raise OAuthAccessTokenException(rsp)
|
|
|
|
|
|
|
|
|
|
def get_oauth_userinfo(self):
|
|
|
|
|
"""
|
|
|
|
|
获取GitHub用户信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
"""
|
|
|
|
|
rsp = self.do_get(self.API_URL, params={}, headers={
|
|
|
|
|
"Authorization": "token " + self.access_token
|
|
|
|
|
})
|
|
|
|
|
@ -314,17 +532,34 @@ class GitHubOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_picture(self, metadata):
|
|
|
|
|
"""
|
|
|
|
|
从GitHub元数据中获取用户头像
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metadata (str): 用户元数据JSON字符串
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 头像URL
|
|
|
|
|
"""
|
|
|
|
|
datas = json.loads(metadata)
|
|
|
|
|
return datas['avatar_url']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FaceBookOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
"""Facebook OAuth管理器"""
|
|
|
|
|
AUTH_URL = 'https://www.facebook.com/v16.0/dialog/oauth'
|
|
|
|
|
TOKEN_URL = 'https://graph.facebook.com/v16.0/oauth/access_token'
|
|
|
|
|
API_URL = 'https://graph.facebook.com/me'
|
|
|
|
|
ICON_NAME = 'facebook'
|
|
|
|
|
|
|
|
|
|
def __init__(self, access_token=None, openid=None):
|
|
|
|
|
"""
|
|
|
|
|
初始化Facebook OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
access_token (str, optional): 访问令牌
|
|
|
|
|
openid (str, optional): 用户唯一标识
|
|
|
|
|
"""
|
|
|
|
|
config = self.get_config()
|
|
|
|
|
self.client_id = config.appkey if config else ''
|
|
|
|
|
self.client_secret = config.appsecret if config else ''
|
|
|
|
|
@ -336,6 +571,15 @@ class FaceBookOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
openid=openid)
|
|
|
|
|
|
|
|
|
|
def get_authorization_url(self, next_url='/'):
|
|
|
|
|
"""
|
|
|
|
|
获取Facebook授权URL
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
next_url (str): 授权后跳转的URL
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: Facebook授权页面URL
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'response_type': 'code',
|
|
|
|
|
@ -346,6 +590,18 @@ class FaceBookOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
def get_access_token_by_code(self, code):
|
|
|
|
|
"""
|
|
|
|
|
通过授权码获取Facebook访问令牌
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): 授权码
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 访问令牌
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
OAuthAccessTokenException: 获取访问令牌失败时抛出异常
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
'client_secret': self.client_secret,
|
|
|
|
|
@ -365,6 +621,12 @@ class FaceBookOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
raise OAuthAccessTokenException(rsp)
|
|
|
|
|
|
|
|
|
|
def get_oauth_userinfo(self):
|
|
|
|
|
"""
|
|
|
|
|
获取Facebook用户信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'access_token': self.access_token,
|
|
|
|
|
'fields': 'id,name,picture,email'
|
|
|
|
|
@ -388,11 +650,21 @@ class FaceBookOauthManager(ProxyManagerMixin, BaseOauthManager):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_picture(self, metadata):
|
|
|
|
|
"""
|
|
|
|
|
从Facebook元数据中获取用户头像
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metadata (str): 用户元数据JSON字符串
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 头像URL
|
|
|
|
|
"""
|
|
|
|
|
datas = json.loads(metadata)
|
|
|
|
|
return str(datas['picture']['data']['url'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QQOauthManager(BaseOauthManager):
|
|
|
|
|
"""QQ OAuth管理器"""
|
|
|
|
|
AUTH_URL = 'https://graph.qq.com/oauth2.0/authorize'
|
|
|
|
|
TOKEN_URL = 'https://graph.qq.com/oauth2.0/token'
|
|
|
|
|
API_URL = 'https://graph.qq.com/user/get_user_info'
|
|
|
|
|
@ -400,6 +672,13 @@ class QQOauthManager(BaseOauthManager):
|
|
|
|
|
ICON_NAME = 'qq'
|
|
|
|
|
|
|
|
|
|
def __init__(self, access_token=None, openid=None):
|
|
|
|
|
"""
|
|
|
|
|
初始化QQ OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
access_token (str, optional): 访问令牌
|
|
|
|
|
openid (str, optional): 用户唯一标识
|
|
|
|
|
"""
|
|
|
|
|
config = self.get_config()
|
|
|
|
|
self.client_id = config.appkey if config else ''
|
|
|
|
|
self.client_secret = config.appsecret if config else ''
|
|
|
|
|
@ -411,6 +690,15 @@ class QQOauthManager(BaseOauthManager):
|
|
|
|
|
openid=openid)
|
|
|
|
|
|
|
|
|
|
def get_authorization_url(self, next_url='/'):
|
|
|
|
|
"""
|
|
|
|
|
获取QQ授权URL
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
next_url (str): 授权后跳转的URL
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: QQ授权页面URL
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'response_type': 'code',
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
@ -420,6 +708,18 @@ class QQOauthManager(BaseOauthManager):
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
def get_access_token_by_code(self, code):
|
|
|
|
|
"""
|
|
|
|
|
通过授权码获取QQ访问令牌
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): 授权码
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 访问令牌
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
OAuthAccessTokenException: 获取访问令牌失败时抛出异常
|
|
|
|
|
"""
|
|
|
|
|
params = {
|
|
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
|
'client_id': self.client_id,
|
|
|
|
|
@ -438,6 +738,12 @@ class QQOauthManager(BaseOauthManager):
|
|
|
|
|
raise OAuthAccessTokenException(rsp)
|
|
|
|
|
|
|
|
|
|
def get_open_id(self):
|
|
|
|
|
"""
|
|
|
|
|
获取QQ用户openid
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 用户openid
|
|
|
|
|
"""
|
|
|
|
|
if self.is_access_token_set:
|
|
|
|
|
params = {
|
|
|
|
|
'access_token': self.access_token
|
|
|
|
|
@ -454,6 +760,12 @@ class QQOauthManager(BaseOauthManager):
|
|
|
|
|
return openid
|
|
|
|
|
|
|
|
|
|
def get_oauth_userinfo(self):
|
|
|
|
|
"""
|
|
|
|
|
获取QQ用户信息
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
OAuthUser: OAuth用户对象
|
|
|
|
|
"""
|
|
|
|
|
openid = self.get_open_id()
|
|
|
|
|
if openid:
|
|
|
|
|
params = {
|
|
|
|
|
@ -477,12 +789,27 @@ class QQOauthManager(BaseOauthManager):
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
def get_picture(self, metadata):
|
|
|
|
|
"""
|
|
|
|
|
从QQ元数据中获取用户头像
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metadata (str): 用户元数据JSON字符串
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 头像URL
|
|
|
|
|
"""
|
|
|
|
|
datas = json.loads(metadata)
|
|
|
|
|
return str(datas['figureurl'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cache_decorator(expiration=100 * 60)
|
|
|
|
|
def get_oauth_apps():
|
|
|
|
|
"""
|
|
|
|
|
获取所有启用的OAuth应用
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list: OAuth应用管理器实例列表
|
|
|
|
|
"""
|
|
|
|
|
configs = OAuthConfig.objects.filter(is_enable=True).all()
|
|
|
|
|
if not configs:
|
|
|
|
|
return []
|
|
|
|
|
@ -493,6 +820,15 @@ def get_oauth_apps():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_manager_by_type(type):
|
|
|
|
|
"""
|
|
|
|
|
根据类型获取OAuth管理器
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
type (str): OAuth类型(如'weibo', 'google', 'github'等)
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
BaseOauthManager: 对应的OAuth管理器实例
|
|
|
|
|
"""
|
|
|
|
|
applications = get_oauth_apps()
|
|
|
|
|
if applications:
|
|
|
|
|
finds = list(
|
|
|
|
|
|