Package: appleauth
Apple AuthPython library to implement Sign In with Apple in your Django backend.Table of Contents💾 Installation🍎 Apple docs📝 Configuration🚀 Usage🤖 Endpoints📜 Code Of Conduct💾 InstallationTo easily install or upgrade to the latest release, use pip.$ pip install appleauth🍎 Apple docsFrom now on, some stuff is much better explained on the Apple docs, so when in doubt just check (if you haven't done so) the following documents:Sign In With AppleRequest an authorization to the Sign in with Apple serverGenerate and validate tokensRevoke tokens📝 ConfigurationTo start using the lib, some Apple Keys needs to be generated:client_id(string)The identifier (App ID or Services ID) for your app. The identifier must not include your Team ID, to help prevent the possibility of exposing sensitive data to the end user.client_secret(string)A secret JSON Web Token, generated by the developer, that uses the Sign in with Apple private key associated with your developer account. Authorization code and refresh token validation requests require this parameter.team_id(string)Team ID of your developer account this can be found in your apple developer portal => identifier of your app => "App ID prefix".key_id(string)The Key ID of the p8 file.🚀 UsageYou can install the library directly from PYPI using pip:pipinstallappleauthEdit your settings.py file and updateINSTALLED_APPSandAPPLE_CONFIGwith the appropriate keys generated via Apple Developer Portal:INSTALLED_APPS=[...,"appleauth"]# Apple ConfigAPPLE_CONFIG={"APPLE_KEY_ID":"","APPLE_TEAM_ID":"","APPLE_CLIENT_ID":"","APPLE_PRIVATE_KEY":"","APPLE_REDIRECT_URL":"{{BASE URL}}/auth/apple/token",# https://127.0.0.1:8000/auth/apple/token"APPLE_SCOPE":["name","email"],"RESPONSE_HANDLER_CLASS":"users.services.AppleSignInResponseHandler",}NOTE:In the above config,APPLE_REDIRECT_URLis an endpoint which serves as a proxy to redirect the response of Apple server authorization to theredirect_urlpassed as query param while generating Authorization URL.The response of authorization by Apple is aPOST requestwhere authcodeandstateis sent in request body. This endpoint converts the request body data to query params and send it to the redirect URL.Create Response Handler Class and update path inAPPLE_CONFIG, In this example we are considering it to be in/users/services/AppleSignInResponseHandlerfromappleauth.servicesimportAppleAuthResponseHandlerclassAppleSignInResponseHandler(AppleAuthResponseHandler):defhandle_fetch_or_create_user(self,request,user_dict):email=user_dict.get("email",None)apple_id=user_dict.get("apple_id",None)# Implement a method to handle user creationuser,is_created=get_or_create_user(email,apple_id)context={"is_created":is_created}returnuser,contextdefgenerate_response_json(self,user,extra_context):# Implement a serializer to serialize user dataresponse=AuthUserSerializer(user,context=extra_context)returnresponse.dataNOTE:AuthUserSerializerused in above ref. could be created as per app's functionality and contain fields which needs to be sent in response of authorization.get_or_create_usermethod used in above code ref. could be created as per app's functionality.Update Routes:fromrest_framework.routersimportDefaultRouterfromappleauth.apisimportAppleAuthViewsetdefault_router=DefaultRouter(trailing_slash=False)default_router.register("auth/apple",AppleAuthViewset,basename="apple-auth")urlpatterns=[...]+default_router.urls🤖 EndpointsProvides following APIs:Authorization URL APIIt generates Apple'sauthorization-urlused to redirect to Apple's Authorization Server to request consent from resource owner.Authorize APIExchange authorization code for access token.Talk to resource server with access token and fetch user's profile information.Authorize IOS Token APIVerifies an ID Token issued by Apple's authorization server.Fetch user details from decoded token.NOTE:This documentation changes frequently, checkout thechangelogfor detailed breaking changes and features added.Code of ConductIn order to foster a kind, inclusive, and harassment-free community, we have a code of conduct, which can be foundhere. We ask you to treat everyone as a smart human programmer that shares an interest in Python and Apple Pass Generator with you.