<reponame>OmarMuhammedAli/social-core<filename>social_core/backends/syrian_doctors.py
"""
Syrian Doctors OAuth2 backend
Reference for implementation: 
https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
"""

from .oauth import BaseOAuth2

class SyrianDoctorsOAuth2(BaseOAuth2):
    """Syrian Doctors authenticatoin backend"""
    name = 'syrian-doctors'
    AUTHORIZATION_URL = 'http://172.16.31.10:8083/oauth/redirect'
    ACCESS_TOKEN_URL = 'http://172.16.31.10:8083/oauth/token'
    REFRESH_TOKEN_URL = 'http://172.16.31.10:8083/oauth/token/refresh'
    ACCESS_TOKEN_METHOD = 'POST'
    STATE_PARAMETER = False
    REDIRECT_STATE = False
    EXTRA_DATA = [
        ('id', 'id'),
        ('expires_in', 'expires'),
    ]
    # SCOPE_PARAMETER_NAME = 'scope'
    # SCOPE_SEPARATOR = ' '

    def get_user_details(self, response):
        """Return user details from Syrian Doctors account"""
        fullname, first_name, last_name = self.get_user_names(
            response['data']['name']
        )
        return {
            'username': response['data'].get('email').split('@', 1)[0],
            'email': response['data'].get('email'),
            'fullname': fullname,
            'first_name': first_name,
            'last_name': last_name
        }
    
    def user_data(self, access_token, *args, **kwargs):
        """
        Loads user data from service
        returns: response: dict
        The returned response is used to populate the data in get_user_details
        """
        response = self.get_json(
            'http://172.16.31.10:8083/app/api/oauth/users/current',
            headers={'Authorization': 'Bearer {}'.format(access_token)}
        )
        # Add an id key to identify the user in the response.
        response['id'] = response['data']['id']
        return response
"""
Syrian Doctors OAuth2 backend
Reference for implementation: 
https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
"""

from .oauth import BaseOAuth2

class SyrianDoctorsOAuth2(BaseOAuth2):
    """Syrian Doctors authenticatoin backend"""
    name = 'syrian-doctors'
    AUTHORIZATION_URL = 'http://172.16.31.10:8083/oauth/redirect'
    ACCESS_TOKEN_URL = 'http://172.16.31.10:8083/oauth/token'
    REFRESH_TOKEN_URL = 'http://172.16.31.10:8083/oauth/token/refresh'
    ACCESS_TOKEN_METHOD = 'POST'
    STATE_PARAMETER = False
    REDIRECT_STATE = False
    EXTRA_DATA = [
        ('id', 'id'),
        ('expires_in', 'expires'),
    ]
    # SCOPE_PARAMETER_NAME = 'scope'
    # SCOPE_SEPARATOR = ' '

    def get_user_details(self, response):
        """Return user details from Syrian Doctors account"""
        fullname, first_name, last_name = self.get_user_names(
            response['data']['name']
        )
        return {
            'username': response['data'].get('email').split('@', 1)[0],
            'email': response['data'].get('email'),
            'fullname': fullname,
            'first_name': first_name,
            'last_name': last_name
        }
    
    def user_data(self, access_token, *args, **kwargs):
        """
        Loads user data from service
        returns: response: dict
        The returned response is used to populate the data in get_user_details
        """
        response = self.get_json(
            'http://172.16.31.10:8083/app/api/oauth/users/current',
            headers={'Authorization': 'Bearer {}'.format(access_token)}
        )
        # Add an id key to identify the user in the response.
        response['id'] = response['data']['id']
        return response
Syrian Doctors OAuth2 backend Reference for implementation:  https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html Syrian Doctors authenticatoin backend # SCOPE_PARAMETER_NAME = 'scope' # SCOPE_SEPARATOR = ' ' Return user details from Syrian Doctors account Loads user data from service         returns: response: dict         The returned response is used to populate the data in get_user_details # Add an id key to identify the user in the response.