예제 #1
0
    def test_authorization_code_error(self, mock_post):
        mock_post.return_value.status_code = 403  # Forbidden

        params = dict(grant_type='authorization_code',
                      code="D5xNoAMwrwSNI5P16zKeXxjT",
                      client_id=settings.CANVAS_OAUTH_CLIENT_ID,
                      client_secret=settings.CANVAS_OAUTH_CLIENT_SECRET,
                      redirect_uri='/oauth/oauth-callback')
        with self.assertRaises(InvalidOAuthReturnError):
            get_access_token(**params)

        mock_post.assert_called_with(self.get_token_url(), params)
def oauth_callback(request):
    """ Receives the callback from canvas and saves the token to the database.
        Redirects user to the page they came from at the start of the oauth
        procedure. """
    error = request.GET.get('error')
    if error:
        return render_oauth_error(error)
    code = request.GET.get('code')
    state = request.GET.get('state')

    if state != request.session['canvas_oauth_request_state']:
        raise InvalidOAuthStateError("OAuth state mismatch!")

    # Make the `authorization_code` grant type request to retrieve a
    access_token, expires, refresh_token = canvas.get_access_token(
        grant_type='authorization_code',
        client_id=settings.CANVAS_OAUTH_CLIENT_ID,
        client_secret=settings.CANVAS_OAUTH_CLIENT_SECRET,
        redirect_uri=request.session["canvas_oauth_redirect_uri"],
        code=code)

    CanvasOAuth2Token.objects.create(
        user=request.user, access_token=access_token,
        expires=expires, refresh_token=refresh_token)

    return redirect(request.session['canvas_oauth_initial_uri'])
예제 #3
0
    def test_authorization_code(self, mock_post, mock_timezone_now):
        access_token = "29EcPu2JpbOOlss5Lo3BzP5OK4"
        refresh_token = "Io9aGV7HT6UzKawzEkf1aevGm"
        seconds_to_expire = 3600

        # mock the json response from the token endpoint
        mock_post.return_value.status_code = 200
        mock_post.return_value.json.return_value = self.get_response_data(
            access_token=access_token,
            refresh_token=refresh_token,
            seconds_to_expire=seconds_to_expire)

        # mock timzone used to determine token expiration
        now = timezone.now()
        mock_timezone_now.return_value = now
        expires = now + timedelta(seconds=seconds_to_expire)

        # make the request
        params = dict(
            grant_type='authorization_code',
            code="D5xNoAMwrwSNI5P16zKeXxjT",
            client_id=settings.CANVAS_OAUTH_CLIENT_ID,
            client_secret=settings.CANVAS_OAUTH_CLIENT_SECRET,
            redirect_uri='/oauth/oauth-callback'
        )
        actual_tuple = get_access_token(**params)
        expected_tuple = (access_token, expires, refresh_token)

        self.assertEqual(expected_tuple, actual_tuple)
        mock_post.assert_called_with(self.get_token_url(), params)
예제 #4
0
def oauth_callback(request):
    """ Receives the callback from canvas and saves the token to the database.
        Redirects user to the page they came from at the start of the oauth
        procedure. """
    error = request.GET.get('error')
    if error:
        return render_oauth_error(error)
    code = request.GET.get('code')
    state = request.GET.get('state')

    if state != request.session['canvas_oauth_request_state']:
        raise InvalidOAuthStateError("OAuth state mismatch!")

    # Make the `authorization_code` grant type request to retrieve a
    access_token, expires, refresh_token = canvas.get_access_token(
        grant_type='authorization_code',
        client_id=settings.CANVAS_OAUTH_CLIENT_ID,
        client_secret=settings.CANVAS_OAUTH_CLIENT_SECRET,
        redirect_uri=request.session["canvas_oauth_redirect_uri"],
        code=code)

    CanvasOAuth2Token.objects.create(user=request.user,
                                     access_token=access_token,
                                     expires=expires,
                                     refresh_token=refresh_token)

    return redirect(request.session['canvas_oauth_initial_uri'])
def refresh_oauth_token(request):
    """ Makes refresh_token grant request with Canvas to get a fresh
    access token.  Update the oauth token model with the new token
    and new expiration date and return the saved model.
    """
    oauth_token = request.user.canvas_oauth2_token

    # Get the new access token and expiration date via
    # a refresh token grant
    oauth_token.access_token, oauth_token.expires, _ = canvas.get_access_token(
        grant_type='refresh_token',
        client_id=settings.CANVAS_OAUTH_CLIENT_ID,
        client_secret=settings.CANVAS_OAUTH_CLIENT_SECRET,
        redirect_uri=request.build_absolute_uri(
            reverse('canvas-oauth-callback')),
        refresh_token=oauth_token.refresh_token)

    # Update the model with new token and expiration
    oauth_token.save()

    return oauth_token
예제 #6
0
def refresh_oauth_token(request):
    """ Makes refresh_token grant request with Canvas to get a fresh
    access token.  Update the oauth token model with the new token
    and new expiration date and return the saved model.
    """
    oauth_token = request.user.canvas_oauth2_token

    # Get the new access token and expiration date via
    # a refresh token grant
    oauth_token.access_token, oauth_token.expires, _ = canvas.get_access_token(
        grant_type='refresh_token',
        client_id=settings.CANVAS_OAUTH_CLIENT_ID,
        client_secret=settings.CANVAS_OAUTH_CLIENT_SECRET,
        redirect_uri=request.build_absolute_uri(
            reverse('canvas-oauth-callback')),
        refresh_token=oauth_token.refresh_token)

    # Update the model with new token and expiration
    oauth_token.save()

    return oauth_token