wagnerdelima / drf-social-oauth2

drf-social-oauth2 makes it easy to integrate Django social authentication with major OAuth2 providers, i.e., Facebook, Twitter, Google, etc.
https://drf-social-oauth2.readthedocs.io/en/latest/
MIT License
270 stars 34 forks source link

How to use custom backends? #111

Closed Danirill closed 2 years ago

Danirill commented 2 years ago

Hi, I'm trying to implement an apple login. But in my project, Apple login is needed both on the web interface and in the mobile application. I set up an apple backend and it works great! But for two platforms, you need to use two different client_id. I saw advice on stackoverflow that two platforms can use multiple backends.

I added my custom backend following python-social-auth instructions but it didn't work.

AUTHENTICATION_BACKENDS = (
    'oauth2_provider.backends.OAuth2Backend',
    'django.contrib.auth.backends.ModelBackend',
    'drf_social_oauth2.backends.DjangoOAuth2',
    'social_core.backends.apple.AppleIdAuth', # Works fine
    '.api.v1.users.custom-auth-backends.AppleIdMobile.AppleIdMobile',  # Not working :(
)

SOCIAL_AUTH_APPLE_ID_CLIENT = 'com.xxxx.xxxx'
SOCIAL_AUTH_APPLE_ID_TEAM = 'xxxxxxxx'               
SOCIAL_AUTH_APPLE_ID_KEY = 'xxxxxxxxxx'                
SOCIAL_AUTH_APPLE_ID_SECRET = "xxxxxxxx"
SOCIAL_AUTH_APPLE_ID_SCOPE = ['email', 'name']
SOCIAL_AUTH_APPLE_ID_EMAIL_AS_USERNAME = False 
# the difference is only in client_id
SOCIAL_AUTH_APPLE_ID_MOBILE_CLIENT = 'com.yyyyyy.yyyyy'       
SOCIAL_AUTH_APPLE_ID_MOBILE_TEAM = SOCIAL_AUTH_APPLE_ID_TEAM
SOCIAL_AUTH_APPLE_ID_MOBILE_KEY = SOCIAL_AUTH_APPLE_ID_KEY
SOCIAL_AUTH_APPLE_ID_MOBILE_SECRET = SOCIAL_AUTH_APPLE_ID_SECRET
SOCIAL_AUTH_APPLE_ID_MOBILE_SCOPE = SOCIAL_AUTH_APPLE_ID_SCOPE
SOCIAL_AUTH_APPLE_ID_MOBILE_EMAIL_AS_USERNAME = SOCIAL_AUTH_APPLE_ID_EMAIL_AS_USERNAME

I implemented my custom backend like this:

  1. I took the source code for entering via apple from social_core/backends
  2. Created a new class and changed its name to apple-id-mobile

It turned out something like this:

class AppleIdMobile(BaseOAuth2):
    name = 'apple-id-mobile'

    JWK_URL = 'https://appleid.apple.com/auth/keys'
    AUTHORIZATION_URL = 'https://appleid.apple.com/auth/authorize'
    ACCESS_TOKEN_URL = 'https://appleid.apple.com/auth/token'
    ACCESS_TOKEN_METHOD = 'POST'
    RESPONSE_MODE = None
    ....

When I try to convert-token, I get error:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/social_core/backends/utils.py", line 50, in get_backend
return BACKENDSCACHE[name]
KeyError: 'apple-id-mobile'

Any ideas what could have gone wrong? Any help would be very helpful, thanks!

Danirill commented 2 years ago

I specified the wrong classpath in AUTHENTICATION_BACKENDSPath should not start with a dot. In my example, correct path is: AUTHENTICATION_BACKENDS = ( .... 'api.v1.users.custom-auth-backends.AppleIdMobile.AppleIdMobile', # Working :) .... )

Hope this helps someone

wagnerdelima commented 2 years ago

Thank you for your contribution @Danirill