Esempio n. 1
0
def authorize_github_done(request):
    received_state = request.GET['state']
    expected_state = request.session.pop(OAUTH_STATE_SESSION_KEY, None)

    if not received_state == expected_state:
        raise PowerAppError("Invalid state")

    authorization_code = request.GET['code']
    redirect_uri = build_absolute_uri(reverse('github:authorize_github_done'))

    resp = requests.post(GITHUB_ACCESS_TOKEN_ENDPOINT, data={
        'client_id': settings.GITHUB_CLIENT_ID,
        'client_secret': settings.GITHUB_CLIENT_SECRET,
        'code': authorization_code,
        'redirect_url': redirect_uri
    }, headers={'Accept': 'application/json'})

    if resp.status_code != 200:
        error = resp.headers.get('X-Error', 'Unknown Error')
        return render(request, 'github/authorize_github_done.html', {'error': error})

    access_token = resp.json()['access_token']
    OAuthToken.register(request.user, ACCESS_TOKEN_CLIENT, access_token)

    redirect_target = request.session.pop('github_auth_redirect', None)
    if not redirect_target:
        redirect_target = 'github:add_integration'

    return redirect(redirect_target)
Esempio n. 2
0
def authorize_evernote(request):
    client = utils.get_unauthorized_evernote_client()
    callback_url = build_absolute_uri(reverse('evernote_sync:authorize_evernote_done'))
    request_token = client.get_request_token(callback_url)
    request.session['evernote_request_token'] = request_token
    context = {'auth_uri': client.get_authorize_url(request_token)}
    return render(request, 'evernote_sync/authorize_evernote.html', context)
Esempio n. 3
0
def authorize_pocket(request):
    resp = requests.post(POCKET_REQUEST_ENDPOINT, data={
        'consumer_key': settings.POCKET_CONSUMER_KEY,
        'redirect_uri': build_absolute_uri(reverse('web_oauth2cb')),
    }, headers={'X-Accept': 'application/json'})
    resp.raise_for_status()
    request.session['pocket_request_token'] = resp.json()['code']

    redirect_uri = build_absolute_uri(
        reverse('powerapp_pocket:authorize_pocket_done'))
    auth_uri = extend_qs(POCKET_AUTHORIZE_ENDPOINT,
                         request_token=request.session['pocket_request_token'],
                         redirect_uri=redirect_uri)

    return render(request, 'powerapp_pocket/authorize_pocket.html',
                  {'auth_uri': auth_uri})
Esempio n. 4
0
def authorize_github_done(request):
    received_state = request.GET['state']
    expected_state = request.session.pop(OAUTH_STATE_SESSION_KEY, None)

    if not received_state == expected_state:
        raise PowerAppError("Invalid state")

    authorization_code = request.GET['code']
    redirect_uri = build_absolute_uri(reverse('github:authorize_github_done'))

    resp = requests.post(GITHUB_ACCESS_TOKEN_ENDPOINT,
                         data={
                             'client_id': settings.GITHUB_CLIENT_ID,
                             'client_secret': settings.GITHUB_CLIENT_SECRET,
                             'code': authorization_code,
                             'redirect_url': redirect_uri
                         },
                         headers={'Accept': 'application/json'})

    if resp.status_code != 200:
        error = resp.headers.get('X-Error', 'Unknown Error')
        return render(request, 'github/authorize_github_done.html',
                      {'error': error})

    access_token = resp.json()['access_token']
    OAuthToken.register(request.user, ACCESS_TOKEN_CLIENT, access_token)

    redirect_target = request.session.pop('github_auth_redirect', None)
    if not redirect_target:
        redirect_target = 'github:add_integration'

    return redirect(redirect_target)
Esempio n. 5
0
    def extra_template_context(self, request, integration):
        if not integration:
            return {}

        webhook_url = build_absolute_uri(
            reverse('github:webhook',
                    kwargs={"integration_id": integration.id}))

        return {"webhook_url": webhook_url}
Esempio n. 6
0
    def extra_template_context(self, request, integration):
        if not integration:
            return {}

        webhook_url = build_absolute_uri(
            reverse('github:webhook', kwargs={"integration_id": integration.id}))

        return {
            "webhook_url": webhook_url
        }
Esempio n. 7
0
def authorize_github(request):
    redirect_uri = build_absolute_uri(reverse('github:authorize_github_done'))
    oauth_state_str = str(uuid.uuid4())
    request.session[OAUTH_STATE_SESSION_KEY] = oauth_state_str

    auth_uri = extend_qs(GITHUB_AUTHORIZE_ENDPOINT,
                         client_id=settings.GITHUB_CLIENT_ID,
                         scope="user,repo",
                         redirect_uri=redirect_uri,
                         state=oauth_state_str)

    return render(request, 'github/authorize_github.html',
                  {'auth_uri': auth_uri})
