Esempio n. 1
0
def github_connect_callback(request):
    task_id = get_session_task(request)
    if task_id:
        social_token = get_social_token(user=request.user,
                                        provider=GitHubProvider.id)
        if not social_token:
            return Response({'status': 'Unauthorized'},
                            status.HTTP_401_UNAUTHORIZED)

        token_info = dict(token=social_token.token,
                          token_secret=social_token.token_secret,
                          token_expires_at=social_token.expires_at)

        save_task_integration_meta(task_id, GitHubProvider.id, token_info)

    return redirect(get_session_next_url(request, provider=GitHubProvider.id))
Esempio n. 2
0
def github_connect_callback(request):
    task_id = get_session_task(request)
    if task_id:
        social_token = get_social_token(user=request.user, provider=GitHubProvider.id)
        if not social_token:
            return Response({'status': 'Unauthorized'}, status.HTTP_401_UNAUTHORIZED)

        token_info = dict(
            token=social_token.token,
            token_secret=social_token.token_secret,
            token_expires_at=social_token.expires_at
        )

        save_task_integration_meta(task_id, GitHubProvider.id, token_info)

    return redirect(get_session_next_url(request, provider=GitHubProvider.id))
Esempio n. 3
0
def harvest_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(),
                                  reverse(request.resolver_match.url_name))
    r = requests.post(url=harvest_utils.get_token_url(),
                      data={
                          'code': code,
                          'client_id': HARVEST_CLIENT_ID,
                          'client_secret': HARVEST_CLIENT_SECRET,
                          'redirect_uri': redirect_uri,
                          'grant_type': 'authorization_code'
                      })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'token_secret': response['refresh_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user,
            provider=APP_INTEGRATION_PROVIDER_HARVEST,
            defaults=defaults)

        task_id = get_session_task(request)
        if task_id:
            token_info = {
                'token': response['access_token'],
                'refresh_token': response['refresh_token'],
                'token_extra': json.dumps(response)
            }
            save_task_integration_meta(task_id,
                                       APP_INTEGRATION_PROVIDER_HARVEST,
                                       token_info)

    return redirect(
        get_session_next_url(request,
                             provider=APP_INTEGRATION_PROVIDER_HARVEST))
Esempio n. 4
0
def slack_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(),
                                  reverse(request.resolver_match.url_name))
    r = requests.post(url=slack_utils.get_token_url(),
                      data={
                          'code': code,
                          'client_id': SLACK_CLIENT_ID,
                          'client_secret': SLACK_CLIENT_SECRET,
                          'redirect_uri': redirect_uri
                      })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user,
            provider=APP_INTEGRATION_PROVIDER_SLACK,
            defaults=defaults)

        task_id = get_session_task(request)
        if task_id:
            token_info = {
                'token': response['access_token'],
                'token_extra': json.dumps(response)
            }
            if 'bot' in response:
                token_info['bot_access_token'] = response['bot'].get(
                    'bot_access_token')
                token_info['bot_user_id'] = response['bot'].get('bot_user_id')
            save_task_integration_meta(task_id, APP_INTEGRATION_PROVIDER_SLACK,
                                       token_info)
    return redirect(
        get_session_next_url(request, provider=APP_INTEGRATION_PROVIDER_SLACK))
