예제 #1
0
    def get(self, request, Format=None):
        logger.info('-' * 100)
        logger.info('Reddit Oauth request =>')

        # Create a valid state integer and save it to cache
        state = Utils.save_valid_state_in_cache('oauth')
        logger.debug(f'Saving oauth_{state} in cache with status pending')

        # Obtain authorization URL
        reddit = reddit = Utils.get_reddit_instance()
        # scopes = ['*'] All OAuth scopes available
        scopes = [
            'edit',
            'history',
            'identity',
            'mysubreddits',
            'read',
            'submit',
            'subscribe',
            'vote',
        ]
        auth_url = reddit.auth.url(scopes, state)
        logger.debug(f'Reddit auth url: {auth_url}')

        return Response({
            'oauth_url': auth_url,
            'state': state
        },
                        status=status.HTTP_200_OK)
예제 #2
0
    def post(self, request, Format=None):
        logger.info('-' * 100)
        logger.info('Reddit Oauth confirmation =>')

        # First validated request.data and save the SalesforceOrg object
        org = SalesforceOrg.objects.get_or_none(
            org_id=request.data.get('org_id'))
        serializer = SalesforceOrgSerializer(instance=org, data=request.data)
        serializer.is_valid(raise_exception=True)
        org = serializer.save()
        logger.debug(f'Org data => {org}')

        state = request.query_params.get('state')
        # Get refresh token from cache
        oauth_data = cache.get(f'oauth_{state}')
        # Delete from cache after getting the data needed
        cache.delete(f'oauth_{state}')

        # Get the reddit code and generate the refresh_token
        reddit_code = oauth_data['code']
        reddit = Utils.get_reddit_instance()
        if reddit_code == 'dummy':
            refresh_token = None
            redditor_data = RedditorsUtils.get_dummy_redditor_data()
            redditor_id = redditor_data['id']
        else:
            refresh_token = reddit.auth.authorize(reddit_code)
            # Get the redditor data from API
            api_redditor = reddit.user.me()
            redditor_id = api_redditor.id
            redditor_data = RedditorsUtils.get_redditor_data(api_redditor)

        # Create or update redditor object for this client
        redditor = Redditor.objects.get_or_none(id=redditor_id)
        serializer = RedditorSerializer(instance=redditor, data=redditor_data)
        serializer.is_valid(raise_exception=True)
        redditor = serializer.save()
        logger.info(repr(redditor))
        redditor_data = serializer.data

        # Finally save the client org object
        client_org = ClientOrg.objects.get_or_none(
            redditor_id=redditor.id, salesforce_org_id=org.org_id)
        serializer = ClientOrgSerializer(
            instance=client_org,
            data={
                'connected_at': now(),
                'reddit_token': refresh_token,
                'is_active': True,
            },
        )
        serializer.is_valid(raise_exception=True)
        client_org = serializer.save(salesforce_org=org, redditor=redditor)

        # Create a random token for this client_org
        # This token will be used to authenticate the client org for all future requests
        token = Token.objects.get_or_none(client_org_id=client_org.id)
        if token:
            token.delete()
        token = Token.objects.create(client_org=client_org)
        logger.debug(f'Bearer token generated: {token.key}')

        subreddits = []
        if reddit_code != 'dummy':
            for sub in reddit.user.subreddits():
                subreddits.append(
                    SubredditsUtils.get_subreddit_data_simple(sub))

        # Return redditor data + subscriptions + token generated
        redditor_data.update(subscriptions=subreddits, bearer_token=token.key)
        return Response(redditor_data, status=status.HTTP_201_CREATED)