Esempio n. 8
0
def authorize_github(request):
    redirect_uri = build_absolute_uri(reverse('github:authorize_github_done'))
    oauth_state_str = str(uuid.uuid4())
    request.session[OAUTH_STATE_SESSION_KEY] = oauth_state_str

    auth_uri = extend_qs(GITHUB_AUTHORIZE_ENDPOINT,
                         client_id=settings.GITHUB_CLIENT_ID,
                         scope="user,repo",
                         redirect_uri=redirect_uri,
                         state=oauth_state_str)

    return render(request, 'github/authorize_github.html',
                  {'auth_uri': auth_uri})
Esempio n. 9
0
    def get(self, request, integration_id=None):
        """
        Override "get" so that integration is saved upon activation
        """
        integration = self.get_integration(request, integration_id)

        if not integration:
            """
            Here we have the integration directly when user first added integration.

            This is needed because to generate webhook url, we needed to have a saved
            integration first.
            """
            service = Service.objects.get(label=self.service_label)
            integration = Integration(service_id=self.service_label,
                                      name=service.app_config.verbose_name,
                                      user=request.user)
            integration.save()

            access_token = OAuthToken.objects.get(user=integration.user,
                                                  client=ACCESS_TOKEN_CLIENT)
            resp = requests.get(
                "https://api.github.com/user",
                params={'access_token': access_token.access_token},
                headers={'Accept': 'application/json'})

            if resp.status_code != 200:
                # TODO: handle unexpect error
                pass

            initial_integration_setting = {
                SETTING_KEY_GITHUB_USER_ID: resp.json()['id'],
                SETTING_KEY_WEBHOOK_SECRET: uuid.uuid4().__str__(),
                SETTING_KEY_PROJECT: request.user.get_inbox_project()
            }

            integration.update_settings(**initial_integration_setting)

            redirect_uri = build_absolute_uri(
                reverse('github:edit_integration',
                        kwargs={"integration_id": integration.id}))

            return redirect(redirect_uri)

        return generic_views.EditIntegrationView.get(self, request,
                                                     integration.id)
Esempio n. 10
0
    def get(self, request, integration_id=None):
        """
        Override "get" so that integration is saved upon activation
        """
        integration = self.get_integration(request, integration_id)

        if not integration:
            """
            Here we have the integration directly when user first added integration.

            This is needed because to generate webhook url, we needed to have a saved
            integration first.
            """
            service = Service.objects.get(label=self.service_label)
            integration = Integration(service_id=self.service_label,
                                      name=service.app_config.verbose_name,
                                      user=request.user)
            integration.save()

            access_token = OAuthToken.objects.get(user=integration.user, client=ACCESS_TOKEN_CLIENT)
            resp = requests.get("https://api.github.com/user",
                                params={'access_token': access_token.access_token},
                                headers={'Accept': 'application/json'})

            if resp.status_code != 200:
                # TODO: handle unexpect error
                pass

            initial_integration_setting = {
                SETTING_KEY_GITHUB_USER_ID: resp.json()['id'],
                SETTING_KEY_WEBHOOK_SECRET: uuid.uuid4().__str__(),
                SETTING_KEY_PROJECT: request.user.get_inbox_project()
            }

            integration.update_settings(**initial_integration_setting)

            redirect_uri = build_absolute_uri(
                reverse('github:edit_integration', kwargs={"integration_id": integration.id}))

            return redirect(redirect_uri)

        return generic_views.EditIntegrationView.get(self, request, integration.id)
Esempio n. 11
0
def subscribe_to_todoist_calendar(integration, calendar):
    """
    Subscribe for all events from the calendar

    See https://developers.google.com/google-apps/calendar/v3/push?hl=en_US for
    more details.
    """
    if 'channel_id' in integration.settings:
        channel_id = integration.settings['channel_id']
        logger.debug('We have cal. channel %s. Skip subscription', channel_id)
        return channel_id

    client = get_authorized_client(integration.user)
    channel_id = str(uuid.uuid4())
    webhook_url = build_absolute_uri(
        reverse('gcal_sync:accept_webhook', args=(integration.id, ))
    )
    token = create_webhook_token(integration)
    resp = json_post(client, '/calendars/%s/events/watch' % calendar['id'],
                     id=channel_id, type='web_hook', address=webhook_url, token=token)
    integration.update_settings(channel_id=channel_id)
    logger.debug('Create cal. channel %s. Server replied %s', channel_id, resp)
    return channel_id
Esempio n. 12
0
 def get_oauth2cb_uri(self):
     return build_absolute_uri(reverse('web_oauth2cb'))
Esempio n. 13
0
 def get_oauth2cb_uri(self):
     return build_absolute_uri(reverse('web_oauth2cb'))