Esempio n. 5
0
def slack_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(),
                                  reverse(request.resolver_match.url_name))
    r = requests.post(url=slack_utils.get_token_url(),
                      data={
                          'code': code,
                          'client_id': SLACK_CLIENT_ID,
                          'client_secret': SLACK_CLIENT_SECRET,
                          'redirect_uri': redirect_uri
                      })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user,
            provider=APP_INTEGRATION_PROVIDER_SLACK,
            defaults=defaults)

        project_id = get_session_project(request)
        task_id = get_session_task(request)
        if project_id or task_id:
            token_info = {
                'token': response['access_token'],
                'token_extra': json.dumps(response),
                'team_name': response['team_name'],
                'team_id': response['team_id'],
            }

            if 'bot' in response:
                token_info['bot_access_token'] = response['bot'].get(
                    'bot_access_token')
                token_info['bot_user_id'] = response['bot'].get('bot_user_id')

            if project_id:
                meta_info = dict()

                slack_client = Slacker(response['access_token'])
                channel_response = slack_client.channels.list(
                    exclude_archived=True)
                if channel_response.successful:
                    channels = channel_response.body.get(
                        slack_utils.KEY_CHANNELS, None)

                    if channels:
                        simple_channels = []

                        for channel in channels:
                            simple_channels.append(
                                dict(id=channel.get('id', None),
                                     name=channel.get('name', None)))

                        meta_info['slack_channels'] = json.dumps(
                            simple_channels)

                groups_response = slack_client.groups.list(
                    exclude_archived=True)
                if groups_response.successful:
                    groups = groups_response.body.get(slack_utils.KEY_GROUPS,
                                                      None)

                    if groups:
                        simple_groups = []

                        for group in groups:
                            simple_groups.append(
                                dict(id=group.get('id', None),
                                     name=group.get('name', None)))

                        meta_info['slack_groups'] = json.dumps(simple_groups)

                for meta_key in token_info:
                    meta_info['slack_{}'.format(
                        meta_key)] = token_info[meta_key]
                save_project_metadata(project_id, meta_info)
            if task_id:
                save_task_integration_meta(task_id,
                                           APP_INTEGRATION_PROVIDER_SLACK,
                                           token_info)

    return redirect(
        get_session_next_url(request, provider=APP_INTEGRATION_PROVIDER_SLACK))
Esempio n. 6
0
def slack_connect_callback(request):
    code = request.GET.get('code', None)
    redirect_uri = '%s://%s%s' % (request.scheme, request.get_host(), reverse(request.resolver_match.url_name))
    r = requests.post(url=slack_utils.get_token_url(), data={
        'code': code,
        'client_id': SLACK_CLIENT_ID,
        'client_secret': SLACK_CLIENT_SECRET,
        'redirect_uri': redirect_uri
    })

    if r.status_code == 200:
        response = r.json()
        defaults = {
            'token': response['access_token'],
            'extra': json.dumps(response)
        }
        AppIntegration.objects.update_or_create(
            user=request.user, provider=APP_INTEGRATION_PROVIDER_SLACK, defaults=defaults
        )

        project_id = get_session_project(request)
        task_id = get_session_task(request)
        if project_id or task_id:
            token_info = {
                'token': response['access_token'],
                'token_extra': json.dumps(response),
                'team_name': response['team_name'],
                'team_id': response['team_id'],
            }

            if 'bot' in response:
                token_info['bot_access_token'] = response['bot'].get('bot_access_token')
                token_info['bot_user_id'] = response['bot'].get('bot_user_id')

            if project_id:
                meta_info = dict()

                slack_client = Slacker(response['access_token'])
                channel_response = slack_client.channels.list(exclude_archived=True)
                if channel_response.successful:
                    channels = channel_response.body.get(slack_utils.KEY_CHANNELS, None)

                    if channels:
                        simple_channels = []

                        for channel in channels:
                            simple_channels.append(
                                dict(
                                    id=channel.get('id', None),
                                    name=channel.get('name', None)
                                )
                            )

                        meta_info['slack_channels'] = json.dumps(simple_channels)

                groups_response = slack_client.groups.list(exclude_archived=True)
                if groups_response.successful:
                    groups = groups_response.body.get(slack_utils.KEY_GROUPS, None)

                    if groups:
                        simple_groups = []

                        for group in groups:
                            simple_groups.append(
                                dict(
                                    id=group.get('id', None),
                                    name=group.get('name', None)
                                )
                            )

                        meta_info['slack_groups'] = json.dumps(simple_groups)

                for meta_key in token_info:
                    meta_info['slack_{}'.format(meta_key)] = token_info[meta_key]
                save_project_metadata(project_id, meta_info)
            if task_id:
                save_task_integration_meta(task_id, APP_INTEGRATION_PROVIDER_SLACK, token_info)

    return redirect(get_session_next_url(request, provider=APP_INTEGRATION_PROVIDER_SLACK))