在Django 4升级后,Apple SSO(即苹果账号单点登录)出现了问题。解决方法是使用Python的Authlib库来支持Apple SSO。以下是代码示例:
pip install authlib
from authlib.integrations.requests_client import RequestsClient
from social_core.backends.apple import AppleIdAuth
from django.conf import settings
class AppleSSOAuth(AppleIdAuth):
OIDC_ENDPOINT = 'https://appleid.apple.com' # use this for production
JWK_SET_URL = 'https://appleid.apple.com/auth/keys' # use this for production
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
session = requests.Session()
session.verify = False
client = RequestsClient(session=session)
self.client = client
def auth_params(self, *args, **kwargs):
params = super().auth_params(*args, **kwargs)
params['response_mode'] = 'form_post'
params['response_type'] = 'code id_token'
return params
def user_data(self, access_token, *args, **kwargs):
apple_public_key = self.get_apple_public_key()
user_data = super().user_data(access_token, *args, **kwargs)
return {
'provider': self.name,
'id': user_data.get('sub'),
'email': user_data.get('email'),
'first_name': user_data.get('first_name', ''),
'last_name': user_data.get('last_name', ''),
'access_token': access_token,
'apple_public_key': apple_public_key
}
def get_apple_public_key(self):
jwk_set = self.client.get(self.JWK_SET_URL).json()
kid = self.jwt_headers()['kid']
for jwk in jwk_set['keys']:
if jwk['kid'] == kid:
public_key = jwk['n'] + '\n' + jwk['e']
break
return public_key
SOCIAL_AUTH_APPLE_KEY = settings.SOCIAL_AUTH_APPLE_KEY
SOCIAL_AUTH_APPLE_SECRET =